import org.lep.leetcode.parseint.IntegerParser;

/**
* Source : https://oj.leetcode.com/problems/palindrome-number/
*
* Created by lverpeng on 2017/7/5.
*
* 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.
*
*/
public class IntPalindrome { /**
* 负数可以自己决定是否进行判断,这里不进行判断,归为不是
* 依次取出整数的第一位和最后一位比较
* 第一位:整除
* 最后一位:对10求余
*
* @param num
* @return
*/
public boolean isPalindrome (int num) {
if (num < 0) {
return false;
}
int base = 1;
while (num / (base) >= 10) {
base *= 10;
} while (num != 0) {
int left = num / base;
int right = num % 10;
if (left != right) {
return false;
}
num = (num % base) / 10;
base /= 100;
}
return true;
} public static void main(String[] args) {
IntPalindrome palindrome = new IntPalindrome();
System.out.println(palindrome.isPalindrome(0));
System.out.println(palindrome.isPalindrome(-101));
System.out.println(palindrome.isPalindrome(1001));
System.out.println(palindrome.isPalindrome(1234321));
System.out.println(palindrome.isPalindrome(2147447412));
System.out.println(palindrome.isPalindrome(5155));
System.out.println(palindrome.isPalindrome(5552));
}
}

leetcode — palindrome-number的更多相关文章

  1. [LeetCode] Palindrome Number 验证回文数字

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  2. [Leetcode]Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. 这题貌似解法挺多,直接用简单的把数倒置,没有考虑数 ...

  3. leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】

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

  4. LeetCode——Palindrome Number

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

  5. [Leetcode] Palindrome number 判断回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  6. leetcode Palindrome Number python

    class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool &quo ...

  7. leetcode:Palindrome Number【Python版】

    一次AC 题目要求中有空间限制,因此没有采用字符串由量变向中间逐个对比的方法,而是采用计算翻转之后的数字与x是否相等的方法: class Solution: # @return a boolean d ...

  8. [LeetCode]Palindrome Number 推断二进制和十进制是否为回文

    class Solution { public: bool isPalindrome2(int x) {//二进制 int num=1,len=1,t=x>>1; while(t){ nu ...

  9. [leetcode] Palindrome Number(不使用额外空间)

    本来推断回文串是一件非常easy的事情,仅仅须要反转字符串后在与原字符串相比較就可以.这道题目明白说明不能使用额外的空间.那么使用将其分解连接成字符串的方法便不是可行的.仅仅好採用数学的方式: 每次取 ...

  10. Palindrome Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...

随机推荐

  1. 九校联考_24OI——餐馆restaurant

    凉心模拟D1T1--最简单的一道题 TAT 餐馆(restaurant) 题目背景 铜企鹅是企鹅餐馆的老板,他正在计划如何使得自己本年度收益增加. 题目描述 共有n 种食材,一份食材i 需要花ti 小 ...

  2. centos 网卡自动连接

    /etc/sysconfig/network-scripts/目录下ifcfg-eth0这个文件,把ONBOOT="no"改为yes,

  3. 转载-js如何设置网页横屏和竖屏切换

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. Unity 游戏运行越久加载越慢

    原因是某个GameObject 被调用多次DontDestroyOnLoad,表面上是调用多次没问题,实际上调用次数越多,加载速度越慢.

  5. linux_文件夹实现挂载(必须在同一网段)

    将外部想要挂载传输的目录开启共享文件夹 首先进行安装 yum install nfs-utils rpcbind yum install nfs* 建立想要挂载的目录 查看可以执行挂载的目录有哪些 s ...

  6. tomcat 配置 使用 HTTPS

    1.生成证书 keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keystore "d:\temp ...

  7. cp备份操作时如何忽略指定的目录

    需求场景:进行CP拷贝备份的时候,子目录里面的某些大文件或是一些log文件是无需备份的,那么在CP操作时需要忽略掉指定的目录. 案例演示如下:备份data目录,但是不包括里面的share子目录. 先看 ...

  8. C# 最齐全的上传图片方法

    方法里包括了图片大小限制.图片尺寸.文件内容等等的判断... 该案例是mvc下的demo,支持单张图片上传. public ActionResult Upload() { string imgurl ...

  9. RxSwift学习笔记10:startWith/merge/zip/combineLatest/withLatestFrom/switchLatest

    //startWith //该方法会在 Observable 序列开始之前插入一些事件元素.即发出事件消息之前,会先发出这些预先插入的事件消息 Observable.of(1,2,3) .startW ...

  10. 做JAVA开发的同学一定遇到过的爆表问题,看这里解决

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由净地发表于云+社区专栏 记一次Java线上服务器CPU过载问题的排查过程,详解排查过程中用到的Java性能监测工具:jvisualvm ...