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. LOJ 103 字串查找 题解

    题面 这道题是KMP的模板. KMP需要注意的细节有很多,所以把这篇文章发上来供参考: #include <bits/stdc++.h> using namespace std; char ...

  2. 打印输出opencv的版本信息

    本文链接: https://mangoroom.cn/opencv/print-opencv-version-info.html 序 查看自己安装的opencv的版本信息的方法有两种. 方法一-查看l ...

  3. hibernate update-->参数绑定

    Hibernate 更新数据库 参数绑定总结: 一.query.setParameter(属性名,真实值,类型); String hql="update User u set u.userN ...

  4. Django 前端通过json 取出后端数据

    Django 前端通过json 取出后端数据 前端通过json 取出后端数据 步骤1:后台数据通过 JSON 序列化成字符串a 注意:1.json是1个字符串 ​ 2.通过json.dumps('xx ...

  5. WebSocket的使用(基于VUE与SpringBoot)

    WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议.WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据.在 We ...

  6. 【Linux】C字节对齐

    原文地址:https://www.jianshu.com/p/e8fcc01041a7 什么是对齐,以及为什么要对齐: 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问 ...

  7. vim学习(一)之简介、安装、配置

    vim简介 Vim是从 vi 发展出来的一个文本编辑器,是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特性在里面. 简单的来说, vi 是老式的文字处理器,不过功能已经很齐全了,但是还是 ...

  8. PHPStorm 远程开发教程

    目的:实现在windows下开发,而所改变代码自动同步到虚拟机 查看虚拟机的 IP地址 配置代码自动同步信息 通过页面上部的选项卡,切换到 Mappings 根路径:指的都是项目代码的根路径 点击一次 ...

  9. SIP协议 会话发起协议(一)

    会话发起协议(SIP)是VoIP技术中最常用的协议之一.它是一种应用层协议,与其他应用层协议协同工作,通过Internet控制多媒体通信会话. SIP - 概述 以下是有关SIP的几点注意事项 - S ...

  10. nginx的服务架构

    nginx服务架构 模块 习惯上将nginx的模块分成核心模块,HTTP模块,邮件模块,以及第三方模块 核心模块主要包含两类功能的支持,一类是主体功能,包括进程管理,权限管理错误日志解析,配置解析:另 ...