Leetcode 8. String to Integer (atoi) 字串轉整數 (atoi)
使用正規表示式非常簡單,程式碼簡短,速度也不慢(68ms)在python中打敗94%
class Solution:
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
import re
pattern1 = re.compile(r'^\s{0,}([+-]?\d+)')
match1 = pattern1.match(str)
if match1:
number_str = match1.group()
number = int(number_str)
else:
number = 0
if number < -1*2**31:
return -1*2**31
elif number > 2**31-1:
return 2**31 - 1
else:
return number
相關文章
- String to Integer (atoi) 字串轉換整數 (atoi)字串
- Leetcode 8 String to Integer (atoi)LeetCode
- LeetCode-8. 字串轉整數 (atoi)LeetCode字串
- 字串轉換整數(atoi)字串
- LeetCode String to Integer (atoi)(008)解法總結LeetCode
- Roman to Integer 羅馬數字轉整數
- Sting 轉List<String>轉List<Integer>
- LeetCode反轉整數(Python)LeetCodePython
- LeetCode(7)--.反轉整數LeetCode
- 《Leetcode of December》劍指 Offer 67. 把字串轉換成整數LeetCode字串
- LeetCode 13[羅馬數字轉整數]LeetCode
- python實現字串轉換整數Python字串
- LeetCode7.整數反轉 JavaScriptLeetCodeJavaScript
- LeetCode7:ReverseInteger(整數反轉)LeetCode
- LeetCode 力扣 羅馬數字轉整數LeetCode力扣
- LeetCode_Python(13)_羅馬數字轉整數LeetCodePython
- leetcode13題——羅馬數字轉整數LeetCode
- python-leetcode13羅馬數字轉整數PythonLeetCode
- java-string轉換成integer的方式及原理Java
- JZ-049-把字串轉換成整數字串
- LeetCode每日一題:整數反轉(No.7)LeetCode每日一題
- 每日一道 LeetCode (2):整數反轉LeetCode
- LeetCode題庫整理【Java】—— 7整數反轉LeetCodeJava
- 2020-11-13整數轉換為字串 字元反轉字串字元
- InputStream流轉換成String字串字串
- 每日一道 LeetCode (4):羅馬數字轉整數LeetCode
- 浮點數轉換為String字串(可設定精度)方法字串
- Leetcode 12 Integer to RomanLeetCode
- Leetcode 13 Roman to IntegerLeetCode
- Leetcode 7 Reverse IntegerLeetCode
- js字串string轉object物件 - 方法篇JS字串Object物件
- 劍指offer(Java版)--將字串轉換為整數Java字串
- 劍指offer——把字串轉換成整數C++字串C++
- 詳解 LeetCode_007_整數反轉(Java 實現)LeetCodeJava
- LeetCode初級-反轉字串LeetCode字串
- 【LeetCode】796. 旋轉字串LeetCode字串
- LeetCode-344-反轉字串LeetCode字串
- Leetcode 273 Integer to English WordsLeetCode