1. 程式人生 > >【LeetCode】【Python】8. String to Integer (atoi)

【LeetCode】【Python】8. 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.


實現

  1. class Solution:  
  2.     # @param {string} str
  3.     # @return {integer}
  4.     def myAtoi(self, str):  
  5.         INT_MAX=2147483647;INT_MIN=-2147483648
  6.         index=0
  7.         while index<len(str) and str[index]==' ':index+=1
  8.         flag=1
  9.         if index<len(str) and str[index]=='-':  
  10.             index+=1
  11.             flag=-1
  12.         elif index<len(str) and str[index]=='+':  
  13.             index+=1
  14.         res=0
  15.         while index<len(str):  
  16.             if str[index]<'0'or str[index]>'9':return flag*res  
  17.             digit=ord(str[index])-ord('0')  
  18.             if flag==1and res*10+digit>INT_MAX:
    return INT_MAX  
  19.             if flag==-1and res*10+digit>-INT_MIN:return INT_MIN  
  20.             res=res*10+digit  
  21.             index+=1
  22.         return flag*res  

相關推薦

LeetCodePython8. String to Integer (atoi)

題目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input

LeetCode8. String to Integer (atoi) - Java實現

文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Implement atoi which converts a string to an integer. The function fir

leetcode8. String to Integer (atoi)(從string中按照規則提取int)

Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until

演算法設計與分析課作業week6leetcode--8. String to Integer (atoi)

題目 Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until th

LeetCode 8. String to Integer (atoi)

req argument require git empty convert int oss 非法字符 Implement atoi to convert a string to an integer. Hint: Carefully consider all possib

Leetcode:8- String to Integer (atoi)

integer font please because sta digits 翻譯 isspace time Implement atoi to convert a string to an integer. Hint: Carefully consider all pos

leetcode-8. String to Integer (atoi)

then ble sig 記得 repr exist elf sta eric 1 題目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible

[leetcode]8. String to Integer (atoi)字串轉整數

Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the firs

[leetcode]8. String to Integer (atoi)

這一題在思路上還是比較簡單。 注意邊界情況。 結合上一個reverse integer來看,要注意處理溢位 for(int m=0;m<j;m++){ if(res>(Integer.MAX_VALUE-(int)k[m]+48)/10

LeetCode 8 String to Integer(atoi)

8 String to Integer(atoi) 按照題目的要求:你要先越過前面的空格。 之後可能遇到’+’’-’ [0-9],other,也照常處理即可 還要注意溢位問題:這裡我用了long long(感覺有點作弊了QwQ)。當int溢位了,long long肯定沒溢位.所以你在轉換

Leetcode 8 String to Integer (atoi)

Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the fi

LeetCode——8. String to Integer (atoi)

1.題目詳情 Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessar

Leetcode 解題 8 String to Integer (atoi)

Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until

leetcode: 8. String to Integer (atoi)

Difficulty Medium Description /** * Example 1: * Input: "42" * Output: 42 * * Example 2: * Input: " -42" * Output: -42 * Explanat

Leetcode-8 String to Integer (atoi)(C語言)

8. String to Integer (atoi) 題目描述 Implement atoi which converts a string to an integer. The function first discards as many whitespace charac

LEETCODE 8 String to Integer (atoi) (JAVA題解)

https://leetcode.com/problems/string-to-integer-atoi/ 原題如上。 題意解析: 要把一個字串轉換成一個整數。 字串的輸入可能有如下幾種情況: 1

Leetcode 8. String to Integer (atoi) 字串轉換整數 (atoi)

Leetcode 8. String to Integer (atoi) 字串轉換整數 (atoi) 標籤: Leetcode 題目地址:https://leetcode-cn.com/problems/string-to-integer-atoi/ 題目描述 請你

LeetCode 8. String to Integer (atoi) C++ --字串轉為數字,包含正負號、空格、字母、數字等字元

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cas

Leetcode#8. String to Integer (atoi)(字串轉數字)

宣告:題目解法使用c++和Python兩種,重點側重在於解題思路和如何將c++程式碼轉換為python程式碼。 題目 Implement atoi to convert a string to an integer. Hint: Carefully con

LeetCode 8 String to Integer (atoi)(轉換到整型)

翻譯 實現“atoi”將字串轉換成整型數。 提示:仔細考慮所有可能的輸入。如你想要挑戰,請不要參閱下面並問問自己都有哪些可能的輸入請看。 說明:模糊的指定(沒有給定的輸入規格)就是為了這個問題