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. PHP pthread 多线程 案例

    <?php /** *检测http服务是否可以正常访问 *启动三个线程执行任务 */ class taskWork extends Thread { public $url = ''; //检测 ...

  2. Node.js 中使用 ES6 中的 import / export 的方法大全

    https://blog.csdn.net/universsky2015/article/details/83754741

  3. java并发编程艺术

    cas算法 概要 刚开始看这本书的时候很经常看到cas算法,个人觉得cas算法在并发编程中也是挺重要的的一部分,cas是比较并交换的意思(compare and swap),campareAndSwa ...

  4. 最近素数问题——C语言

    从键盘输入一个整数,输出距离该数最近的素数 #include<stdio.h> #include<math.h> int judge(int x) { //判断素数 if (x ...

  5. ios 在使用 SDWebimage UIButton setBackgroundimage

    解决方法 : 有法为此在 SDWebImage: SDWebImage / SDWebImage / UIButton+WebCache.h 导入此文件在您的类: #import <SDWebI ...

  6. ping内网一台虚拟机延时很大(hyper-v虚拟机)的解决办法

    问题现象: ping 内网一台虚拟机延时很大,不稳定,造成业务系统响应慢.查看服务器上各种资源都正常. 解决办法: 在物理机上找到和hyper-v绑定的那个网卡,把“虚拟机队列”禁用掉就好了,如下图: ...

  7. Java之IO流总结

    IO流·Java流式输入/输出原理·Java流类的分类·输入/输出流类·常见的节点流和处理流·文件流·缓冲流·转换流·数据流·Print流·Object流 ①Java流式输入/输出原理         ...

  8. ABP 异常处理 第四篇

    1.ABP异常处理机制是通过过滤器实现的,我们查看的webAPI的异常处理,我们来看看他的源码,AbpApiExceptionFilterAttribute 继承ExceptionFilterAttr ...

  9. ActiveMQ_3Java实现

    Java实现 添加相应的jar包 <dependency> <groupId>org.apache.activemq</groupId> <artifactI ...

  10. ReactNative 深拷贝

    1: 导入 import _ from 'lodash' 2: _.cloneDeep(obj)