LeetCode算法练习题Java

LeetCode算法练习题Java

242. Valid Anagram
Total Accepted: 50994 Total Submissions: 127543 Difficulty: Easy

Given two strings s and t, write a function to determine if t is an anagram of s.

For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.

Note:
You may assume the string contains only lowercase alphabets.

public class Solution {
    public boolean isAnagram(String s, String t) {
        if (s == null || t == null) {
            return false;
        }
        if (s == "" && t == "") {
            return true;
        }
        if (s.length() != t.length()) {
            return false;
        }
        char[] slist = s.toCharArray();
        Arrays.sort(slist);
        char[] tlist = t.toCharArray();
        Arrays.sort(tlist);
        s = new String(slist);
        t = new String(tlist);
        if (s.equals(t)) {
            return true;
        }
        return false;
    }
}
=====================================================================================
326. Power of Three
Total Accepted: 7096 Total Submissions: 19850 Difficulty: Easy

Given an integer, write a function to determine if it is a power of three.

Follow up:
Could you do it without using any loop / recursion?

public class Solution {
    public boolean isPowerOfThree(int n) {
        double res = Math.log(n) / Math.log(3);
        return Math.abs(res - Math.rint(res)) < 0.0000000001;
    }
}

=============================================================
303. Range Sum Query - Immutable
Total Accepted: 14383 Total Submissions: 60060 Difficulty: Easy

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.

Example:

Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3

Note:

    You may assume that the array does not change.
    There are many calls to sumRange function.

public class NumArray {

    public int[] sums;
 
    public NumArray(int[] nums) {
        if (nums == null) {
            sums = null;
        } else if (nums.length == 0) {
            sums = new int[0];
        } else {
            this.sums = new int[nums.length];
            sums[0] = nums[0];
            for (int i = 1; i < nums.length; i++) {
                sums[i] = sums[i - 1] + nums[i];
            }
        }
    }
 
    public int sumRange(int i, int j) {
        if (sums == null) {
            return 0;
        }
        if (i >= sums.length || j >= sums.length || i > j) {
            return 0;
        } else if (i == 0) {
            return sums[j];
        } else {
            return sums[j] - sums[i - 1];
        }
    }
}
===========================================================================    
263. Ugly Number
Total Accepted: 36897 Total Submissions: 104746 Difficulty: Easy

Write a program to check whether a given number is an ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

Note that 1 is typically treated as an ugly number.
public class Solution {
     public boolean isUgly(int num) {
        if (num <= 0)
            return false;
        if (num == 1)
            return true;

        while (num >= 2 && num % 2 == 0)
            // 因子2已经被除完
            num /= 2;
        while (num >= 3 && num % 3 == 0)
            num /= 3;
        while (num >= 5 && num % 5 == 0)
            num /= 5;

        return num == 1;
    }
}
========================================================================


26. Remove Duplicates from Sorted Array
Total Accepted: 108780 Total Submissions: 333801 Difficulty: Easy

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

Subscribe to see which companies asked this question
public class Solution {
    public int removeDuplicates(int[] nums) {
        int len = nums.length;
        if (len == 0)
            return 0;
        int count = 1;
        for (int i = 1; i < len; i++) {
            if (nums[i] == nums[i - 1]) {
                continue;
            }else{
                nums[count] = nums[i];
                count++;
            }
        }
        return count;
    }
}




你可能感兴趣的:(LeetCode算法练习题Java)