Palindrome Number & Reverse Integer
Determine whether an integer is a palindrome. Do this without extra space.
分析:把一个数倒过来,然后看两个数是否相同。
public class Solution {
public boolean isPalindrome(int x) {
if (x < ) {
return false;
}
long palindromeX = , inputX = x;
while (x > ) {
palindromeX = palindromeX * + (x % );
x = x / ;
}
return palindromeX == inputX;
}
}
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
public class Solution {
public int reverse(int x) {
boolean isPositive = true; if (x < ) {
x = -x;
isPositive = false;
} long reversedValue = ;
while (x != ) {
reversedValue = reversedValue * + x % ;
x /= ;
}
if (Math.abs(reversedValue) > Integer.MAX_VALUE) return ; return isPositive ? (int)reversedValue : (int)(-reversedValue);
}
}
Palindrome Number & Reverse Integer的更多相关文章
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- Reverse Integer - Palindrome Number - 简单模拟
第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
- Leetcode 题目整理-3 Palindrome Number & Roman to Integer
9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. clic ...
- 9. Palindrome Number
/* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...
- No.009 Palindrome Number
9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
随机推荐
- golang 热升级
需求场景 干净利落地升级正在运行的agent程序.适用于Devops团队. 目标: 不关闭现有连接:例如我们不希望关掉已部署的运行中的程序.但又想不受限制地随时升级服务. 新的进程要能够启动并替换掉旧 ...
- SQL语法基础之高级应用
SQL语法基础之高级应用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.存储过程与函数 1>.CREATE PROCEDURE 用来创建存储过程 mysql> ? ...
- 网络编程基础【day09】:通过socket实现简单ssh客户端(三)
本节内容 1.概述 2.socket发送中文 3.重复发送和多次接收 4.模拟ssh客户端 一.概述 本篇博客讲一下,如果socket客户端断了,另外的客户端怎么接入服务端,还有模拟ssh的链接等. ...
- try{s.send(t.hasContent&&t.data||null)}catch(e){if(n)throw e}}引发的惨案
如题,ajax请求报错:try{s.send(t.hasContent&&t.data||null)}catch(e){if(n)throw e}}引发的惨案 要么是404,要么是40 ...
- 阅读:ECMAScript 6 入门(4)
参考 ECMAScript 6 入门 ES6新特性概览 ES6 全套教程 ECMAScript6 (原著:阮一峰) JavaScript 教程 重新介绍 JavaScript(JS 教程) 数组的扩展 ...
- [转]Red Hat Linux相关产品iso镜像下载【百度云】
超强汇总!献上大佬链接:http://www.linuxfly.org/post/659/ 还有一些可用链接: 下面的直接复制到迅雷下载,链接是打不开的. RHEL 5.4 ISO下载http://r ...
- mvn打包时添加日期参数
maven打包时想添加日期参数,如:将"xxx.jar"打包为"xxx-yyyyMMdd.jar"这样的格式.如何实现? 自Maven 2.1.0-M1版本之后 ...
- SpringBoot系列: 使用MyBatis maven插件自动生成java代码
====================================pom.xml 文件====================================需要在 pom.xml 文件增加 m ...
- JDK8新特性01 Lambda表达式01_设计的由来
1.java bean public class Employee { private int id; private String name; private int age; private do ...
- CTF 湖湘杯 2018 WriteUp (部分)
湖湘杯 2018 WriteUp (部分),欢迎转载,转载请注明出处! 1. CodeCheck(WEB) 测试admin ‘ or ‘1’=’1’# ,php报错.点击登录框下面的滚动通知,URL ...