LeetCode Top Interview 150

194 阅读2分钟

Math

Q****9. Palindrome Number

Given an integer x, return trueif x is a palindrome, and false otherwise.

Example 1:

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Constraints:

  • -231 <= x <= 231 - 1

Follow up: Could you solve it without converting the integer to a string?

解法及注释:

class Solution {    public boolean isPalindrome(int x) {        //pre-check        if(x < 0 || x > Math.pow(2,31)-1 || (x != 0 && x %10 == 0 )) {            return false;        }        if(x < 10) {            return true;        }         return useString(x);        //return useMath(x);    }    private boolean useString(int x) {        String s = String.valueOf(x);        int n = s.length();        for(int i = 0; i < n/2; i++) {            if(s.charAt(i) != s.charAt(n-i-1)) {                return false;            }        }        return true;    }    private boolean useMath(int x) {        int rev = 0;        while(x > rev) {            rev = rev * 10 + x % 10;            x /= 10;        }        return (x == rev || x == rev/10);     }}

Q***66. Plus One

You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.

Increment the large integer by one and return the resulting array of digits.

Example 1:

Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].

Example 2:

Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
Incrementing by one gives 4321 + 1 = 4322.
Thus, the result should be [4,3,2,2].

Example 3:

Input: digits = [9]
Output: [1,0]
Explanation: The array represents the integer 9.
Incrementing by one gives 9 + 1 = 10.
Thus, the result should be [1,0].

Constraints:

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9
  • digits does not contain any leading 0's.

解法及注释:

class Solution {    public int[] plusOne(int[] digits) {        //pre-check        if(digits.length  == 0 || digits.length > 100) {            return digits;        }        int n = digits.length, curr = 0, add = 0;        for(int i = n - 1; i >= 0; i--) {            if(add == 1) {                curr = digits[i] + add;            } else {                curr = digits[i] + 1;            }            if(curr >= 10) {                add = 1;                digits[i] = 0;            } else {                add = 0;                digits[i] = curr;                break;            }        }        if(add != 1) {            return digits;        } else {            int[] res = new int[n + 1];            System.arraycopy(digits, 0, res, 1, n);            res[0] = 1;            return res;        }    }}

Q**172. Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes inn!.

Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.

Example 1:

Input: n = 3
Output: 0
Explanation: 3! = 6, no trailing zero.

Example 2:

Input: n = 5
Output: 1
Explanation: 5! = 120, one trailing zero.

Example 3:

Input: n = 0
Output: 0

Constraints:

  • 0 <= n <= 104

解法及注释:

class Solution {
    public int trailingZeroes(int n) {
        //recursion apporach
       return n == 0 ? 0 : n/5 + trailingZeroes(n / 5);  

       //loop approach
       //return getTrailingZeros(n);
    }

    private int getTrailingZeros(int n) {
        int count = 0;
        while(n > 0) {
            n /= 5;
            count += n;
        }

        return count;
    }
}

Q***50. Pow(x, n)

Implement pow(x, n), which calculates x raised to the power n (i.e., xn).

Example 1:

Input: x = 2.00000, n = 10
Output: 1024.00000

Example 2:

Input: x = 2.10000, n = 3
Output: 9.26100

Example 3:

Input: x = 2.00000, n = -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25

Constraints:

  • -100.0 < x < 100.0
  • -231 <= n <= 231-1
  • n is an integer.
  • Either x is not zero or n > 0.
  • -104 <= xn <= 104

解法及注释:

class Solution {    public double myPow(double x, int n) {        int positiveN = Math.abs(n);        if(n < 0) return 1 / myPowCalc(x, positiveN);        return myPowCalc(x, n);    }    private double myPowCalc(double x, int n) {        if(x == 1 || n == 0) return 1;        double temp = myPowCalc(x, n / 2);        if(n % 2 == 0) {            return temp * temp;        } else {            return x * temp * temp;        }    }}

Q***69. Sqrt(x)

Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.

You must not use any built-in exponent function or operator.

  • For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.

Example 1:

Input: x = 4
Output: 2
Explanation: The square root of 4 is 2, so we return 2.

Example 2:

Input: x = 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.

Constraints:

  • 0 <= x <= 231 - 1

解法及注释:

class Solution {    public int mySqrt(int x) {        //pre-check        if(x <= 0 || x > Math.pow(2,31) - 1) {            return 0;        }        //use binary search        int left = 1, right = x;        while(left <= right) {            int mid = left + (right - left) / 2;            if(mid > x / mid) {                right = mid - 1;            } else if (mid < x / mid) {                left = mid + 1;            } else {                return mid;            }        }        return right;    }}