[LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com'
https://oj.leetcode.com/problems/string-to-integer-atoi/ 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. ===Comments by Dabay===
主要就是处理各种情况:
先把两边的引号和空格去掉。
然后确定符号。
接着读数字。
最后判断越界。
'''
class Solution:
# @return an integer
def atoi(self, str):
str_new = str.strip("'").strip('"').strip()
if str_new == "":
return 0 result = 0
positive = True
start_flag = False
for i in xrange(len(str_new)):
if start_flag is False:
if str_new[i] == "-":
positive = False
start_flag = True
continue
if str_new[i] == "+":
start_flag = True
continue
if str_new[i] in "0123456789":
start_flag = True
result = result * 10 + int(str_new[i])
else:
break if not positive:
result = result * -1
if result > 2147483647:
result = 2147483647
if result < -2147483648:
result = -2147483648 return result def main():
s = Solution()
print s.atoi(" -00012a3 +2") if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[LeetCode][Python]String to Integer (atoi)的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- leetcode 解题 String to Integer (atoi)(C&python)
//此题是easy题,比较简单,主要困难在考虑全输入的各种情况://1.开始的时候有空格等空白字符//2.开头有加减号//3.溢出(第一次写就是没有考虑到这个情况) //C代码int myAtoi(c ...
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
- 蜗牛慢慢爬 LeetCode 8. String to Integer (atoi) [Difficulty: Medium]
题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cas ...
- LeetCode 8. String to Integer (atoi) (字符串到整数)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
随机推荐
- 自学HTML5第一天(认识HTML5的全局属性)
contextmenu 属性 规定 <div> 元素的上下文菜单.上下文菜单会在用户右键点击元素时出现.列子: <div contextmenu="mymenu" ...
- win7下设置 WiFi AP
开启windows 7的隐藏功能:虚拟WiFi和SoftAP(即虚拟无线AP),就可以让计算机变成无线路由器.实现共享上网. 1.以管理员身份运行命令提示符: “开始”---在搜索栏输入“cmd”-- ...
- QtQml 应用程序的性能考虑与建议(来自小V的翻译)
QtQml 应用程序的性能考虑与建议 原文:csdn aidear_evo QtQml应用程序的性能考虑与建议 本文翻译自Qt官网文档:http://doc.qt.io/qt-5/qtquick-pe ...
- java常量池理解
String类两种不同的创建方式 String s1 = "zheng"; //第一种创建方式 String s2 = new String("junxiang" ...
- 习题3.10 约瑟夫环 josephus问题
/* assume a header */ /* 双向循环链表 */ struct Node; typedef struct Node * PtrToNode; typedef PtrToNode L ...
- AlertView点击确定后再执行后面的代码
AlertView的show方法执行后,后面的代码会继续运行,而不会等待用户按键结束再走,这样,如果把弹出的代码写在一个BOOL函数里,没等用户确认直接返回NO就惨了,解决方法: - (BOOL)be ...
- ORA-07445 [mdagun_iter+957] When Using SDO_AGGR_UNION 问题处理
问题描写叙述: ORA-07445: mdagun_iter() [Address not mapped to object] Oracle Database 10g Enterprise Edit ...
- sqlserver查询某一字段重复超5次的所有记录
用的sqlserver2008 r2. SELECT * FROM t_class WHERE id IN (SELECT id FROM (SELECT ROW_NUMBER() OVER ( ...
- JDBC连接数据库及增删改查操作
什么是JDBC?Java语言访问数据库的一种规范,是一套APIJDBC (Java Database Connectivity) API,即Java数据库编程接口,是一组标准的Java语言中的接口和类 ...
- oracle tablespace
oracle tablespace 1. 查看所有表空间大小 SQL> select tablespace_name,sum(bytes)/1024/1024 from dba_data_fil ...