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有比较。
  • 相关阅读:
    高速C/C++编译工具(ccache)
    CentOS7关闭自动下载更新
    GCC中同时使用动态和静态库链接的编译
    porting libiconv to android(arm)——libiconv-1.14.tar.gz
    编译cBPM-android-19—CodeBlocks—CentOS7— ndk10—编译libiconv和xerces-c
    Trying to build Xerces-C++ for Android
    计算机安全技术(第二版)第2版
    为android提供的部分第三方C/C++静态库—libsqlite—libuuid—libevent_static
    error: undefined reference to '__aeabi_uidiv'
    编译cBPM-android—CodeBlocks(全局、局部)参数设置—CentOS 7— android-ndk
  • 原文地址:https://www.cnblogs.com/dslover/p/4318022.html
Copyright © 2011-2022 走看看