8 atoi python
http://azaleasays.com/2009/09/05/python-strip-non-numeric-characters/
python 去除字串中的非數字或非字母
http://blog.csdn.net/shark0001/article/details/1363564
filter()函式
filter()函式包括兩個引數,分別是function和list。該函式根據function引數返回的結果是否為真來過濾list引數中的項,最後返回一個新列表,如下例所示:a=[1,2,3,4,5,6,7]
b=filter(lambda x:x>5, a)
print b
[6,7]
如果filter引數值為None,就使用identity()函式,list引數中所有為假的元素都將被刪除。如下所示:
a=[0,1,2,3,4,5,6,7]
b=filter(None, a)
print b
[1,2,3,4,5,6,7]
(注意對於空格和—+號的不同對待)
version1(mistake (input +-2 output: -2 expected:0))
class Solution:
# @return an integer
def atoi(self, str):
INT_MAX=2147483647
INT_MIN=-2147483648
n=len(str)
i=0
sign=1
sum=0
while str=='':
return 0
while i<n and (str[i]=='+' or str[i]=='-' or str[i].isspace()):
if str[i]=='-':
sign=-1
i+=1
while i<n and str[i].isdigit():
digit= int(str[i])
if INT_MAX/10 > sum:
sum *=10
else:
if sign == 1:
return INT_MAX
else:
return INT_MIN
if INT_MAX - digit >= sum:
sum += digit
else:
if sign == 1:
return INT_MAX
else:
return INT_MIN
i+=1
return sign*sum
version1(mistake (input=“ 010” output:0 expected:10))
class Solution:
# @return an integer
def atoi(self, str):
INT_MAX=2147483647
INT_MIN=-2147483648
n=len(str)
i=0
sign=1
sum=0
while str=='':
return 0
if i<n and (str[i]=='+' or str[i]=='-' or str[i].isspace()):
if str[i]=='-':
sign=-1
i+=1
while i<n and str[i].isdigit():
digit= int(str[i])
if INT_MAX/10 > sum:
sum *=10
else:
if sign == 1:
return INT_MAX
else:
return INT_MIN
if INT_MAX - digit >= sum:
sum += digit
else:
if sign == 1:
return INT_MAX
else:
return INT_MIN
i+=1
return sign*sum
version3
class Solution:
# @return an integer
def atoi(self, str):
INT_MAX=2147483647
INT_MIN=-2147483648
n=len(str)
i=0
sign=1
sum=0
while str=='':
return 0
while i<n and str[i].isspace():
i+=1
if i<n and (str[i]=='+' or str[i]=='-' ):
if str[i]=='-':
sign=-1
i+=1
while i<n and str[i].isdigit():
digit= int(str[i])
if INT_MAX/10 >= sum:
sum *=10
else:
if sign == 1:
return INT_MAX
else:
return INT_MIN
if INT_MAX - digit >= sum:
sum += digit
else:
if sign == 1:
return INT_MAX
else:
return INT_MIN
i+=1
return sign*sum
相關文章
- Leetcode 8. String to Integer (atoi) 字串轉整數 (atoi)LeetCode字串
- Leetcode 8 String to Integer (atoi)LeetCode
- LeetCode-8. 字串轉整數 (atoi)LeetCode字串
- 【LeetCode 8_字串_實現】String to Integer (atoi)LeetCode字串
- String to Integer (atoi) 字串轉換整數 (atoi)字串
- 字串轉換整數(atoi)字串
- 不再傻傻分不清:atoi, itoa, iota
- atoi函式簡單實現函式
- leetcode String to Integer (atoi)LeetCode
- Leetcode - String to Integer (atoi)LeetCode
- String to Integer (atoi) leetcode javaLeetCodeJava
- [C練習]my_atoi函式實現函式
- C中atoi和strcpy的自定義實現
- C語言 itoa函式及atoi函式C語言函式
- LeetCode String to Integer (atoi)(008)解法總結LeetCode
- C語言50題之模擬實現atof、atoiC語言
- [LeetCode] String to Integer (atoi) 字串轉為整數LeetCode字串
- 8 python 抽象類Python抽象
- 字串轉數字atoi的重新編寫及注意事項字串
- chap8-fluent pythonPython
- C語言atoi()函式:將字串轉換成int(整數)C語言函式字串
- [Cexpert-001] How to implement atoi with least codes using C language?AST
- Python入門基礎(8)Python
- python學習手冊(8)Python
- 小白學python系列-(8)dictPython
- python操作k8sPythonK8S
- Python基礎入門(8)- Python模組和包Python
- python的PEP8規範Python
- python mysql utf8mb4PythonMySql
- 8個關於Python的小技巧Python
- python中flake8是什麼Python
- WingPro 8 for Mac(Python開發工具)MacPython
- Python面試中8個必考問題Python面試
- Python 面試中 8 個必考問題Python面試
- 8個開源的Python變種Python
- PEP8——Python程式碼規範Python
- Python程式設計入門(8) (轉)Python程式設計
- python管理k8s叢集PythonK8S