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有比较。
  • 相关阅读:
    Java提高班(五)深入理解BIO、NIO、AIO
    Java提高班(四)面试必备—你不知道的数据集合
    Spring Boot 系列总目录
    Java提高班(三)并发中的线程同步与锁
    Java提高班(二)深入理解线程池ThreadPool
    Spring Boot(十四)RabbitMQ延迟队列
    Spring Boot(十三)RabbitMQ安装与集成
    Spring Boot(十二)单元测试JUnit
    Spring Boot(十一)Redis集成从Docker安装到分布式Session共享
    VS2013中Python学习笔记[环境搭建]
  • 原文地址:https://www.cnblogs.com/dslover/p/4318022.html
Copyright © 2011-2022 走看看