[leetcode]7. Reverse Integer反转整数
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
题意:
给定一个10进制整数,翻转它。
Solution1: directly do the simulation.
Two tricky parts to be handled:
(1) overflow : 32-bit signed integer range: [−231, 231 − 1], whihc means [−2,147,483,648 2,147,483,647]. What if input is 2,147,483,647, after reversing, it will be 7,463,847,412.
(2) negative numbers: for each iteration, we do multiplication or division with a position number -- 10 , which means if sign is '-' , the sign will be kept all the time.
code:
/*
Time Complexity: O(log(n)) coz we just travese half part of original input
Space Complexity: O(1)
*/
class Solution {
public int reverse(int input) {
long sum = 0;
while(input !=0){
sum = sum*10 + input %10;
input = input /10; if(sum > Integer.MAX_VALUE || sum < Integer.MIN_VALUE){
return 0; // returns 0 when the reversed integer overflows
}
}
return (int)sum;
}
}
[leetcode]7. Reverse Integer反转整数的更多相关文章
- leetcode 7 reverse integer 反转整数
描述: 给定32位整数,反转,如321转成123. 解决: 关键是溢出检测: int reverse(int x) { ; int temp; while (x) { temp = ret * + x ...
- [Leetcode] reverse integer 反转整数
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- 7. Reverse Integer 反转整数
[抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: ...
- [LeetCode] 7. Reverse Integer 翻转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- LeetCode 7. Reverse Integer 一个整数倒叙输出
潜在问题:(1)随着求和可能精度会溢出int 范围,需要使用long 来辅助判断是否溢出,此时返回 0 Assume we are dealing with an environment which ...
- 【LeetCode题解】7_反转整数
目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- leetCode(62)-Reverse Integer
题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 clic ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
随机推荐
- Flutter 学习资料
Flutter 学习资料: 学习资料 网址 Flutter 中文网 https://flutterchina.club/ <Flutter实战>电子书 https://book.flutt ...
- laravel 路由分組
laravel 路由分組 Route::group(['prefix' => 'admin'], function () { $namespacePrefix="\\App\\Http ...
- ElasticSearch的lowlevelApi和低级别API
之前开发使用的其实都是lowLevel的api,所谓lowlevelapi就是操作ES的json字符串要自己去写:所谓highlevel的api就是指将查询的json字符串给对象化,创建一个Searc ...
- Linux which命令详解
Linux which命令 Linux which命令用于查找文件. which指令会在环境变量$PATH设置的目录里查找符合条件的文件 用法: which [options] [--] COMMAN ...
- map/reduce/filter/lambda
Python内建了map()/reduce()/filter()函数. map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的It ...
- Azure SQL 数据库仓库Data Warehouse (4) 2018 TechSummit 动手实验营
<Windows Azure Platform 系列文章目录> 上传一下之前在2018 TechSummit的动手实验营:Azure数据仓库PaaS项目架构规划与实战入门 包含PPT和Wo ...
- HTTP Protocol - URI
Uniform Resource Identifier (URI): compact sequence of characters that identifies an abstract or phy ...
- 注解:大话AOP与Android的爱恨情仇
转载:大话AOP与Android的爱恨情仇 1. AOP与OOP的区别 平时我接触多的就是OOP(Object Oriented Programming面向对象).AOP(Aspect Oriente ...
- WOW
WOW http://bbs.ngacn.cc/read.php?tid=4992959 http://ngasave.us/popcn/?t=gems 地精科技:国服最流行 http://bbs ...
- git 修改客户端用户名和密码
1.修改某个具体项目中的用户名密码 vim xx/{yourProject dir }/.git/.git-credentials 在.git-credentials文件中配置用户名密码 https: ...