【leetcode】Palindrome Number
题目简述:
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的更多相关文章
- 【leetcode】Palindrome Number (easy)
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- 【LeetCode】Palindrome Number(回文数)
这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【LeetCode】Palindrome Partitioning 解题报告
[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- 【Leetcode】【Easy】Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 判断一个整数是不是回文整数(例12321).不能使 ...
- 【leetcode】Palindrome Partitioning II(hard) ☆
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
随机推荐
- Linux 计划任务 访问网页
1.linux安装 [root@CentOS ~]# yum -y install vixie-cron[root@CentOS ~]# yum -y install crontabs 说明:vixi ...
- 2014年---移动端webapp个人年度总结
我今年是由零基础开始入门的,刚好我第一家公司入职后就马上让我接手做ipad版的专题app了.(一入门就是移动端开发,是幸运也是艰辛的开始). 我是自学前端的,当然,对Bootstrap,JQuery ...
- DNA解链统计物理
来源:Kerson Huang, Lectures on Statistical Physics and Protein Folding, pp 24-25 把双链DNA解开就像拉拉链.设DNA有\( ...
- ubuntu16.04(beaglebone) 下vim 和gcc 的信息
root@arm:~# dpkg -L gcc /. /usr /usr/bin /usr/bin/c99-gcc /usr/bin/c89-gcc /usr/share /usr/share/doc ...
- C#如何获取本机网络IP地址
在开发过程中我们经常会碰到需要IP地址,用来记录用户上次登录的时间地址,或者sokect网络编程等等,下面介绍两种方式: 1. public static string GetIP() { retur ...
- Oracle常用语法
Oracle常用语句语法汇总 Oracle10g 1 第一章Oracle命令 a) 系统管理员连接 conn */* as sysdba b) 查询当前用户 show user c) 创建新用户 cr ...
- javascript实用技巧、javascript高级技巧
字号+作者:H5之家 来源:H5之家 2016-10-31 11:00 我要评论( ) 三零网提供网络编程. JavaScript 的技术文章javascript实用技巧.javascript高级技巧 ...
- JavaScript学习笔记(1))——————call,apply方法
学习前端也有一段时间了,但是效果甚微.利用时间不够充分,虽然是利用工作之余来学习.但是这不能成为我的借口. 今天学习了(其实看了很多遍)call apply方法. function abc(a,b){ ...
- python 装饰器的理解
一. 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能本身无关的雷 ...
- Eclipse使用Maven tomcat:run命令启动web项目时修改默认端口