LeetCode - String to Integer (atoi)
来源:互联网 发布:剑三淘宝金币怎么买 编辑:程序博客网 时间:2024/04/29 17:08
LeetCode - String to Integer (atoi)
The problem is described as following:
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 theC++
function had beenupdated. If you still see your function signature accepts aconst char *
argument, please click the reload button to reset your code definition.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 behaviorof 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.
My Solution is as following:
class Solution: # @param str, a string # @return an integer def myAtoi(self, s): if len(s) == 0: return 0 sign, re, i, INT_MAX, INT_MIN = 1, 0, 0, 2147483647, -2147483648 while i < len(s) and s[i] == ' ': i += 1 if i < len(s) and s[i] == '-': sign = -1 i += 1 elif i < len(s) and s[i] == '+': i += 1 while i < len(s) and '0' <= s[i] <= '9': re = re * 10 + ord(s[i]) - ord('0') i += 1 re *= sign if re > INT_MAX: return INT_MAX if re < INT_MIN: return INT_MIN return re
- 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)
- 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)
- imap协议
- 链队列通用链表实现
- 别人技术面试总结(一)
- 军训
- 【Java】string split 分割误区
- LeetCode - String to Integer (atoi)
- 黑马程序员——OC语言基础篇---内存管理
- leetcode之String to Integer (atoi)
- map,reduce用法举例
- 使用Intent传递数据之返回结果
- eclipse代码提示
- 7-1通信系统中为什么要进行编码和译码?常见的编码方法有哪些?
- c++顺序容器的操作
- webkit常见问题汇总