LeetCode - 9. Palindrome Number
9. Palindrome Number
Problem's Link
----------------------------------------------------------------------------
Mean:
給你一個數,判斷這個數是不是迴文數.
analyse:
水題.
Time complexity: O(N)
view code
/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
* Author: crazyacking
* Date : 2016-02-15-16.34
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);
class Solution
{
public:
bool isPalindrome(int x)
{
if(x<0) return 0;
char s[20];
int cnt=0;
while(x)
{
int tmp=x%10;
x/=10;
s[cnt++]=tmp+'0';
}
s[cnt]='\0';
int len=strlen(s);
int midLen=len/2;
int endPos=len-1;
for(int i=0;i<midLen;++i,--endPos)
{
if(s[i]!=s[endPos])
return 0;
}
return 1;
}
};
int main()
{
Solution solution;
int n;
while(cin>>n)
{
if(solution.isPalindrome(n))
puts("Yes.");
else puts("No.");
}
return 0;
}
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
* Author: crazyacking
* Date : 2016-02-15-16.34
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);
class Solution
{
public:
bool isPalindrome(int x)
{
if(x<0) return 0;
char s[20];
int cnt=0;
while(x)
{
int tmp=x%10;
x/=10;
s[cnt++]=tmp+'0';
}
s[cnt]='\0';
int len=strlen(s);
int midLen=len/2;
int endPos=len-1;
for(int i=0;i<midLen;++i,--endPos)
{
if(s[i]!=s[endPos])
return 0;
}
return 1;
}
};
int main()
{
Solution solution;
int n;
while(cin>>n)
{
if(solution.isPalindrome(n))
puts("Yes.");
else puts("No.");
}
return 0;
}
相關文章
- [LeetCode] 9. Palindrome NumberLeetCode
- leetcode Palindrome NumberLeetCode
- Leetcode 9 Palindrome NumberLeetCode
- Palindrome Number leetcode javaLeetCodeJava
- LeetCode 第 9 題(Palindrome Number)LeetCode
- LeetCode Palindrome Number(009)解法總結LeetCode
- LeetCode - 解題筆記 - 8 - Palindrome NumberLeetCode筆記
- leetcode學習筆記09 palindrome-numberLeetCode筆記
- 【LeetCode從零單排】No.9 Palindrome NumberLeetCode
- [LeetCode] Palindrome Number 驗證迴文數字LeetCode
- leetcode第九題Palindrome Number 驗證迴文數字LeetCode
- 9 check Palindrome Number by using pythonPython
- LeetCode-Palindrome PairsLeetCodeAI
- Leetcode Valid PalindromeLeetCode
- LeetCode-Palindrome Permutation IILeetCode
- 【LeetCode】Palindrome Pairs(336)LeetCodeAI
- LeetCode-Valid PalindromeLeetCode
- Leetcode: Palindrome Partitioning IILeetCode
- Leetcode Palindrome Linked ListLeetCode
- Valid Palindrome leetcode javaLeetCodeJava
- Palindrome Partitioning leetcode javaLeetCodeJava
- Leetcode Number of islandsLeetCode
- LeetCode:Largest NumberLeetCode
- [LeetCode] 336. Palindrome PairsLeetCodeAI
- LeetCode-Palindrome Linked ListLeetCode
- Leetcode-Palindrome Partitioning IILeetCode
- leetcode刷題--Valid PalindromeLeetCode
- LeetCode: Valid Palindrome [125]LeetCode
- Palindrome Partitioning II Leetcode javaLeetCodeJava
- [LeetCode/LintCode] Largest Palindrome ProductLeetCode
- LeetCode131:Palindrome PartitioningLeetCode
- LeetCode132:Palindrome Partitioning IILeetCode
- [LeetCode] Third Maximum NumberLeetCode
- [LeetCode] Find the Duplicate NumberLeetCode
- LeetCode-Strobogrammatic NumberLeetCode
- LeetCode-Largest NumberLeetCode
- LeetCode-Number of IslandsLeetCode
- Leetcode Valid NumberLeetCode