自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

LeetCode之判斷回文數(shù)字

開發(fā) 前端
實(shí)現(xiàn)一個(gè) func myAtoi(_ str: String) -> Int 函數(shù),使其能將字符串轉(zhuǎn)換成一個(gè) 32 位有符號(hào)整數(shù)(類似 C/C++ 中的 atoi 函數(shù))。

[[437748]]

前言

我們社區(qū)陸續(xù)會(huì)將顧毅(Netflix 增長黑客,《iOS 面試之道》作者,ACE 職業(yè)健身教練。微博:@故胤道長[1])的 Swift 算法題題解整理為文字版以方便大家學(xué)習(xí)與閱讀。

LeetCode 算法到目前我們已經(jīng)更新了 3 期,我們會(huì)保持更新時(shí)間和進(jìn)度(周一、周三、周五早上 9:00 發(fā)布),每期的內(nèi)容不多,我們希望大家可以在上班路上閱讀,長久積累會(huì)有很大提升。

難度水平:中等

1. 描述

實(shí)現(xiàn)一個(gè) func myAtoi(_ str: String) -> Int 函數(shù),使其能將字符串轉(zhuǎn)換成一個(gè) 32 位有符號(hào)整數(shù)(類似 C/C++ 中的 atoi 函數(shù))。

函數(shù) func myAtoi(_ str: String) -> Int 的算法如下:

  • 讀入字符串并丟棄無用的前導(dǎo)空格
  • 檢查下一個(gè)字符(假設(shè)還未到字符末尾)為正還是負(fù)號(hào),讀取該字符(如果有)。確定最終結(jié)果是負(fù)數(shù)還是正數(shù)。如果兩者都不存在,則假定結(jié)果為正。
  • 讀入下一個(gè)字符,直到到達(dá)下一個(gè)非數(shù)字字符或到達(dá)輸入的結(jié)尾。字符串的其余部分將被忽略。
  • 將前面步驟讀入的這些數(shù)字轉(zhuǎn)換為整數(shù)(即,"123" -> 123, "0032" -> 32)。如果沒有讀入數(shù)字,則整數(shù)為 0 。必要時(shí)更改符號(hào)(從步驟 2 開始)。
  • 如果整數(shù)數(shù)超過 32 位有符號(hào)整數(shù)范圍 [−2^31, 2^31 − 1] ,需要截?cái)噙@個(gè)整數(shù),使其保持在這個(gè)范圍內(nèi)。具體來說,小于 −2^31 的整數(shù)應(yīng)該被固定為 −2^31 ,大于 2^31 − 1 的整數(shù)應(yīng)該被固定為 2^31 − 1 。
  • 返回整數(shù)作為最終結(jié)果。

注意:

  • 本題中的空白字符只包括空格字符 ' ' 。
  • 除前導(dǎo)空格或數(shù)字后的其余字符串外,請(qǐng)勿忽略 任何其他字符。

2. 示例

示例 1

  1. 輸入:str = "42" 
  2. 輸出:42 
  3. 解釋:加粗的字符串為已經(jīng)讀入的字符,插入符號(hào)是當(dāng)前讀取的字符。 
  4. 第 1 步:"42"(當(dāng)前沒有讀入字符,因?yàn)闆]有前導(dǎo)空格) 
  5.          ^ 
  6. 第 2 步:"42"(當(dāng)前沒有讀入字符,因?yàn)檫@里不存在 '-' 或者 '+') 
  7.          ^ 
  8. 第 3 步:"42"(讀入 "42") 
  9.            ^ 
  10. 解析得到整數(shù) 42 。 
  11. 由于 "42" 在范圍 [-231, 231 - 1] 內(nèi),最終結(jié)果為 42 。 

示例 2

  1. 輸入:str = "-42" 
  2. 輸出:-42 
  3. 解釋: 
  4. 第 1 步:"   -42"(讀入前導(dǎo)空格,但忽視掉) 
  5.             ^ 
  6. 第 2 步:"   -42"(讀入 '-' 字符,所以結(jié)果應(yīng)該是負(fù)數(shù)) 
  7.              ^ 
  8. 第 3 步:"   -42"(讀入 "42") 
  9.                ^ 
  10. 解析得到整數(shù) -42 。 
  11. 由于 "-42" 在范圍 [-231, 231 - 1] 內(nèi),最終結(jié)果為 -42 。 

示例 3

  1. 輸入:str = "4193 with words" 
  2. 輸出:4193 
  3. 解釋: 
  4. 第 1 步:"4193 with words"(當(dāng)前沒有讀入字符,因?yàn)闆]有前導(dǎo)空格) 
  5.          ^ 
  6. 第 2 步:"4193 with words"(當(dāng)前沒有讀入字符,因?yàn)檫@里不存在 '-' 或者 '+') 
  7.          ^ 
  8. 第 3 步:"4193 with words"(讀入 "4193";由于下一個(gè)字符不是一個(gè)數(shù)字,所以讀入停止) 
  9.              ^ 
  10. 解析得到整數(shù) 4193 。 
  11. 由于 "4193" 在范圍 [-231, 231 - 1] 內(nèi),最終結(jié)果為 4193 。 

