leetcode 8. String to Integer (atoi)
来源:互联网 发布:笔记软件 编辑:程序博客网 时间:2024/06/03 16:25
一、逗比原题
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
二、注意事项,提示都在上面了
三、个人AC代码
class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int """ if len(str) == 0: return 0 else: l = 0 r = 0 k = 1 f = 0 ss=str for i, s in enumerate(ss): if s == ' ': str = str[1:] else: break if str[0] == '-': str = str[1:] k = -1 * k elif str[0] == '+': str = str[1:] k = k rr = len(str) for i, s in enumerate(str): if (s <= '9') and (s >= '0'): if f == 0: l = i f = 1 else: rr = i break st = str[l:rr] for i, s in enumerate(st): r += int(s) * 10 ** (len(st) - i - 1) rs=k*r #shiiiiiiiiiiiiiiiiit! if rs>2147483647: rs=2147483647 elif rs<-2147483648: rs=-2147483648 return rs
- [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)
- ESP8266模块使用,环境搭建,编译和烧录
- 前进杀蝙蝠--简单递归
- 【Python】dlib实现视频中人脸68特征点提取
- 【C++】模拟实现STL中的list
- c++之旅———开始
- leetcode 8. String to Integer (atoi)
- HTTP协议
- 使用脚本自动化安装MySQL/MariaDB
- JSON解析
- python 学习手册重点
- struts2整合freemarker(一)
- 【NOIP2017提高A组集训10.21】Fantasy
- 用的VS,写的字符串的copy 、 cmp 、cat, 在VC环境下运行没有终止,在VS下就终止了
- oracle db link的查看创建与删除