42:String to Integer (atoi)
来源:互联网 发布:狗狗用品专卖店淘宝 编辑:程序博客网 时间:2024/04/27 14:50
本题解法代码的思想及编写参考了网址https://github.com/soulmachine/leetcode#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.
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(const string& str) { int num = 0; int sign = 1; const int n = str.size(); int i = 0; while (i < n && str[i]) ++i; if (i == n) return 0; if (str[i] == '+') ++i; else if (str[i] == '-') sign = -1, ++i; for (; i < n; ++i) { if (str[i] < '0' || str[i] > '9') break; if (num > INT_MAX / 10 || (num == INT_MAX / 10 && (str[i] - '0') > INT_MAX % 10)) return sign == -1 ? INT_MIN : INT_MAX; num = num * 10 + str[i] - '0'; } return num * sign }};
解题代码版本二如下:
class Solution {public: int myAtoi(string str) { string s; int sign = 1; int i = 0; while (i != str.size() && isspace(str[i])) ++i; if (i == str.size()) return 0; else if (str[i] == '+') ++i; else if (str[i] == '-') ++i, sign = -1;; for ( ; i != str.size(); ++i) if (isdigit(str[i])) s += str[i]; else break; if (s.size() > 10 && (s.size() == 10 && s > 2147483647)) return sign == -1 ? INT_MIN : INT_MAX; int target = 0; for (int i = s.size() - 1; i >= 0; --i) target += (s[i] - '0') * power(10, s.size() - 1 - i); return target * sign; }};
- 42:String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- string to integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer(atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- ural1017 Staircases (动态规划)
- 谁是卧底
- HM编码器代码阅读(44)——样点自适应补偿SAO(三)选取最优的SAO模式并确定补偿值
- Android两种播放视频的方法(SurfaceView+MediaPlayer+SeekBar)跟(VideoView+MediaController)
- 信道效率以及信道的吞吐率
- 42:String to Integer (atoi)
- Only the original thread that created a view hierarchy can touch its views.
- leetcode 412 python
- java中初始化顺序
- System.arraycopy() 与 Arrays.copyOf()两个方法的区别
- Android转场动画总结篇(不基于Android5.0)
- openstack入门学习笔记(一) 各组件介绍
- Leetcode 525. Contiguous Array
- 欢迎使用CSDN-markdown编辑器