题目简述:

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

解题思路:

首先最简单的就是想到直接转成字符串最前一个和最后一个比较,而且做出来效果还不错。不过题目说可以直接操作数字,那么我们也可以想办法每次取出数字的最高位和最低位进行比较,办法就是用一个base取最高位,每次取后base/100

class Solution:
# @return a boolean
def isPalindrome(self, x):
if x < 0:
return False
else:
s = str(x)
l = len(s)
for i in range(l/2):
if s[i] != s[l-i-1]:
return False
return True class Solution:
# @return a boolean
def isPalindrome(self, x):
if x < 0:
return False
base = 1
while x / base >= 10:
base *= 10
while x > 0:
left = x / base
right = x % 10
if left != right:
return False
x -= base * left
x /= 10
base /= 100
return True

【leetcode】Palindrome Number的更多相关文章

  1. 【leetcode】Palindrome Number (easy)

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  2. 【LeetCode】Palindrome Number(回文数)

    这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...

  3. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  4. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  5. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  6. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  7. 【LeetCode】Palindrome Partitioning 解题报告

    [题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  8. 【Leetcode】【Easy】Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. 判断一个整数是不是回文整数(例12321).不能使 ...

  9. 【leetcode】Palindrome Partitioning II(hard) ☆

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

随机推荐

  1. js

    1.判断是否为空 if (typeof(a)!="undefined") 2. pop()移除数组最后一个元素 var arr = [];   $('#tableid tr').f ...

  2. jquery1.7.2的源码分析(二)

    jquery.extend jQuery.extend = jQuery.fn.extend = function () { var options, name, src, copy, copyIsA ...

  3. GCD总结

    //用block只有两种:同步执行/异步执行(参数1:队列;参数二:任务) dispatch_async(dispatch_get_global_queue(0, 0),^{ });//异步在新的线程 ...

  4. async await

    参考:https://blogs.msdn.microsoft.com/windowsappdev_cn/2012/04/30/winrt-await/

  5. 总是多次出现 那个同样的 权限错误 _storage_write_error_, 所以一开始就把机器设好setenforce 0

    把根目录下的所有文件/目录, 都设为权限777. 好像没有必要把所有的东西, 都设为777, 实际上问题是由selinux引起的, 所以, 只要把相应的目录, 不一定是所有的目录, 只是要写入内容的目 ...

  6. VBA笔记(三)——常用对象

    VBA实际上就是操作Excel,把Excel进行拆解,划分多层对象,由顶至下为(也可以说是层层包裹): Application:代表Excel程序本性,之后我们操作对象都在它之下,因为是唯一且至高点, ...

  7. Android之弹出/隐藏系统软键盘

    Android弹出/隐藏系统软键盘的代码如下: InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT ...

  8. C#设置输入框只输入数字

    为输入框添加keyPress事件,然后添加代码: || e.KeyChar > ) && e.KeyChar != && e.KeyChar != &&a ...

  9. 函数floor(n)

    Matlab函数floor(n): 仅取整数部分,剔除小数部分 例如: floor(9.2)=9; 仅此

  10. pwd命令

    [pwd]      打印当前的工作目录             pwd==print work director 命令格式: pwd [OPTION]... 命令功能: 打印当前工作目录的全路径 命 ...