【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)
7 - Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Notice:
2.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?
3.For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
int reverse(int x)
{
long long num=x; //To avoid overflow
long long result=;
int sign=;
if(num<){
sign=-;
num=-num;
}
while(num!=){
result=result*+num%;
num/=;
}
if(result>INT_MAX){
if(result==INT_MAX+&&sign==-)return INT_MIN;
else
return ;
}
return result*sign;
}
8 - String to Integer
Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
Tips:
1. Input:"+-2" Output:-2 Expected:0
2. Each time input str[i], it needs to judge if overflow happened, the number may exceed either long or long long
int myAtoi(string str)
{
if(str.size()==)return ;
int i=,flag=;
while(str[i]==' ')i++;
if(str[i]=='-'||str[i]=='+')
{
if(str[i]=='-')flag=-;
i++;
}
if(str[i]>''||str[i]<'')return ;
long long temp;
for(int j=i;j<str.size();j++)
{
if(str[j]>''||str[j]<'')break;
temp=temp*+(str[j]-'');
if(flag== && temp>INT_MAX)
return INT_MAX;
else if(flag==- && temp>INT_MAX)
return INT_MIN;
}
return (int)(temp*flag);
}
【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)的更多相关文章
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 【LeetCode】190 & 191 - Reverse Bits & Number of 1 Bits
190 - Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...
- 【LeetCode】758. Bold Words in String 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
随机推荐
- JSP页面的构成
JSP页面就是带有JSP元素的常规Web页面,它由静态内容和动态内容构成.其中,静态内容指HTML元素,动态内容(JSP元素)包括指令元素.脚本元素.动作元素.注释等内容. 1.指令元素 指令 ...
- 解决:Eclipse调试的时候报错'Launching XXX' has encountered a problem. Cannot connect to VM.
问题如下图: 原因:开了代理(我使用的是Proxifier)如图 解决方案:关闭Proxifier即可
- .net Windows服务程序和安装程序制作图解 及 VS 2010创建、安装、调试 windows服务(windows service)
.net Windows服务程序和安装程序制作 最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作 ...
- python中的类简单讲解
类似其它的语言, Python 中的函数使用小括号( () )调用.函数在调用之前必须先定义.如果函数中没有 return 语句, 就会自动返回 None 对象. Python 是通过引用调 ...
- ACdream 1726 A Math game (dfs+二分)
http://acdream.info/problem?pid=1726 官方题解:http://acdream.info/topic?tid=4246 求n个数里面能不能选一些数出来让它们的和等于k ...
- vs中常用的快捷键
VS中常用的快捷键: ctrl+s 保存 ctrl+Shift+S 保存所有VS中打开的所有文件 ctrl+O 打开新文件 ctrl+Shift+O 打开项目 ...
- [转]深入hibernate的三种状态
学过hibernate的人都可能都知道hibernate有三种状态,transient(瞬时状态),persistent(持久化状态)以及detached(离线状态),大家伙也许也知道这三者之间的区别 ...
- 函数lock_rec_enqueue_waiting
type_mode基础上 加上 LOCK_WAIT 表示等待状态 /****************************************************************** ...
- [转]JAVA中Action层, Service层 ,modle层 和 Dao层的功能区分
首先这是现在最基本的分层方式,结合了SSH架构.modle层就是对应的数据库表的实体类.Dao层是使用了Hibernate连接数据库.操作数据库(增删改查).Service层:引用对应的Dao数据库操 ...
- CodeForces Round #250 Div2
A. The Child and Homework 注意仔细读题,WA了好多次,=_= #include <cstdio> #include <cstring> #includ ...