• 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;
        }
    }
    

      

  • 相关阅读:
    #2019120500018-LG 小雨的数字游戏
    假期Noip笔记
    #2019120500016 逆序对与归并排序
    #2019120500015-LG 全排列
    #2019120500014-LG 采药
    #2019120500013-LG 合并果子
    二分与三分
    #2019120500012-LG 小鱼比可爱
    #2019120500011-LG 约瑟夫问题&玩具谜题
    HDU 5738 共线点集
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/11244811.html
Copyright © 2020-2023  润新知