Reverse Integer [LeetCode]
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
Solution: be careful about corner case, like INT_MIN, 0, and -INT_MIN is not equals to INT_MAX. Here is the definition of them in limits.h
#define INT_MIN (-2147483647 - 1) /* minimum (signed) int value */
#define INT_MAX 2147483647 /* maximum (signed) int value */
int reversePostiveInt(int x){
vector<int> digits;
while(true) {
if(x < ){
digits.push_back(x);
break;
}else {
int remainder = x % ;
int quotient = x / ;
digits.push_back(remainder);
x = quotient;
}
}
// reverse output
long long int ret = ;
for(int i = ; i < digits.size(); i ++) {
ret = ret * + digits[i];
if(ret > INT_MAX){
ret = INT_MAX;
break;
}
}
return (int) ret;
}
int reverse(int x) {
if(x == )
return ;
if(x > ) {
return reversePostiveInt(x);
}else if( x == INT_MIN){
//overflow
return INT_MIN;
}else if ( x > INT_MIN) {
return -reversePostiveInt(-x);
}
}
Reverse Integer [LeetCode]的更多相关文章
- Reverse Integer LeetCode Java
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 public cl ...
- Reverse Integer ---- LeetCode 007
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Solution ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- LeetCode 【2】 Reverse Integer --007
六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- LeetCode之Easy篇 ——(7)Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...
- LeetCode之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode 7 Reverse Integer & int
Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...
随机推荐
- 取消mod_sofia的呼叫鉴权
FreeSWITCH中默认的SIP呼叫是要鉴权的,流程如下. 终端 FreeSWITCH A -----Invite------> FS A <----Trying------ FS A ...
- XShell 安装与虚拟机连接
XShell:是liunx的远程管理工具 为啥要用这个工具呢?因为在古老的liunx字符命令下,是看不到中文的,要么使用liunx的图形化界面(支持中文),要么使用远程管理工具,是在windows中的 ...
- C# 导出数据至 CSV
有时候将Excel的数据另存到csv文件会出现csv格式错误,以下示例实现将DataTable里面的数据直接保存到csv文件. System.Web.HttpRuntime.Cache["v ...
- python_way day16 DOM
Python_way day16 1.Dom (找到html中的标签) 一.DOM 1.查找元素 直接查找 document.getElementById 根据ID获取一个标签 --->这里是 ...
- webview页面和壳通信的库(精简版)
// PG精简版 (function() { var PG = { iosBridge: null, callbackId: 0, callbacks: [], commandQueue: [], c ...
- NPN&PNP
一.晶体管基础知识 晶体管分2种:NPN.PNP 晶体管通常封装为TO-92,下面是元件实物图 和 元件符合: NPN: 当电压和电流被加到基极上时,NPN晶体管: 其工作原理: 就像水龙头—给控制开 ...
- Windows_cmd_命令
1. netstat -ano 查看端口占用情况 netstat -anp // 命令来查看一下,Linux系统是否在监听 3306 这个端口号 2.
- web设计经验<九>教你测试手机网页的5大方法
我们知道手机浏览器的使用量每天都在增长,根据StatCounter的统计数据,手机和平板的使用量约占30%的网络流量,这意味着消费者耗费在移动版网页上的时间比以往任何时候都高.可即使具备诸如移动端优先 ...
- Python学习笔记6-字典
定义 使用键值对, >>> person = {"name":"keven","age":15,"gender& ...
- gO语言的安装和环境变量的配置
一.Go语言下载 go语言官方下载地址:https://golang.org/dl/ 找到适合你系统的版本下载,本人下载的是windows版本.也可以下载Source自己更深层次研究go语言. 二.G ...