Leetcode #8. String to Integer (atoi)
来源:互联网 发布:wps在线数据库 编辑:程序博客网 时间:2024/06/08 17:33
题目
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(object): def myAtoi(self, str): """ :type str: str :rtype: int """ if str == None: return 0 str = str.strip() if len(str) == 0: return 0 # if string be like '++1234' if len(str) >= 2 and (str[1] == '+' or str[1] == '-'): return 0 flag = '+' result = 0 for i in range(len(str)): if str[i] >= '0' and str[i] <= '9': result = result * 10 + int(str[i]) elif i == 0: if str[0] == '-': flag = '-' continue elif str[0] == '+': continue else: # begin with NA e.g. 'b11228552307' return 0 else: break INT_MAX_PlUS = (1<<31) - 1 INT_MAX_MINUS = -1 * (1<<31) if flag == '-': result = -result if result > INT_MAX_PlUS: result = INT_MAX_PlUS if result < INT_MAX_MINUS: result = INT_MAX_MINUS return result
- [LeetCode]8. String to Integer (atoi)
- LeetCode 8. String to Integer (atoi)
- 8. String to Integer (atoi) Leetcode Python
- LeetCode --- 8. String to Integer (atoi)
- LeetCode 8.String to Integer (atoi)
- [Leetcode] 8. String to Integer (atoi)
- [leetcode] 8.String to Integer (atoi)
- [LeetCode] 8.String to Integer (atoi)
- <LeetCode OJ> 8. String to Integer (atoi)
- leetCode 8. String to Integer (atoi)
- 8. String to Integer (atoi) LeetCode
- leetcode 8. String to Integer (atoi)
- leetcode 8. String to Integer (atoi)
- LeetCode OJ 8.String to Integer (atoi)
- Leetcode ☞ 8. String to Integer (atoi)
- 8. String to Integer (atoi) ---Leetcode
- leetcode 8. String to Integer (atoi)
- Leetcode: 8. String to Integer (atoi)(JAVA)
- GIT常用命令,gitLinux常用命令,实用命令----3
- 嗯,哇! Git 教程 上手实战,立马学会(一)
- mysql存储路径修改
- 初学者简单搭建vue.js环境
- aes 加密算法。
- Leetcode #8. String to Integer (atoi)
- Timing Wheel 定时轮算法
- java从入门到精通学习笔记(四)——多线程
- ZeroClipboard,竟然无法在Google Chrome浏览器中使用
- css3动画模拟光标
- bridge模式实例
- LINQ的左连接、右连接、内连接
- Spring框架之IoC容器—AutowireCapableBeanFactory接口
- Windows Server 2008 R2 开启Win7主题效果Aero