原题地址:https://oj.leetcode.com/problems/palindrome-number/

题意:

Determine whether an integer is a palindrome. Do this without extra space.

click to show 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:# @return a booleandef isPalindrome(self, x):if x < 0:return Falsediv = 1while x/div >= 10:div = div * 10while x:left = x / divright = x % 10if left != right:return Falsex = ( x % div ) / 10div = div / 100return True

[leetcode]Palindrome Number @ Python相关推荐

  1. [leetcode] Palindrome Number

    2019独角兽企业重金招聘Python工程师标准>>> Determine whether an integer is a palindrome. Do this without e ...

  2. leetcode Single Number python

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

  3. [LeetCode] Palindrome Number Valid Palindrome - 回文系列问题

    题目概述: Determine whether an integer is a palindrome. Do this without extra space. 题目分析: 判断数字是否是回文 例如1 ...

  4. Leetcode:Palindrome Number

    题目大意是提供一个32位整数,判断这个数的十进制字符串形式是否是回文.所谓的回文就是字符串的逆序形式与正序形式完全一致.比如123不等于321不符合条件,121等于121符合条件,当然-1不等于1-也 ...

  5. [leetcode] Palindrome Number 回文数判断

    Determine whether an integer is a palindrome. Do this without extra space. 题目大意:略 bool isPalindrome( ...

  6. LeetCode - Palindrome Number

    用Java刷这道题没有太大意义. public class Solution {public boolean isPalindrome(int x) {boolean ans = false;Stri ...

  7. LeetCode Palindrome Number 五秒一题,击败82%用户

    解题思路 要验证一个数字是否是对称的,只需要将它的信息保存到一个可以实现翻转的数据类型就好了 步骤 将数据类型转换成str 验证生成的str是否对称 原题链接 欢迎在我的博客探索更多思路 代码 cla ...

  8. 【整数转字符串】LeetCode 9. Palindrome Number

    LeetCode 9. Palindrome Number Solution1: 不利用字符串 class Solution { public:bool isPalindrome(int x) {if ...

  9. 【回文串2】LeetCode 9. Palindrome Number

    LeetCode 9. Palindrome Number Solution1:我的答案 思路一:转化为字符串 class Solution { public:bool isPalindrome(in ...

最新文章

  1. 指针运算(自己做个笔记)
  2. 处理错误:ORA-27101: shared memory realm does not exist 解决方案
  3. 地平线黄畅:软件2.0时代,数据驱动进化,算力将成为智能化的基石丨MEET2021...
  4. OpenCV创建自己的corner检测器
  5. 小公司出来的产品经理被大厂鄙视?
  6. ireport怎么套打_DNF小小王国历险记巨人击退战怎么打 巨人击退战通关技巧攻略...
  7. 调研3家学校,分析10万数据,发现有了大数据再也不用“清考”
  8. 【SpringBoot】SpringBoot最精简的设置
  9. html target=_blank 弹出独立窗口,HTML base 标签的 target 属性 —— base target=_blank /...
  10. DJ设备市场现状及未来发展趋势
  11. 深入浅出WPF学习笔记
  12. 启发式搜索算法最短路径搜索算法概述
  13. Java 多线程学习(1)一些容易被遗忘的基础概念
  14. Displayport 连接、握手时序
  15. Windows10 U盘无法格式化怎么办?U盘拒绝访问?
  16. 软件开发平台之争:NET VS Java,谁是更好的选择?
  17. Docusaurus 搭建个人博客(支持离线搜索)
  18. Stereo Matching文献笔记之(六):浅谈置信度传播算法(Belief-Propagation)在立体匹配中的应用~
  19. [35期] 神奇的rgb
  20. 水果怎么画?橘子怎么画?橙子怎么画?

热门文章

  1. mysql 最近_关于mysql查询最近一条记录
  2. 两个时间的差值Java,Java如何找到两个时间之间的差值?
  3. python wordpress建站_WordPress快速建站
  4. word如何一键全选_【众点学】学了这些Word技巧才知道,原来这么多年的班都白加了...
  5. linux任务队列设计,linux 内核任务队列.doc
  6. java commons logging_Java日志介绍(5)-commons-logging
  7. 为什么jdk中把String类设计成final
  8. 《天天数学》连载32:二月一日
  9. 使用jedis访问redis
  10. vex夹球机器人_汕头市4名中学生获2020年VEX机器人世锦赛初中组亚军