Palindrome Number(回文串)

题目:

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

分析:

该题目来源于leetcode。回文串是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。当然整数形式的回文串也是类似的。但负数不是回文串。两种思路:

  1. 按定义来,依次比较串的首尾,直到中间相隔1个或0个元素(取决于整数是奇数位数还是偶数位数)。优点是当不是回文串时,可以很快发现。如果是回文串,则必须比较全部位数。
  2. 根据回文串的特点,正反序,其数是相同的。优点是不必按位数来判断。

代码:

<span style="font-size:12px;font-weight: normal;">class Solution {
public:
	bool isPalindrome(int x) {
		vector<int> v;
		int i, j;
		if (x == 0)
			return true;

		while (x)
		{
			v.push_back(x % 10);
			x /= 10;
		}
		i = 0;
		j = v.size()-1;
		while (i < j)
		{
			if (v.at(i++) != v.at(j--))
				return false;
		}

		return true;
	}
};</span>

class Solution {
public:
	bool isPalindrome(int x) {
		int y = 0;
		int t = x;

		if (t < 0) //负数不是回文串
			return false;

		while (t)
		{
			y = y * 10 + t % 10;
			t /= 10;
		}
		if (y ^ x) //判断正反序是否相等
			return false;

		return true;
	}
};
时间: 2024-12-22 06:10:58

Palindrome Number(回文串)的相关文章

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

UVa 10617 Again Palindrome(回文串区间DP)

UVa 10617 Again Palindrome(经典回文串区间DP) 题意: 给定一个字符串s,对s进行删除操作,使得剩下的子串是回文字符串,问最多有多少种这种子串. 思路: 涉及到回文字符串,首先要想到的肯定是区间DP,如何写出状态转移方程? 直接从题意切入:dp[i, j]表示区间[i, j]最多有多少个这样的子串. 1. s[i] == s[j] 去掉s[i],则一个子问题就是dp[i+1, j]; 去掉s[j],另一个子问题就是dp[i, j-1]; 显然这两个子问题是会有重叠的,

Uva 10617 Again Palindrome (DP+回文串)

Problem I Again Palindromes Input: Standard Input Output: Standard Output Time Limit: 2 Seconds A palindorme is a sequence of one or more characters that reads the same from the left as it does from the right. For example, Z, TOT and MADAM are palind

bzoj 3768: spoj 4660 Binary palindrome二进制回文串

Description 给定k个长度不超过L的01串,求有多少长度为n的01串S满足: 1.该串是回文串 2.该串不存在两个不重叠的子串,在给定的k个串中. 即不存在a<=b<c<=d,S[a,b]是k个串中的一个,S[c,d]是k个串中的一个 (It does not contain two non-overlapped substrings in the given list of K binary strings.) 举个例子,若给定2(k=2)个01串:101和1001 1010

HDU 1513 Palindrome 求回文串

这个题是走弯路了,刚开始自己DP出了方程,无限MLE,唉 if(s1[i]==s1[j]) dp[i][j]=dp[i+1][j-1]; else dp[i][j]=min(dp[i][j-1],dp[i+1][j]) +1; 后来百度了一下,这个原来是个经典回文串问题,即先将串置反,然后求LCS........ 然后就是这题卡时间卡的特别厉害,多用了一次strlen就TLE AC: #include<cstdio> #include<string> #include<str

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

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