Leetcode 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 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.

分析:

回文数,并且题目要求不能使用额外的空间。

即,不能使用回文串的方法。

本题很简单,算出x的倒置数a,比较a是否和x相等就行了。

 1 class Solution {
 2 public:
 3     bool isPalindrome(int x) {
 4         //算出x的倒置数a,比较a是否和x相等就行了
 5         int a = 0, b = x;
 6         while(b > 0){
 7             a = a * 10 + b % 10;
 8             b /= 10;
 9         }
10         if(a == x)
11             return true;
12         else
13             return false;
14
15     }
16 };

网上其他的代码,其思路:每次提取头尾两个数,判断它们是否相等,判断后去掉头尾两个数

 1 class Solution {
 2 public:
 3     bool isPalindrome(int x) {
 4
 5         //negative number
 6         if(x < 0)
 7             return false;
 8
 9         int len = 1;
10         while(x / len >= 10)
11             len *= 10;
12
13         while(x > 0)    {
14
15             //get the head and tail number
16             int left = x / len;
17             int right = x % 10;
18
19             if(left != right)
20                 return false;
21             else    {
22                 //remove the head and tail number
23                 x = (x % len) / 10;
24                 len /= 100;
25             }
26         }
27
28         return true;
29     }
30 };
时间: 2024-10-19 09:32:32

Leetcode 9. Palindrome Number(判断回文数字)的相关文章

LeetCode 9 Palindrome Number(回文数字判断)

Long Time No See ! 题目链接https://leetcode.com/problems/palindrome-number/?tab=Description 首先确定该数字的位数.按照已知数字x对10进行多次求余运算,可以得到数字位数. 具体思路: 1.每次取出该数字的最高位和最低位进行比较. 2.如果不相等则直接返回FALSE, 3.如果相等修改x的值(去掉最高位也同时去掉最低位)其中去掉最高位可以通过求模运算,去掉最低位可以采用除以10 4.进行循环直到x的值不大于0为止

[LeetCode] 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 -- 判断回文数

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 ex

LeetCode 9 Palindrome Number (回文数)

翻译 确定一个整数是否是回文数.不能使用额外的空间. 一些提示: 负数能不能是回文数呢?(比如,-1) 如果你想将整数转换成字符串,但要注意限制使用额外的空间. 你也可以考虑翻转一个整数. 然而,如果你已经解决了问题"翻转整数(译者注:LeetCode 第七题), 那么你应该知道翻转的整数可能会造成溢出. 你将如何处理这种情况? 这是一个解决该问题更通用的方法. 原文 Determine whether an integer is a palindrome. Do this without ex

LeetCode OJ Palindrome Number(回文数)

1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 int r=0,init=x; 5 if(init==0) return true; 6 if(init<0) return false; 7 while(init!=0){ 8 r=r*10+init%10; 9 init=init/10; 10 } 11 if(r==x) 12 return true; 13 else 14 return false; 15 } 16 };

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题解:Valid Palindrome(判断回文)

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider tha

Palindrome Number (回文数)

回文数是指这样的数字:正读和倒读都是一样的.如:595,2332都是回文数,234不是回文数. 注意:负数不是回文数 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

Palindrome-Number(判断回文数字)

判断一个数是否为回文数字,如1,2,3,121,1001,999都是回文数字,10,9898就不是回文数字. 解法:判断对称中心两端数字是否相同. 代码如下: bool isPalindrome(int x) { if (x<0 || (x != 0 && x % 10 == 0)) return false; int sum = 0; while (x>sum) { sum = sum * 10 + x % 10; x = x / 10; } return (x == sum