[LeetCode] String to Integer (atoi) 字符串
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.
Update (2015-02-10):
The signature of the C++
function had been updated. If you still see your function signature accepts a const char *
argument, please click the reload button to reset your code definition.
spoilers alert... click to show 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.
题目是实现string 转换int 的功能,需要注意的是前置空格,但是会忽略后置部分,需要注意的是判断int 的溢出,可以通过这样的一个判断:
if( res> (INT_MAX - str[i] +'') / )
overflow = ;
end;
#include<string>
#include<iostream>
using namespace std; class Solution {
public:
int atoi(string str) {
if(str.length()==) return ;
int len = str.length();
int idx= ;
while(idx<len&&str[idx]==' ') idx++;
if(idx==len) return ;
int flag =;
if(str[idx]=='+') idx++;
else if(str[idx]=='-'){
flag=;
idx++;
}
int sum = ;
int overflow =;
while(idx<len&&str[idx]>=''&&str[idx]<=''){
if(sum>(INT_MAX - str[idx]+'')/){
overflow = ;
break;
}
sum *=;
sum+=str[idx] -'';
idx++;
}
if(overflow){
if(flag) return INT_MAX;
else return INT_MIN;
}
// while(idx<len&&str[idx]==' ') idx++;
// if(idx!=len) return 0;
if(flag) return sum;
else return -sum;
}
}; int main()
{
string str = " 2147483648 ";
Solution sol;
cout<<sol.atoi(str)<<endl;
return ;
}
[LeetCode] String to Integer (atoi) 字符串的更多相关文章
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [Leetcode] String to integer atoi 字符串转换成整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- [leetcode]8. String to Integer (atoi)字符串转整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))
这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...
随机推荐
- 微信公众帐号开发之一(java)
闲来没事,就记录一下微信公众平台的开发吧~ 其实微信公众平台开发没有想象中的那么困难,因为注册了微信公众平台帐号登录之后在开发者模式里有详细的文档,个人感觉介绍还是比较详细的. 微信公众平台订阅号和服 ...
- IDEA整合Mybatis+Struts2+Spring(一)--新建项目
1.IDEA新建Maven项目: (1)依次点击File->New->Project,弹出如下对话框: (2)在弹出的New Project页面上,①选择Maven,② 勾选Create ...
- 关于mybatis callSettersOnNulls 配置
今天做了一件坑了自己的事情,为此浪费了好多时间... 在mybatis的设置中,看到了这样的一行设置.出于程序员的好奇,去搜索了一下,这条设置是干什么的. <setting name=" ...
- 整理好的一些mysql表详细操作
一.创建表的完整语法#语法:create table 库名.表名( 字段名1 类型[(宽度) 约束条件], 字段名2 类型[(宽度) 约束条件], 字段名3 类型[(宽度) 约束条件]);约束条件:是 ...
- RPC框架 - thrift 服务端
-------服务端程序 ------ 下载 下载 thrift 源代码包 下载 thrift 的bin包 准备描述文件(使用源代码包的示例文件) \thrift-0.10.0\tu ...
- 微信小程序navigator的open-type跳转问题
navigator的open-type属性 可选值 'navigate'.'redirect'.'switchTab',对应于wx.navigateTo.wx.redirectTo.wx.switch ...
- Keywords Search HDU - 2222 ( ac自动机)模版题
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- 排列算法汇总(下一个排列,全排列,第K个排列)
一.下一个排列 首先,STL提供了两个用来计算排列组合关系的算法,分别是next_permutation和prev_permutation. next_permutation(nums.begin() ...
- Linux命令之---rm
命令简介 rm命令为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除.对于链接文件,只是删除了链接,原有文件均保持不变. rm是一个危险的命令,使用的时候要特别当 ...
- “帮你”app-NABCD
1.你的创意解决了用户的什么需求?(N) 本学校已存在的失物招领.表白墙.二手市场等QQ群普遍存在信息冗杂,时效性差等缺点.不能充分发挥信息有效性的,我们的“帮你”APP能够充分发挥信息的有效性,让失 ...