【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 ( ...
随机推荐
- SpringMVC整合Shiro——(3)
SpringMVC整合Shiro,Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能. 第一步:配置web.xml <!-- 配置Shiro过滤器,先让Shiro ...
- sql语句的基本操作
建立一个数据库 create DATABASE mydatabase; 建立一张数据表: ##创建一个员工表##create table employee( eid int not NULL PRIM ...
- 应用scikit-learn做文本分类
文本挖掘的paper没找到统一的benchmark,只好自己跑程序,走过路过的前辈如果知道20newsgroups或者其它好用的公共数据集的分类(最好要所有类分类结果,全部或取部分特征无所谓)麻烦留言 ...
- 30 个 PHP 的 Excel 处理类
下面的 PHP Excel 处理类中,包含 Excel 读写.导入导出等相关的类,列表如下: PHP Excel Reader classes 1. Read Excel Spreadsheets u ...
- PHP也20岁了
当今许多世界著名的编程语言的年纪已经够大了.举个例子,PHP昨天过了生日已经20岁了,Python也24岁,HTML已经服务了22年,Ruby和JavaScript有20年,Java前段时间刚过了20 ...
- vim快捷键笔记【原创】
Vim zR 全部展开 zM全部合并 vim 快捷键 shift + i (‘I’) 进行编辑 shift + 4 (‘$’) 跳到行尾 shift ...
- SeaJS 学习
什么是系统 在生活和工作中,我们会接触到大量系统:自然界生态系统.计算机操作系统.软件办公系统,还有教育系统.金融系统.网络系统.理论系统等等.究竟什么是系统呢? 来看下维基百科的解释: 系统泛指由一 ...
- leetcode:Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 《OD大数据实战》HBase入门实战
官方参考文档:http://abloz.com/hbase/book.html#shell_tricks 1.2.3. Shell 练习 用shell连接你的HBase $ ./bin/hbase s ...
- 二、理解over()函数
1.1.两个order by的执行时机分析函数是在整个sql查询结束后(sql语句中的order by的执行比较特殊)再进行的操作, 也就是说sql语句中的order by也会影响分析函数的执行结果: ...