示例 4

  1. 輸入:str = "words and 987" 
  2. 輸出:0 
  3. 解釋: 
  4. 第 1 步:"words and 987"(當(dāng)前沒有讀入字符,因?yàn)闆]有前導(dǎo)空格) 
  5.          ^ 
  6. 第 2 步:"words and 987"(當(dāng)前沒有讀入字符,因?yàn)檫@里不存在 '-' 或者 '+') 
  7.          ^ 
  8. 第 3 步:"words and 987"(由于當(dāng)前字符 'w' 不是一個(gè)數(shù)字,所以讀入停止) 
  9.          ^ 
  10. 解析得到整數(shù) 0 ,因?yàn)闆]有讀入任何數(shù)字。 
  11. 由于 0 在范圍 [-231, 231 - 1] 內(nèi),最終結(jié)果為 0 。 

示例 5

  1. 輸入:str = "-91283472332" 
  2. 輸出:-2147483648 
  3. 解釋: 
  4. 第 1 步:"-91283472332"(當(dāng)前沒有讀入字符,因?yàn)闆]有前導(dǎo)空格) 
  5.          ^ 
  6. 第 2 步:"-91283472332"(讀入 '-' 字符,所以結(jié)果應(yīng)該是負(fù)數(shù)) 
  7.           ^ 
  8. 第 3 步:"-91283472332"(讀入 "91283472332") 
  9.                      ^ 
  10. 解析得到整數(shù) -91283472332 。 
  11. 由于 -91283472332 小于范圍 [-231, 231 - 1] 的下界,最終結(jié)果被截?cái)酁?nbsp;-231 = -2147483648 。 

約束條件:

  • 0 <= s.length <= 200
  • s 由英文字母(大寫和小寫)、數(shù)字(0-9)、' '、'+'、'-' 和 '.' 組成

3. 答案

  1. class Atoi { 
  2.     func myAtoi(_ str: String) -> Int { 
  3.         var res = 0, flag = 1, index = 0 
  4.         let intMax = 2147483647, intMin = -2147483648, strChars = Array(str) 
  5.          
  6.         // trim 
  7.         while index < strChars.count { 
  8.             let currentChar = strChars[index
  9.              
  10.             // trim 
  11.             guard currentChar.isWhitespace else { 
  12.                 break 
  13.             } 
  14.              
  15.             index += 1 
  16.         } 
  17.          
  18.         guard index < strChars.count else { 
  19.             return res 
  20.         } 
  21.          
  22.         // handle flag 
  23.         if strChars[index] == "-" { 
  24.             flag = -1 
  25.             index += 1 
  26.         } else if strChars[index] == "+" { 
  27.             index += 1 
  28.         } 
  29.          
  30.         // cast to number 
  31.         while index < strChars.count { 
  32.             let currentChar = strChars[index
  33.              
  34.             guard currentChar.isNumber else { 
  35.                 break 
  36.             } 
  37.              
  38.             res = res * 10 + currentChar.wholeNumberValue! 
  39.              
  40.             if res >= intMax { 
  41.                 if flag == 1 { 
  42.                     return intMax 
  43.                 } else if flag == -1 && res > intMax { 
  44.                     return intMin 
  45.                 } 
  46.             } 
  47.              
  48.             index += 1 
  49.         } 
  50.          
  51.         return flag * res 
  52.     } 
  • 主要思想:修剪,正和負(fù),整數(shù)溢出,是字符數(shù)字
  • 時(shí)間復(fù)雜度:O(n)
  • 空間復(fù)雜度:O(1)

該算法題解的倉庫:LeetCode-Swift[2]

點(diǎn)擊前往 LeetCode[3] 練習(xí)

參考資料

[1]@故胤道長: https://m.weibo.cn/u/1827884772

[2]LeetCode-Swift: https://github.com/soapyigu/LeetCode-Swift

[3]LeetCode: https://leetcode.com/problems/string-to-integer-atoi/

 

責(zé)任編輯:姜華 來源: Swift社區(qū)
相關(guān)推薦

2021-12-14 09:01:01

LeetCode整數(shù)羅馬數(shù)字

2021-01-14 08:23:15

LeetCode變量

2021-12-15 09:00:53

LeetCode 羅馬數(shù)字整數(shù)

2021-01-22 08:30:50

LeetCode數(shù)字數(shù)組

2021-01-21 08:23:29

鏈表單鏈表循環(huán)鏈表

2021-03-12 08:19:20

數(shù)組跳躍游戲

2021-12-31 09:01:44

LeetCode 羅馬數(shù)字四數(shù)之和

2023-09-08 09:38:59

2022-02-11 09:01:45

LeetCode函數(shù)括號(hào)生成

2024-06-14 08:54:54

2021-11-24 09:08:38

LeetCode字符串算法

2020-10-22 14:00:31

JavaScript數(shù)字變量

2020-10-22 08:06:05

JavaScrip語言類型

2016-12-29 17:14:41

回文串算法代碼

2021-02-04 08:18:53

LeetCode鏈表

2018-10-16 10:54:45

2016-08-29 16:20:27

戴爾

2022-05-07 15:35:38

數(shù)字化轉(zhuǎn)型企業(yè)效率

2022-02-16 09:12:22

LeetCode升序鏈表鏈表數(shù)組

2021-03-22 08:23:29

LeetCode二叉樹節(jié)點(diǎn)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)