zoukankan      html  css  js  c++  java
  • palindrome-number

    /**
    * @author gentleKay
    * 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.
    *
    * 确定整数是否为回文。这样做没有额外的空间。
    * 单击以显示扰流器。
    * 一些提示:
    * 负整数可以是回文吗?(即-1)
    * 如果要将整数转换为字符串,请注意使用额外空间的限制。
    * 您还可以尝试反转一个整数。但是,如果您解决了“反向整数”问题,
    * 您就会知道反向整数可能溢出。你将如何处理这样的案件?
    * 有一种更通用的方法来解决这个问题。
    */

    /**
     * @author gentleKay
     * 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.
     * 
     * 确定整数是否为回文。这样做没有额外的空间。
     * 单击以显示扰流器。
     * 一些提示:
     * 负整数可以是回文吗?(即-1)
     * 如果要将整数转换为字符串,请注意使用额外空间的限制。
     * 您还可以尝试反转一个整数。但是,如果您解决了“反向整数”问题,
     * 您就会知道反向整数可能溢出。你将如何处理这样的案件?
     * 有一种更通用的方法来解决这个问题。
     */
    
    public class Main12 {
    	public static void main(String[] args) {
    		int x = 123421;
    		System.out.println(Main12.isPalindrome(x));
    	}
    	
    	public static boolean isPalindrome(int x) {
    		if (x < 0) {
    			return false;
    		}
    		int last = 0;
    		int res = x;
    		while (res != 0) {
    			last = last*10 + res%10;
    			res = res / 10;
    		}
    		return last == x;
        }
    }
    

      

  • 相关阅读:
    数据库之主表、从表、主键、外键
    eclipse编写js代码没有提示
    思维导图xmind的使用方法
    整理一下Apache与Tomcat的关系
    全栈开发者,一个很好的自学编程网站
    svn文件被锁不能提交的解决办法
    在SQL Server数据库中执行存储过程很快,在c#中调用很慢的问题
    php安装redis扩展
    PHP点击按钮拷贝
    PHP文件下载
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/11244811.html
Copyright © 2011-2022 走看看