LeetCode之判斷回文數(shù)字
前言
我們社區(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
- 輸入:str = "42"
- 輸出:42
- 解釋:加粗的字符串為已經(jīng)讀入的字符,插入符號(hào)是當(dāng)前讀取的字符。
- 第 1 步:"42"(當(dāng)前沒有讀入字符,因?yàn)闆]有前導(dǎo)空格)
- ^
- 第 2 步:"42"(當(dāng)前沒有讀入字符,因?yàn)檫@里不存在 '-' 或者 '+')
- ^
- 第 3 步:"42"(讀入 "42")
- ^
- 解析得到整數(shù) 42 。
- 由于 "42" 在范圍 [-231, 231 - 1] 內(nèi),最終結(jié)果為 42 。
示例 2
- 輸入:str = "-42"
- 輸出:-42
- 解釋:
- 第 1 步:" -42"(讀入前導(dǎo)空格,但忽視掉)
- ^
- 第 2 步:" -42"(讀入 '-' 字符,所以結(jié)果應(yīng)該是負(fù)數(shù))
- ^
- 第 3 步:" -42"(讀入 "42")
- ^
- 解析得到整數(shù) -42 。
- 由于 "-42" 在范圍 [-231, 231 - 1] 內(nèi),最終結(jié)果為 -42 。
示例 3
- 輸入:str = "4193 with words"
- 輸出:4193
- 解釋:
- 第 1 步:"4193 with words"(當(dāng)前沒有讀入字符,因?yàn)闆]有前導(dǎo)空格)
- ^
- 第 2 步:"4193 with words"(當(dāng)前沒有讀入字符,因?yàn)檫@里不存在 '-' 或者 '+')
- ^
- 第 3 步:"4193 with words"(讀入 "4193";由于下一個(gè)字符不是一個(gè)數(shù)字,所以讀入停止)
- ^
- 解析得到整數(shù) 4193 。
- 由于 "4193" 在范圍 [-231, 231 - 1] 內(nèi),最終結(jié)果為 4193 。
示例 4
- 輸入:str = "words and 987"
- 輸出:0
- 解釋:
- 第 1 步:"words and 987"(當(dāng)前沒有讀入字符,因?yàn)闆]有前導(dǎo)空格)
- ^
- 第 2 步:"words and 987"(當(dāng)前沒有讀入字符,因?yàn)檫@里不存在 '-' 或者 '+')
- ^
- 第 3 步:"words and 987"(由于當(dāng)前字符 'w' 不是一個(gè)數(shù)字,所以讀入停止)
- ^
- 解析得到整數(shù) 0 ,因?yàn)闆]有讀入任何數(shù)字。
- 由于 0 在范圍 [-231, 231 - 1] 內(nèi),最終結(jié)果為 0 。
示例 5
- 輸入:str = "-91283472332"
- 輸出:-2147483648
- 解釋:
- 第 1 步:"-91283472332"(當(dāng)前沒有讀入字符,因?yàn)闆]有前導(dǎo)空格)
- ^
- 第 2 步:"-91283472332"(讀入 '-' 字符,所以結(jié)果應(yīng)該是負(fù)數(shù))
- ^
- 第 3 步:"-91283472332"(讀入 "91283472332")
- ^
- 解析得到整數(shù) -91283472332 。
- 由于 -91283472332 小于范圍 [-231, 231 - 1] 的下界,最終結(jié)果被截?cái)酁?nbsp;-231 = -2147483648 。
約束條件:
- 0 <= s.length <= 200
- s 由英文字母(大寫和小寫)、數(shù)字(0-9)、' '、'+'、'-' 和 '.' 組成
3. 答案
- class Atoi {
- func myAtoi(_ str: String) -> Int {
- var res = 0, flag = 1, index = 0
- let intMax = 2147483647, intMin = -2147483648, strChars = Array(str)
- // trim
- while index < strChars.count {
- let currentChar = strChars[index]
- // trim
- guard currentChar.isWhitespace else {
- break
- }
- index += 1
- }
- guard index < strChars.count else {
- return res
- }
- // handle flag
- if strChars[index] == "-" {
- flag = -1
- index += 1
- } else if strChars[index] == "+" {
- index += 1
- }
- // cast to number
- while index < strChars.count {
- let currentChar = strChars[index]
- guard currentChar.isNumber else {
- break
- }
- res = res * 10 + currentChar.wholeNumberValue!
- if res >= intMax {
- if flag == 1 {
- return intMax
- } else if flag == -1 && res > intMax {
- return intMin
- }
- }
- index += 1
- }
- return flag * res
- }
- }
- 主要思想:修剪,正和負(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/