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 9 Palindrome NumberLeetCode
- LeetCode Palindrome Number(009)解法總結LeetCode
- LeetCode - 解題筆記 - 8 - Palindrome NumberLeetCode筆記
- leetcode學習筆記09 palindrome-numberLeetCode筆記
- leetcode第九題Palindrome Number 驗證迴文數字LeetCode
- 牛課題霸--palindrome-number
- [LeetCode/LintCode] Largest Palindrome ProductLeetCode
- [LeetCode] 336. Palindrome PairsLeetCodeAI
- Leetcode 234. Palindrome Linked ListLeetCode
- Leetcode Number of islandsLeetCode
- [LeetCode] Third Maximum NumberLeetCode
- [LeetCode] Find the Duplicate NumberLeetCode
- Leetcode 447 Number of BoomerangsLeetCodeOOM
- Leetcode 933 Number of Recent CallsLeetCode
- LeetCode之Fibonacci Number(Kotlin)LeetCodeKotlin
- Leetcode 611 javascript Valid Triangle NumberLeetCodeJavaScript
- LeetCode之Number of Recent Calls(Kotlin)LeetCodeKotlin
- [LeetCode] 248. Strobogrammatic Number IIILeetCode
- [LeetCode] 191. Number of 1 BitsLeetCode
- [LeetCode] 305. Number of Islands IILeetCode
- Leetcode 17 Letter Combinations of a Phone NumberLeetCode
- Leetcode 137. Single Number IILeetCode
- 【Leetcode】1395. Count Number of TeamsLeetCode
- Leetcode – 017. Letter Combinations of a Phone NumberLeetCode
- [LeetCode] 3238. Find the Number of Winning PlayersLeetCode
- [LeetCode] 2684. Maximum Number of Moves in a GridLeetCode
- Leetcode 202 Happy Number Javascript 解決方案LeetCodeAPPJavaScript
- [LeetCode] 2406. Divide Intervals Into Minimum Number of GroupsLeetCodeIDE
- LeetCode Letter Combinations of a Phone Number(017)解法總結LeetCode
- Leetcode 之 PHP 解析 (260. Single Number III)LeetCodePHP
- LeetCode演算法題-Number of Boomerangs(Java實現)LeetCode演算法OOMJava
- LeetCode65. Valid Number — 判斷合法數字LeetCode
- [LeetCode] 1953. Maximum Number of Weeks for Which You Can WorkLeetCode
- [LeetCode] 3226. Number of Bit Changes to Make Two Integers EqualLeetCode
- LeetCode 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance??LeetCode
- LeetCode 452. Minimum Number of Arrows to Burst Balloons Sort/MediumLeetCode
- [LeetCode] 3239. Minimum Number of Flips to Make Binary Grid Palindromic ILeetCode
- Leetcode 1365. How Many Numbers Are Smaller Than the Current Number (cpp)LeetCode