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.

题目:判断一个整数是否是回文的

1、负数可以是回文的吗 负数不是回文整数
2、如果你想把整形转换成字符串来处理,注意空间限制
3、你也可以反转整数,但是可能出现溢出的情况

Test  cases:

-2147483648
=>false
-1
=>false
212
=>true
3
=>true

 public class Solution{
public boolean isPalindrome(int x) {
if(x<0)
return false;
if(x>=0 && x<=9)
return true;
int len = (x + "").length();
int[] arr = new int[len];
int index = 0;
while(x>0){
arr[index++] = x%10;
x = x/10;
}
System.out.println(Arrays.toString(arr));
int middle = (len+1)/2;
boolean flag = true;
for(index=0; index<middle; index++){
if(arr[index] != arr[len-1-index]){
flag = false;
}
}
return flag;
} public static void main(String[] args){
int param = 5486;
if(args.length!=0)
param = Integer.valueOf(args[0]);
Solution solution = new Solution();
boolean res = solution.isPalindrome(param);
System.out.println(res);
}
}

[LeetCode]-009-Palindrome_Number的更多相关文章

  1. 【JAVA、C++】LeetCode 009 Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. 解题思路一: 双指针法,逐位判断 Java代码如下 ...

  2. Palindrome Number ---- LeetCode 009

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

  3. [Leetcode]009.Palindrome Number

    public class Solution { public boolean isPalindrome(int x) { if (x<0 || (x!=0 && x%10==0) ...

  4. leetcode python 009

    ##懒得自己做 ##  验证回文数字int0=63435435print(int(str(int0)[::-1])==int)

  5. 【LeetCode】009. Palindrome Number

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

  6. [目录][Leetcode] Leetcode 题解索引

    之前想边做题边写结题报告,发现有点力不从心,而且很多地方也是一知半解,现在重新做题,重新理解.这篇文章主要起一个目录的作用. 目前没有什么特定的顺序,基本都是看心情翻牌的,哈哈~ 我在Github上新 ...

  7. Leetcode 题解

    Leetcode Solutions Language: javascript c mysql Last updated: 2019-01-04 https://github.com/nusr/lee ...

  8. 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...

  9. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  10. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

随机推荐

  1. Android快捷键大全

    参考来源:https://mp.weixin.qq.com/s/T809p17Wt8XHkbLwcQf9ow 1,Ctrl + J  快捷代码列表 2,Ctrl+Alt+O 这个快捷键可以自动导包或删 ...

  2. ElasticSearch5.3安装IK分词器并验证

    ElasticSearch5.3安装IK分词器 之前使用Elasticsearch安装head插件成功了,但是安装IK分词器却失败了.貌似是ElasticSearch5.0以后就不支持直接在elast ...

  3. Java学习:通过Scanner读取文件

    Scanner不仅能够读取用户的键盘输入,还可以读取文件输入. 需要在创建Scanner对象的时候传入一个File对象作为参数.代码如下: import java.util.Scanner; impo ...

  4. RateLimiter 源码分析(Guava 和 Sentinel 实现)

    作者javadoop,资深Java工程师.本文已获作者授权发布. 原文链接https://www.javadoop.com/post/rate-limiter 本文主要介绍关于流控的两部分内容. 第一 ...

  5. js中过滤在输入框中过滤掉特殊表情

    在页面输入text 时,经常会出现某些特殊符号例如:❤

  6. git 本地tag和远程tag对应不上 vscode里pull不下代码

    vscode拉取代码是用  git pull --tags origin saas-xxx > git pull --tags origin saas-base From 172.16.0.xx ...

  7. 深入理解Java的反射机制

    https://blog.csdn.net/u012585964/article/details/52011138 http://www.importnew.com/20339.html 一,java ...

  8. Verilog中的Timescale作用

    很多时候,我们拿到已有的东西理所当然的用了,其实,你真的对你所使用的东西了解吗? 再次犯下这样的错误,是因为在把代码从Altera 的CycloneV移植到Xilinx的Spartan6上,我遇到了非 ...

  9. centos7下通过LVS的DR模式实现负载均衡访问

    一.两台服务器作为real server ,一台作为director director:172.28.18.69 vip:172.28.18.70 real server1:172.28.18.71 ...

  10. Windows如何下载nginx软件包到linux系统虚拟机上

    1.打开浏览器,输入“nginx下载官网” 2.点击nginx:download 3.找到你想下载的nginx软件包 4.点击你所需要的版本之后(我点击的是nginx-1.12.2版本,根据自己的需要 ...