SHU-2016-SummerPractice / AlgorithmExerciseIssues

算法训练相关文件及工单。
https://github.com/SHU-2016-SummerPractice/AlgorithmExerciseIssues/issues
2 stars 0 forks source link

数字处理 2016-7-29 #19

Open zhaokuohaha opened 7 years ago

zhaokuohaha commented 7 years ago

LeetCode7. https://leetcode.com/problems/reverse-integer/

LeetCode9. https://leetcode.com/problems/palindrome-number/

zhaokuohaha commented 7 years ago

9 - C

  1. 负数不是回文
  2. 千万不要改变原数
  3. 边界数据: 10
public class Solution
{
    public bool IsPalindrome(int x)
    {
        if (x < 0) return false;
        if(x < 10) return true;
        int divten = 10;
        int resten = 10;
        while (x / divten >= 10) divten *= 10;
        while (resten <= divten)
        {
            if ((x / divten) % 10 != (x % resten) / (resten/10))
                return false;
            divten /= 10;
            resten *= 10;
        }
        return true;
    }
}
zhaokuohaha commented 7 years ago

7 - C# - 别人的方法(投机取巧)

public class Solution
{
    public int Reverse(int x)
    {
        long res = 0;
        while (x != 0)
        {
            res = (x % 10) + (res * 10);
            if (res > Int32.MaxValue || res < Int32.MinValue) return 0;
            x /= 10;
        }
        return (int)res;
    }
}
dayang commented 7 years ago
/**
 * [AC] LeetCode 7 Reverse Integer
 * @param {number} x
 * @return {number}
 */
var reverse = function(x) {
    var intMax = Math.pow(2,31) - 1;
    var intMin = -Math.pow(2,31);
    var res = 0;
    while(x !== 0){
        res = res * 10 + x % 10;
        if(res < intMin || res > intMax)
            return 0;
        x = (x - x % 10) / 10;
    }
    return res;
};
SnackMen commented 7 years ago
/*
*[AC] LeetCode 7 Reverse Integer
*换种思路,利用java库
*/
public class Solution {
    public int reverse(int x) {
    if(x==0 || x==Integer.MIN_VALUE)
        return 0;
      while(x%10==0)
            x/=10;
        if(x>0){
            return reverseInt(x);
        }

        return -1*reverseInt((-1)*x);
    }
    public int reverseInt(int x){
        StringBuilder sb = new StringBuilder(String.valueOf(x));
        String string = sb.reverse().toString();
        Long lo=Long.parseLong(string);
        if(lo>Integer.MAX_VALUE || lo<Integer.MIN_VALUE)
            return 0;
        return Integer.parseInt(string);

    }
}
dayang commented 7 years ago
/**
 * [AC] LeetCode 9 Palindrome Number
 * 计算后一半与前一半比较
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function(x) {
    if(x < 0 || (x > 0 && x % 10 === 0))
        return false;

    let sum = 0;
    while(x > sum){
        sum = sum * 10 + x % 10;
        x = Math.floor(x / 10);
    }

    return x === sum || x === Math.floor(sum / 10);
};
SnackMen commented 7 years ago
/*
*[AC] LeetCode 9 Palindrome Number
*全部跑完比较
*/
public class Solution {
    public boolean isPalindrome(int x) {
        if(x<0)
            return false;
        int num=0;
        int y=x;
        while(x>0){
            num=num*10+x%10;
            x/=10;
        }
        return y==num;
    }
}