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 size = 0;
		int tx = x;
		while(tx){
			tx /= 10;
			size++;
		}
		return size;
	}
    bool isPalindrome(int x) {
		if(x<0)return false;
		else if(x>=0 && x<=9)return true;
		else{
			int size = _size(x);
			int head = x/(int)pow(10,size-1);
			int tail = x%10;
			if(head != tail){
				return false;
			}
			x = x%(int)pow(10,size-1);
			x = x/10;
			int newsize = _size(x);
			if(size-newsize > 2)
				if(x%(int)pow(10, size-newsize-2) == 0) //考虑数后跟着一串0的情况
					x = x/(int)pow(10, size-newsize-2);
				else
					return false;
			return isPalindrome(x);
		}
    }
};

【思路2】

先生成原数的reverse Number, 然后跟原数比较

【代码】

class Solution {
public:
    bool isPalindrome(int x) {
	    long long reverseX=0;
	    if(x<0)return false;
	    int xcopy=x;
	    while(xcopy){       //将整数倒置
	        reverseX=reverseX*10+xcopy%10;
	        xcopy/=10;
	    }
	    if(reverseX>INT_MAX)return false;   //判断倒置后的结果是否越界
	    if(x==(int)reverseX)return true;      //判断倒置结果和原数是否相等
	    return false;
    }
};

LeetCode 009 Palindrome Number,布布扣,bubuko.com

时间: 2024-08-27 02:08:16

LeetCode 009 Palindrome Number的相关文章

[LeetCode] 009. Palindrome Number (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 009.Palindrome_Number (Easy) 链接: 题目:https://oj.leetcode.com/problems/palindrome-number/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个数是否是回文数. 分析: 按自己想

Java for LeetCode 009 Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. 解题思路: 由于题目中给定不能使用额外空间开销,因此不能转为String类型,我们需要一位一位处理. Java代码如下: static public boolean isPalindrome(int x) { if (x < 0) return false; int temp = x; int beginIndex = 0, endIndex =

LeetCode 009 Palindrome Number - Java

Determine whether an integer is a palindrome. Do this without extra space. 定位:简单题 题目要求判断给出的数字是否是回文数,并且要求不适用额外空间.我们不能使用其他数据类型过度处理,那从个位开始计算,每提高计算一位将原先的值*10+新值,结束后判断是否与原值相同即可. Java实现: 1 public class Solution { 2 public boolean isPalindrome(int x) { 3 in

LeetCode——009 Palindrome Number

Description 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. F

【LeetCode】009 Palindrome Number

题目:LeetCode 009 Palindrome Number 题意:判断一个整数是否为回文数,不要用额外空间 思路:我不会不用额外空间的方法,需要利用一个长度为20以内的字符串.将整数先写入一个字符串,然后判断首位字符是否相等即可. 代码如下: 1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 string s = to_string(x); 5 int len = s.size(); 6 for(int i = 0;

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 数 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 integ

【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