[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. ...
随机推荐
- 关于Nexus 7的Usb host开发问题
按照API Guides和搜索到的各种方法,都没办法把Nexus 7上面的USB 设备列举出来.使用市场上的软件依然不行. 在找demo的时候找到一位大神chainfire,他似乎有所解释 看来得换一 ...
- Qt中添加OpenCV库
配置在Qt中的OpenCV,看了很多“教程”,最终成功.记一下过程. 本机配置: window7 32位系统: qt-opensource-windows-x86-mingw492-5.5.1: Op ...
- 实现拦截API的钩子(Hook)
道理不多讲,简单说就是将系统API的跳转地址,替换为我们自己写的API的地址,所以要求我们自定义的API函数要和被拦截的API有相同的参数.在用完后,记得恢复. 因为要挂全局的钩子,所以Hook的部分 ...
- Delphi编写的Android程序获取Root权限实现(2015.4.15更新,支持Android 4.4)
借助谷歌,并经过本大侠施展坑.蒙.拐.骗.偷五大绝技,终于成功实现在Delphi下获取Root权限并将其扩展为一个完整功能更加完整的TQAndroidShell记录,在华为荣耀2(Android 4. ...
- chrome浏览器debugger 调试,有意思。
JavaScript代码中加入一句debugger;来手工造成一个断点效果. 例子: ajax看看返回的数据内容,或者想知道js变量获取值是什么的时候. $.ajax({ type:"pos ...
- 二叉查找树的Find,FindMin,FindMax的递归和非递归实现
typedef struct TreeNode *Position; typedef struct TreeNode *SearchTree; struct TreeNode{ ElementType ...
- libiconv_百度百科
libiconv_百度百科 由于历史原因,国际化的文字常常由于语言或者国家的原因使用不同的编码.目录 1libiconv历史简介 2libiconv编码简介 3libico ...
- wxAui Frame Management用法
wxAui Frame Management用法:1. 总体步骤# 安装wxpython2.8.*.*后import wx.aui# 初始化一个wxAui管理框架对象mgr = wx.aui.AuiM ...
- javascript第四课变量作用域
局部变量: function f1() { var n1=0; //局部变量 n1=10; //全局变量,当前页面均可调用 } n1=10;//全局变量 var n1=10;//全局变量 在方法内的 ...
- S3C2440实现wifi、3G上网和迷你无线路由的制作(一)
S3C2440实现wifi.3G上网和迷你无线路由的制作 fulinux 凌云实验室 本文将通过ARM.linux平台,借助RT2070/RT3070芯片的无线模块(或使用RT2070/RT3070芯 ...