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. 2-django配置

    一.settings.py配置 1.时区配置 现在看到的界面是英文的,将 LANGUAGE_CODE = 'en-us' 改为 LANGUAGE_CODE = 'zh-Hans '就可以看到如下界面 ...

  2. 小白学习django第三站-自定义过滤器及标签

    要使用自定义过滤器和标签,首先要设置好目录结构 现在项目目录下建立common的python包 再将common加入到setting.py中的INSTALLED_APP列表中 在common创建目录t ...

  3. Voting CodeForces - 749C (set,模拟)

    大意: n个人, 两个党派, 轮流投票, 两种操作(1)ban掉一个人 (2)投票, 每轮一个未被ban的人可以进行一次操作(1)或操作(2), 求最终哪个党派得票最多. 显然先ban人会更优, 所以 ...

  4. Scala Partial Function从官方文档解析

    A partial function of type PartialFunction[A, B] is a unary function where the domain does not neces ...

  5. python日志汇总

    1. 文件头在Linux下推荐文件头: #! /usr/bin/env python # -*- coding: utf-8 -*- 2.import导入语句 import: 一般导入第三包可以直接使 ...

  6. Nagios监控系统部署(源码)(四)

    Nagios监控系统部署(源码)   1. 概述2. 部署Nagios2.1 创建Nagios用户组2.2 下载Nagios和Nagios-plugin源码2.3 编译安装3. 部署Nagios-pl ...

  7. x64工程属性 选择切换不了

    删除x64工程属性 重新建立新的 配置属性 和导入工程类似  属性冲突 最好重新建立新的工程 解决

  8. 【POJ2486】Apple Tree

    题目大意:给定一棵 N 个节点的有根树,点有点权,边权均为1.现允许从根节点出发走 K 步,求可以经过的点权之和最大是多少. 题解:可以将点权看作是价值,将可以走的步数看作是重量,则转化成了一个树上背 ...

  9. hive中对子查询如in,exists等支持

    案例情况:同事使用公司数据探查跑一段代码,部分代码如下,报错,显示不支持in内的子查询.但是直接用虚拟机去跑的话代码没有任何报错,也出结果,很奇怪. SELECT t1.SIGN_CODE AS bu ...

  10. 【NOIP2016提高A组模拟9.15】Map

    题目 分析 发现,当原图是一棵树的时候,那么新建一条边后,就会变成环套树, 而环内的所有点对都是安全点对,如果环中有k个点,答案就是\(k(k-1)\) 联想到,当把原图做一遍tarjan缩点,每个环 ...