zoukankan      html  css  js  c++  java
  • LeetCode:Palindrome Number,Reverse Integer

    Determine whether an integer is a palindrome. Do this without extra space.
    
    click to show spoilers.
    Some hints:
    
    Could negative integers be palindromes? (ie, -1)
    
    If you are thinking of converting the integer to string, note the restriction of using extra space.
    
    You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
    
    There is a more generic way of solving this problem.

    这个我是用java做的,感觉只要使用高级语言的话就很简单:

    1:负数返回0;

    2:将数字转化为字符对象,使用StringBufer进行倒置,然后equals()就可以了.

    public class Solution {
        public boolean isPalindrome(int x) {
            if (x < 0) return false;
            String str1 = String.valueOf(x);
            StringBuffer sb = new StringBuffer(str1);
            String str2 = sb.reverse().toString();
            if(str1.equals(str2))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    下面是第二个:Reverse Integer

    Reverse digits of an integer.
    
    Example1: x = 123, return 321
    Example2: x = -123, return -321
    
    click to show spoilers.
    Have you thought about this?
    
    Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
    
    If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
    
    Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
    
    For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
    
    Update (2014-11-10):
    Test cases had been added to test the overflow behavior.

    1:判断正负数

    2:倒置字符串

    3:构造数字

    public int reverse(int x) {
            long result = 0;
            boolean flag = false;
            String str = String.valueOf(x);
            if(str.startsWith("-"))
            {
                flag = true;
                str = str.substring(1);
            }
            else if(str.startsWith("+"))
            {
                flag = false;
                str = str.substring(1);
            }
            str = new StringBuffer(str).reverse().toString();
            for(int i = 0; i < str.length(); i++)
            {
                result = result * 10 + (str.charAt(i) - '0');
                if(result > Integer.MAX_VALUE)
                {
                    return 0;
                }
                
            }
            if(flag)
            {
                result = result * (-1);
            }
            return (int)result;
        }
    这里的result设置为long类型,因为int类型可以会溢出,同时与Integer.MAX_VALUE有比较。
  • 相关阅读:
    JS 面向对象
    inline-block元素间隙处理
    MUI
    MUI
    chrome://inspect调试html页面空白,DOM无法加载的解决方案
    MUI
    MUI
    MUI
    MUI
    MUI
  • 原文地址:https://www.cnblogs.com/dslover/p/4318022.html
Copyright © 2011-2022 走看看