Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)
Github:https://github.com/illuz/leetcode
009.palindrome_number (Easy)
links:
Title: https://oj.leetcode.com/problems/palindrome-number/
Code (GitHub): Https://github.com/illuz/leetcode
Test Instructions:
Determines whether a number is a palindrome number.
Analysis:
Just write as you want.
- You can first convert to a string and then judge. (This solution can be done with Python in one sentence =w=)
- A better method is to directly calculate the number of palindrome and then direct comparison.
Code:
C + +:(can be converted to a string first)
Class Solution {public: bool Ispalindrome (int x) {if (x < 0) return false;int bit[10];int cnt = 0;while (x) {BIT[CN t++] = x% 10;x/= 10;} for (int i = 0; i < cnt; i++) if (bit[i]! = bit[cnt-i-1]) return False;return true; }};
Python:
Class solution: # @return A Boolean def ispalindrome (self, x): return str (x) = = str (x) [::-1]
C + +:(calculate palindrome)
Class Solution {public: bool Ispalindrome (int x) {long Long xx = X;long long new_xx = 0;while (xx > 0) {new_xx = NE W_XX * + xx% 10;xx/= 10;} return new_xx = = (long long) x; }};
Java:
public class Solution {public boolean ispalindrome (int x) { long xx = x; Long new_xx = 0; while (xx > 0) { new_xx = new_xx * + xx%; XX/= ten; } return new_xx = = x; }}
Python:
Class solution: # @return A Boolean def ispalindrome (self, x): xx = x new_xx = 0 while xx > 0: New_xx = new_xx * + xx% xx/= return new_xx = = X
[Leetcode] 009. Palindrome number (Easy) (C++/java/python)