【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 ( ...
随机推荐
- istream, outstream使用及常见错误
使用方法: 使用filebuf打开文件,并拷贝给istream/ostream. 如下面的例子中,实现读取并处理deseq文件夹下所有文件,输出到ostream fw. code: #include& ...
- vc 6.0 连接数据库
在mysql的文档(C:\Program Files\MySQL\MySQL Server 5.5\lib)里面找到libmysql.dll和libmysql.lib 两个文件,将他们移到 工程文件下 ...
- WINCE设备开机灰屏问题(很怪异)
WINCE设备开机灰屏问题(很怪异) 1. 问题现象 图1 无法进入系统,虽然没有调试信息,但应该可以判断是程序跑飞了.我们这款产品用到3种显示屏(采用不同的驱动IC),可是测试发现1和2号屏 ...
- JSON 之 SuperObject(4): 增、删、改
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- phpcms上线步骤,无法生成缓存,页面空白原因
缓存目录没有设置成777. 1.修改数据库配置 2.修改数据表中的本地地址成线上地址 3.修改代码中的配置,本地地址批量改为线上地址 4.登录后台,更新缓存
- c#调用系统资源大集合-2
public static void 打开格式化对话框() { Process.Start("rundll32.exe"," shell32.dll,SHFormatDr ...
- PHPStorm/webstorm tips
phpstorm对于使用PHP开发web的人员来说,是一个非常不错的编辑开发IDE,以前用过sublime,但是相比于storm,sublime在浏览legacy代码,类代码编辑方面明显要逊色不少.同 ...
- 嵌入式linux内核是什么?
linux内核是一种可以被内核动态加载(insmode)和卸载(rmmod)的可执行二进制代码 最简单的内核 #include <linux/module.h> #include < ...
- CSS3之边框样式(动画过渡)
简述 CSS3中transition属性定义了过渡,我们可以使用它来辅助我们实现一个边框样式的动画过渡. 简述 transition 定义和用法 语法 实现 效果 源码 transition 定义和用 ...
- POJ2886 Who Gets the Most Candies? 线段树 反素数
题意:有一群小朋友围成一个环,编号1,2,3…N.每个人手上握着一个非0的数字,首先第K个人出列,然后看他手上的数字,假设为m,则从下一个开始第m个人出列,一直如此.并设i为小于等于N的最大反素数,问 ...