【Leet Code】String to Integer (atoi) ——常考类型题
String to Integer (atoi)
Total Accepted: 15482 Total
Submissions: 106043My Submissions
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
字符串的操作。敲代码常常性遇到。string这个类真的很实用哟,题目要求自行实现atoi的功能:
class Solution
{
public:
int atoi(const char *str)
{
while(' ' == *str)
{
str++;
}
bool isNegative = false;
if('-' == *str)
{
isNegative = true;
str++;
}
else if('+' == *str)
{
str++;
}
long long ret = 0;
while(*str)
{
if( isdigit(*str) )
{
ret = ret * 10 + (*str - '0');
if(isNegative && (-ret <= INT_MIN))
{
return INT_MIN;
}
if(!isNegative && (ret >= INT_MAX))
{
return INT_MAX;
}
}
else
{
break;
}
str++;
}
return (isNegative ? -ret : ret);
}
};
【Leet Code】String to Integer (atoi) ——常考类型题的更多相关文章
- LeetCode【8】. String to Integer (atoi) --java实现
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
随机推荐
- Nginx出现504 Gateway Time-out的解决方案
使用Nginx作为WEB服务器时,经常会遇到504 Gateway Time-out的错误提示.经过研究,基本可以确定多数情况下这个错误与Nginx本身无关,问题的根源在于Nginx将PHP的解析提交 ...
- Codechef APRIL14 ANUCBC Cards, bags and coins 背包DP变形
题目大意 有n个数字,选出一个子集,有q个询问,求子集和模m等于0的方案数%1000000009.(n <= 100000,m <= 100,q <= 30) 假设数据很小,我们完全 ...
- CDOJ 1294 天行廖的游戏 dp 容斥
天行廖的游戏 题目连接: http://acm.uestc.edu.cn/#/problem/show/1294 Description 天行健,君子以自强不息.地势坤,廖爷以厚德载物 一日在喵哈哈村 ...
- 使用pyplot和seaborn进行画图
pyplot的一些知识 matplotlab中的对象: matplotlib是面向对象的,在画图的时候我们了解一些对象,对我们画图是有帮助的.绘图的对象大致分为三层: backend_bases.Fi ...
- maven构建jar包
1.执行可执行的class,代码内需要有入口main方法 2.通过mvn package来构建jar包 3.使用java -jar test.jar来执行jar包 https://www.cnblog ...
- CentOS 6.9/7通过yum安装指定版本的JDK/Maven
说明:通过yum好处其实很多,环境变量不用配置,配置文件放在大家都熟悉的地方,通过rpm -ql xxx可以知道全部文件的地方等等. 一.安装JDK(Oracle JDK 1.8) # wget -- ...
- 普通主板设置BIOS实现电脑插电自动启动
说明: 1.为什么要实现这种功能,很多时候在民间都基本用普通PC来做小型服务器,公司的私服等等,而这些普通PC在民用电环境中经常会停电,一停就会导致服务器不能自动来电重启,所以这个功能来点开机是必须的 ...
- Current limiter allows large USB bypass capacitance
The USB (Universal Serial Bus) specification requires a connected USB device to present a load to th ...
- Spring依赖检查
在Spring中,可以使用依赖检查功能,以确保所要求的属性可设置或者注入. 依赖检查模式 4个依赖检查支持的模式: none – 没有依赖检查,这是默认的模式. simple – 如果基本类型(int ...
- C# 如何做类似微博和QQ的授权登录
记录下吧,dotnet下有这个http://dotnetopenauth.net/开源的框架可以用来实现服务端和客户端的实现,具体的使用方法还真研究,第一次接触OAuth这个概念