leetcode — palindrome-number
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的更多相关文章
- [LeetCode] Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- [Leetcode]Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 这题貌似解法挺多,直接用简单的把数倒置,没有考虑数 ...
- leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- LeetCode——Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- [Leetcode] Palindrome number 判断回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- leetcode Palindrome Number python
class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool &quo ...
- leetcode:Palindrome Number【Python版】
一次AC 题目要求中有空间限制,因此没有采用字符串由量变向中间逐个对比的方法,而是采用计算翻转之后的数字与x是否相等的方法: class Solution: # @return a boolean d ...
- [LeetCode]Palindrome Number 推断二进制和十进制是否为回文
class Solution { public: bool isPalindrome2(int x) {//二进制 int num=1,len=1,t=x>>1; while(t){ nu ...
- [leetcode] Palindrome Number(不使用额外空间)
本来推断回文串是一件非常easy的事情,仅仅须要反转字符串后在与原字符串相比較就可以.这道题目明白说明不能使用额外的空间.那么使用将其分解连接成字符串的方法便不是可行的.仅仅好採用数学的方式: 每次取 ...
- Palindrome Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...
随机推荐
- Python3实战系列之六(获取印度售后数据项目)
问题:续接上一篇.说干咱就干呀,勤勤恳恳写程序呀! 目标:此篇我们试着把python程序打包成.exe程序.这样就可以在服务器上运行了.实现首篇计划列表功能模块的第三步: 3..exe文件能在服务器上 ...
- 【转】linux 查看进程启动路径
在linux下查看进程大家都会想到用 ps -ef|grep XXX可是看到的不是全路径,怎么看全路径呢?每个进程启动之后在 /proc下面有一个于pid对应的路径例如:ps -ef|grep pyt ...
- OKR 与 KPI
作者:Cat Chen链接:https://www.zhihu.com/question/22478049/answer/23833548来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...
- ABP框架系列之一:(Entity-实体)
Entities are one of the core concepts of DDD (Domain Driven Design). Eric Evans describe it as " ...
- 1119 Pre- and Post-order Traversals
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...
- JSP的分页技术
在实际应用中,如果从数据库中查询的记录特别的多,甚至超过了显示屏的显示范围,这个时候可将结果进行分页显示. 假设总记录数为intRowCount,每页显示的数量为inPageSize,总页数为intP ...
- CPP之内存分配
new & delete expression 1. Introduction A new expression allocates and constructs an object of a ...
- 【接口时序】4、SPI总线的原理与Verilog实现
一. 软件平台与硬件平台 软件平台: 1.操作系统:Windows-8.1 2.开发套件:ISE14.7 3.仿真工具:ModelSim-10.4-SE 硬件平台: 1. FPGA型号:Xilinx公 ...
- CVE-2015-1641 Office类型混淆漏洞及shellcode分析
作者:枕边月亮 原文来自:CVE-2015-1641 Office类型混淆漏洞及shellcode分析 0x1实验环境:Win7_32位,Office2007 0x2工具:Windbg,OD,火绒剑, ...
- Eclipse 中 SVN 插件的安装与使用
下载和安装SVN插件 插件在线安装 可以选择在线安装插件的方式,就是使用eclipse里Help菜单的“Install New Software”,通过输入SVN地址,直接下载安装到eclipse里. ...