LeetCode题目:String to Integer (atoi)
来源:互联网 发布:英语语义学 知乎 编辑:程序博客网 时间:2024/05/21 15:50
题目原址:点击打开链接
题目描述:
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 aconst 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.
我的代码:
class Solution {public: int myAtoi(string str) { long result=0; int sign=0,b=0; int l=str.length(); for(int i=0;i<l;i++){ if(str[i]==' '){ if(b==1)break; continue; } if(str[i]=='+'){ b=1; if(sign!=0)return 0; sign=1; continue; } if(str[i]=='-'){ b=1; if(sign!=0)return 0; sign=-1; continue; } if(str[i]<'0'||str[i]>'9'){ b=1; break; } if(str[i]>='0'&&str[i]<='9'){ b=1; result =result*10+str[i]-'0'; } if(result>INT_MAX)return INT_MAX; if(result<INT_MIN)return INT_MIN; } if(sign!=0)return result*sign; return result; }};
- LeetCode算法题目:String to Integer (atoi)
- LeetCode题目:String to Integer (atoi)
- LeetCode题目: 8. String to Integer (atoi)
- LeetCode题目:String to Integer (atoi)
- leetcode String to Integer (atoi) -- 很无聊的题目
- 3.leetcode题目8:String to Integer (atoi)
- LeetCode:String to Integer (atoi)
- LeetCode: String to Integer (atoi)
- LeetCode String to Integer(atoi)
- LeetCode :String to Integer (atoi)
- Leetcode: String to Integer (atoi)
- Leetcode : String to Integer (atoi)
- 【leetcode】String to Integer (atoi)
- [LeetCode]String to Integer (atoi)
- LeetCode-String to Integer (atoi)
- [leetcode] String to Integer (atoi)
- LeetCode - String to Integer (atoi)
- 【leetcode】String to Integer (atoi)
- 服务器搭建出现问题
- 金蝶EAS,BOS,KDTable表格增加普通行、合计行,需先解析表格
- 编译预处理
- 游戏sdk接入教程:uc(阿里游戏)接入
- numtri-section1.5
- LeetCode题目:String to Integer (atoi)
- MongoDB复制集安全认证
- tf.nn.conv2d 参数含义及结果维度
- tensorflow 多线程操作文件中关于文件名命名的思考
- redis自启动
- MyBatis--查询缓存
- java 检测代码运行时间
- 魔戒
- 支付宝java sdk jar和源代码包上传到本地仓库