题目链接:https://leetcode.com/problems/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.

解题思路:
有两种方式可以解决。
首先想到的是转换为字符串,然后从字符串两端向中间比较。
看了别人的答案才发现这是一道数学题。即,回文数字逆序后还是其本身。

注意:
负数不是回文, 0 是回文。

字符串方法:

public class Solution {public boolean isPalindrome(int x) {if(x < 0)return false;if(x / 10 == 0)return true;String num = Integer.toString(x);for(int i = 0; i <= num.length() / 2; i ++) {if(num.charAt(i) != num.charAt(num.length() - 1 - i))return false;}return true;}
}
11506 / 11506 test cases passed.
Status: Accepted
Runtime: 532 ms

整数转置方法:

public class Solution {public boolean isPalindrome(int x) {if(x < 0)return false;if(x / 10 == 0)return true;int num = x;int temp = 0;while(num != 0) {temp = temp * 10 + num % 10;num = num / 10;}if(temp == x)return true;elsereturn false;}
}
11506 / 11506 test cases passed.
Status: Accepted
Runtime: 532 ms

9 Palindrome Number相关推荐

  1. 北林oj-算法设计与分析-Tom palindrome number

    描述 Tom is studing math these days. If there is a number X, whose binary form and decimal form are al ...

  2. Bailian4067 回文数字(Palindrome Number)【数学】

    4067:回文数字(Palindrome Number) 总时间限制: 1000ms 内存限制: 65536kB 描述 给出一系列非负整数,判断是否是一个回文数.回文数指的是正着写和倒着写相等的数. ...

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

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

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

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

  5. hdu 5062 Beautiful Palindrome Number(水题)

    题目链接:hdu 5062 Beautiful Palindrome Number 题目大意:略. 解题思路:暴力或者手算都可以,注意手算的话,分别算出1,2,3...位的情况后,答案是累加上去的. ...

  6. js 数组倒序_我用JS刷LeetCode | Day 6 | Palindrome Number

    来公众号「九零后重庆崽儿」,我们一起学前端 回文数: 说明:现阶段的解题暂未考虑复杂度问题 首发地址: 我用JS刷LeetCode | Day 6 | Palindrome Number​www.br ...

  7. LeetCode小白菜笔记[3]:Palindrome Number

    LeetCode小白菜笔记[3]:Palindrome Number 9. Palindrome Number [Easy] 题目:Determine whether an integer is a ...

  8. 9. Palindrome Number*

    9. Palindrome Number* https://leetcode.com/problems/palindrome-number/description/ 题目描述 Determine wh ...

  9. 9—— Palindrome Number

    9. Palindrome Number 回文数 判断一个正整数是不是回文数. 回文数的定义是,将这个数反转之后,得到的数仍然是同一个数. 注意事项 给的数一定保证是32位正整数,但是反转之后的数就未 ...

  10. 9. Palindrome Number

    题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...

最新文章

  1. vue-router的基本使用
  2. LI标签在Ul中居中显示
  3. c语言 集中上机题目,C语言集中上机题目.doc
  4. Android QQ登录 程序奔溃的问题
  5. [html] HTML5的Geolocation不允许定位后如何于次让它弹起授权定位?
  6. linux关机命令_Linux基于centOS 7.6常见的Linux命令
  7. JS逆向笔记-记录某测试论坛的js逆向练习
  8. [蛋蛋无厘头日记]约会ing~~
  9. 点阵字体显示系列之三:使用ncurses显示汉字
  10. 怎么修改win11睡眠时间
  11. VS2008 SP1安装失败
  12. python 判断三角矩阵 pta
  13. Keil MDK5 STM32F401CCU6开发环境配置
  14. 公有云、私有云、混合云
  15. ES 搜索19 (match 查询时权重提升)
  16. 什么是上变频和下变频
  17. IPAD DHCP
  18. 最新的JavaScript知识总结,欢迎各位大佬指正,需要的留下邮箱,给你们发原稿(PDF版)...
  19. mysql 加序号以及成绩单排名的几种实现方式
  20. 胶囊网络可以PK掉CNN吗?//——暂时不能 胶囊网络的不足点思考

热门文章

  1. 杭州电子科技大学c语言题目,2007-2008杭州电子科技大学学生C语言程序设计考试卷(2份)...
  2. Excel 2010 SQL应用030 查询关键字
  3. 金蝶K3物料属性修改转换问题
  4. 灾难恢复全攻略:从定级、规划到实施
  5. 11.29直播预告 | 论文分享—Storage and Processing of Large Evolving Graphs
  6. Windows 安装 Podman Desktop
  7. 使用java实现注册登录信息验证
  8. JavaScript闭包函数详解
  9. shell脚本的调试
  10. java中contains_java中的contains()方法