字符串转int型atoi函数-leetcode
来源:互联网 发布:国际淘宝网官方网站 编辑:程序博客网 时间:2024/05/19 02:19
昨天在leetcode上面刷题,遇到了字符串转整型的一道题,贴到这里。
原题:
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.
Subscribe to see which companies asked this question
首先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什么意思呢?就是输入的字符串里面可能带正负号,也可能有空格。但是这些都是合法的,可以转为为整型。比如“+123”,“123”结果就是123。
“-123”就是-123。“ -123”这个字符串结果就是-123。
接下来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.这句话就是字符串里面可以有其他字符,但是有条件的。比如“ -123a123”结果就是-123,但是“ b123”就是非法的。意思就是若想转为整型,字符串除空格外,第一项必须是“+“,”-”或者数字。除此之外,连续的数字后面若是有其他非数字字符,忽略掉,不影响结果。就是这样的。“123abc”结果就是123。“1234a12”结果就是1234.
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.这句话就是非法情况返回值为0。但是考虑整型能表示的最大值。如果溢出,我们就返回INT_MAX或者INT_MIN。ok,可以写代码了。代码如下:
int myAtoi(string str) { int begin=0; int end=0; long long Iteger=0; int i=0; if(str.size()<=0) return 0; for(i=0;i<str.size();i++) { if(str[i]>='0'&&str[i]<='9'||str[i]=='+'||str[i]=='-') { begin=i; break; } else if(str[i]!=' ') { return 0; } } end=begin; for(i=begin+1;i<str.size();i++) { if(str[i]<'0'||str[i]>'9') { end=i; break; } } if(i==str.size()) end=str.size()-1; for(i=begin;i<=end;i++) { if(str[i]>='0'&&str[i]<='9') { int tmp=str[i]-'0'; Iteger=Iteger*10+tmp; if(Iteger-1>INT_MAX) { break; } } } if(str[begin]=='-') { if(Iteger-1>=INT_MAX) return INT_MIN; else return -Iteger; } else { if(Iteger>=INT_MAX) return INT_MAX; else return Iteger; } }
需要考虑的情况还蛮多的。好在最后AC了。心累!
- 字符串转int型atoi函数-leetcode
- 字符串string转整数int 实现自己的atoi函数
- C语言中atoi()函数实现--字符串转int型整数
- atoi()函数:将字符串转换成int
- atoi-字符串转Int的实现
- atoi/atof 字符串转Int/float
- [C++]LeetCode 8:String to Integer (atoi)(字符串转int)
- atoi函数:c\c++中把字符串整数转换为int型整数
- C语言atoi()函数:将字符串转换成int(整数)
- atoi() 函数用来将字符串转换成整数(int)
- C语言atoi()函数:将字符串转换成int(整数)
- C语言atoi()函数:将字符串转换成int(整数)
- C语言atoi()函数:将字符串转换成int(整数)
- leetcode 412 to_string 转string atoi(str.c_str())转int
- 1.leetcode-字符串处理函数atoi的书写
- C语言字符串转整数atoi函数
- C语言atoi()函数:将字符串转换成int(整数)和sprintf和memset
- itoa、atoi 整型转字符串、字符串转整型的函数
- 神经网络中交叉熵代价函数 求导
- powerdesigner 使用
- Javascript - 数组详解
- IOS开发编程建议与规范
- Android软键盘弹出时把布局顶上去的解决方法
- 字符串转int型atoi函数-leetcode
- 根运动 (Root Motion) – 工作原理
- lodash:javascript工具库
- iOS 上传照片到服务器 获取相册和拍照功能的照片 照片压缩上传
- 自定义Activity的启动关闭动画
- 北邮OJ 981. 16校赛-Saber's Number Game
- "==" 与 equals()的 区别
- Android 五大布局讲解与应用
- 如何快速区分MindMapper中的超链接和附件