leetcode9_Palindrome Number
2016-09-12 20:47
351 查看
一.问题描述
Palindrome Number
Determine whether an integer is a palindrome. Do this
without extra space.
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 extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might
overflow. How would you handle such case?
There is a more generic way of solving this problem.
二.代码编写
class Solution(object):def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
#编程思想:将x反过来得到的int如果等于x,则x为回文的
rx=x
com_x=0
while rx>0 :
y=rx%10
com_x=com_x*10+y
rx/=10
if com_x==x:
return True
else:
return False
代码思想:依次取原整数的最后一位加到com_x上,加的时候原com_x要乘以10,由此可以做到将原整数x反序变成新的整数com_x,只要com_x==x,则x为回文的。
相关文章推荐
- [LeetCode] Remove Duplicates from Sorted Array II
- leetcode Merge Intervals
- [Leetcode] Populating Next Right Pointers in Each Node
- 【LeetCode】Unique Paths I && II && Minimum Path Sum
- LeetCode "Maximal Rectangle"
- Leetcode--Reverse Nodes in k-Group
- 【Leetcode】Largest Number
- [LeetCode 50] Pow(x, n)
- [LeetCode] Read N Characters Given Read4
- [leetcode] 209.Minimum Size Subarray Sum
- leetcode : Two Sum
- LeetCode——Minimum Path Sum
- <LeetCode OJ> 19. Remove Nth Node From End of List
- LeetCode第202题. Happy Number
- LeetCode -- Counting Bits
- Leetcode no. 257
- Leetcode 307. Range Sum Query - Mutable (Python)
- Leetcode五大常用算法
- LeetCode 209. Minimum Size Subarray Sum
- LeetCode解题报告 107. Binary Tree Level Order Traversal II [easy]