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的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...
随机推荐
- sql-多表查询
一:外连接 1.左外连接(left join) select * from A left join B on A.id=B.a_id 结果如下 很明显,A表的所有数据都显示出来了 ...
- 设置DataGridView中表头颜色
默认的DataGridView表头颜色实在不是太好看,想设置下,上google搜了一通, 都说这样设置 this.dataGridView1.ColumnHeadersDefaultCellStyle ...
- Java的 volatile关键字的底层实现原理
我们知道volatile关键字的作用是保证变量在多线程之间的可见性,它是java.util.concurrent包的核心,没有volatile就没有这么多的并发类给我们使用.本文详细解读一下volat ...
- STM32 + RC522(SPI2 和 模拟SPI)
STM32 + RC522(SPI2 和 模拟SPI) 一. STM32 + RC522(SPI2 模式) 1. 头文件: rc522.h #include "stm32f10x.h&quo ...
- ABP框架系列之二:(Entity Framework Core-实体核心框架)
Introduction(介绍) Abp.EntityFrameworkCore nuget package is used to integrate to Entity Framework (EF) ...
- 无网情况下linux安装django
创建虚拟环境~/project/hanqin/django> virtualenv monitor2~/project/hanqin/django/monitor2> cd bin~/pr ...
- php,mysql存储过程的简单例子
一.连接mysql 使用phpmyadmin: 打开test数据库: 二.创建存储过程 参数如图. 三.php中调用存储过程 <?php $user = "root"; // ...
- Note | 学术论文写作方法和技巧
目录 1. 论文发表流程 2. 确定科研方向 3. 思考问题和解决问题 4. 审稿 5. 论文写作 5.1. 标题 5.2. 摘要 5.3.介绍 5.4. 相关工作 5.5. 段落 5.6. 方法 5 ...
- 【repost】H5的新特性及部分API详解
h5新特性总览 移除的元素 纯表现的元素: basefont.big.center.font等 对可用性产生负面影响的元素: frame.frameset.noframes 新增的API 语义: 能够 ...
- channel和Stream的对比
这篇文章主要想总结下NIO的channel的传统io中的stream的差别在哪.网上找了很多文章,都感觉只是说了概念.然后自己大概看了下源码,结合概念,整理一下.有些地方可能不是很准确,也希望可以给点 ...