8 - String to Integer (atoi)
来源:互联网 发布:socket函数 linux 编辑:程序博客网 时间:2024/05/18 03:21
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.
class Solution {public: int myAtoi(string str) { string line(str); line.erase(0, line.find_first_not_of(" \t")); line.erase(line.find_last_not_of(" \t") + 1); long result = 0; bool start = false; int positive = 0; bool con = false; for(int i = 0; i < line.size(); i++) { if( start == false && line[i] == '-' ) { if(positive == 1) { positive = 3; break; } positive = 2; i++; } if( start == false && line[i] == '+') { if(positive == 2) { positive = 3; break; } positive = 1; i++; } if( start == false && !((line[i] >= '0' && line[i] <= '9') || line[i] == '+' || line[i] == '-')) break; if(positive != 0 && !isdigit(line[i]) ) { break; } if(isdigit(line[i])) { result = result * 10 + ( line[i] - '0' ); if(result > 2147483648L) break; start = true; } else { if(start == true) break; } } if(positive == 3) result = 0; if(positive == 2) result = -result; if(result < (numeric_limits<int>::min)() ) result = (numeric_limits<int>::min)(); if(result > (numeric_limits<int>::max)()) result = (numeric_limits<int>::max)(); return result; }};
- 8、String to Integer (atoi)
- 8,String to Integer (atoi)
- 8 String to Integer (atoi)
- 8 String to Integer (atoi)
- #8 String to Integer (atoi)
- 8String to Integer (atoi)
- 8 String to Integer (atoi)
- 8 - String to Integer (atoi)
- [8]String to Integer (atoi)
- 8、String to Integer (atoi)
- 【8】String to Integer (atoi)
- 8String to Integer (atoi)
- 8 String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- FastJson---高性能JSON开发包
- hibernate一对多关联映射—单向
- Git的使用六(撤销修改)仅限Mac亲测
- Material Design Icons v1.2.65 图标工具
- STL源码剖析 [容器](十七)[stl_multimap.h]
- 8 - String to Integer (atoi)
- android EditText 监听焦点变化
- Oracle_如何从数据库 11.2 降级至之前的版本(包括 11.2.0.3-11.2.0.1)
- 栈的应用:后缀表达式到中缀表达式的转换
- 【新的开始】把握现在
- 仿QQ消息提示小圆点---启发demo
- WPF comboBox内字体选择器的简单使用
- C++ 各种全局常量的声明方式的优缺点?
- web前端学习-不常用的HTML5全局属性