LeetCode 9 Palindrome Number 回文数字

题目:Determine whether an integer is a palindrome. Do this without extra space.

翻译:判断一个数字是否是回文数,不要额外空间。

解题思路:因为数字既然传过去了,就不会有越界的问题。每次只需要取最前面和最后面的那一位数字进行比较,相同则继续,不同则返回、

首先要获取数字的位数,假设数字是12344321,一共有8位。

其次是要每次取前后各一位来进行比较,用数字除以1后面7个0得到第一位,用数字对10取余数得到最后一位。

此时要比较第二位和倒数第二位,只需将数字对1后面7个0取余后,在除以10.这样数字就变为234432,然后再把位数除以100。循环可得到结果。

public class Solution {
   	 public static boolean isPalindrome(int x) {
	     if(x <0) return false;

	     int length = 1;//数字的位数
	     while(x/10 >= length)
	     {
	    	 length *=10;
	     }
		 while(x>0)
		 {
			 int high = x /length;
			 int low = x % 10;
			 if(high != low)
				 return false;
			 x = (x%length)/10;
			 length = length /100;
		 }
		 return true;
	    }

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		int x = 1234554321;
		boolean t = isPalindrome(x);
		System.out.println(t);

	}
}

做完后,查看别人的做法,发现有个不错的。采用的是递归,

粘在这里,方便以后复习用、

代码2:http://blog.csdn.net/ithomer/article/details/8798274

    bool check(int x, int &y) {
        if (x == 0) return true;  

        if (check(x/10, y) && (x%10 == y%10)) {
            y /= 10;
            return true;
        } else {
            return false;
        }
    }  

    bool isPalindrome(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(x < 0) return false;  

        return check(x, x);
    } 
时间: 2024-10-10 13:54:44

LeetCode 9 Palindrome Number 回文数字的相关文章

LeetCode (30) Palindrome Number (回文数字)

题目描述 Determine whether an integer is a palindrome. Do this without extra space. 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.

[LeetCode]68. Palindrome Number回文数字

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 ext

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

leetcode 9 Palindrome Number 回文数

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 ext

[LeetCode]9. Palindrome Number回文数

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to

Leetcode——3 Palindrome Number(回文数)

Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过,不过从基础开始尝试吧. Solution: public class Solution { public boolean isPalindrome(int x) { int n=1; int copyx=x; if(x<0)return false; if(x<10)return true; w

LeetCode Valid Palindrome 有效回文(字符串)

1 class Solution { 2 public: 3 bool isPalindrome(string s) { 4 if(s=="") return true; 5 if(s.length()==1) return true; //单个字符,对称 6 char *p,*q; 7 p=&s[0]; //p指向开头 8 q=&s[s.length()-1]; //q指向末尾 9 while(p!=q){ 10 //测试字符串里是否有字母或数字,若没有,则立刻返回

9. Palindrome Number 回文 my second leetcode 20170807

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 ext

LeetCode Problem 9:Palindrome Number回文数

描述:Determine whether an integer is a palindrome. Do this without extra space. 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 a