Leetcode 数 Palindrome Number

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

Palindrome Number

Total Accepted: 12165 Total
Submissions: 41736

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.

题意:判断给定的数字是不是回文,不可以用额外空间

思路:

取首位取末位,检查是否相同。

如果不同,返回false

如果相同,去掉首位去掉末位,继续上面的操作

复杂度:时间O(log n), 空间O(1)

注:对于像102201这种中间有零的情况,因为x等于220的时候,n还是1000,所以x / n还是能得到220前面的0

相关题目:Reverse Integer

class Solution {
public:
	bool isPalindrome(int x) {
		if(x < 0) return false;
		int n = 1;
		while(x / n >= 10){
			n *= 10;
		}
		while(x){
			int a = x / n;   //取首位数字
			int b = x % 10;  //取末位数字
			if(a != b) return false;
			x %= n ; //去掉首位数字
			x /= 10; //去掉末位数字
			n /= 100; //n要对应去掉两位
		}
		return true;
	}
};

Leetcode 数 Palindrome Number,布布扣,bubuko.com

时间: 2024-08-05 07:06:34

Leetcode 数 Palindrome Number的相关文章

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 009 Palindrome Number

[题目] Determine whether an integer is a palindrome. Do this without extra space. [题意] 题意判断一个整数是否是回文数 注意一下几点: 1. 不能用额外的空间 2. 负数不是回文数 [思路1] 依次比较首位值,需要注意处理类似1002001这样的数,当比较完首位值之后余下的数变成200, 2之前的两个0会自动清除. [代码] class Solution { public: int _size(int x){ int

leetcode 9. 判断整数是否是回数 Palindrome Number

Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. 注:回数是指正读和倒读都一样的数,如121,234432 解题分析: 需要注意点 (1)负数不是回数 (2)个位数都是回数 思路: (1)使用前一题的整数的倒序,将一个数倒序,然后和原值比较:存在的问题是可能有溢出,需要计算每一位,影响高效,有改进空间 (2)逐个比较最高位和最低位,不等跳出,相等就比较次高位和次

【LeetCode】Palindrome Number

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 restri

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 (回文数)

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

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