ðŸ¥ˆ

# String to Integer (atoi)

Created
2022/01/14 01:56
ë¬¸ì œ ë²ˆí˜¸
8
ì¹´í…Œê³ ë¦¬
String

### Code

 .css-15tnwsa{max-width:100%;width:100%;white-space:pre-wrap;word-break:break-word;padding:7px 9px;background-color:transparent;font-size:14px;line-height:20px;min-height:1em;}.css-15tnwsa:empty::after{content:" ";}ì œì¶œ ë‚ ì§œ ì‹œê°„ ë©”ëª¨ë¦¬ 2021/01/14 0 ms 2.4 MB
// 8. String to Integer (atoi) // // https://leetcode.com/problems/string-to-integer-atoi/ // math package has been imported to use Min and Max value of int32. // unicode package has been imported to check the specific rune. import ( "math" "unicode" ) // myAtoi function converts string to int. // Some points are different from Atoi in strconv. // The logic of this function consists of skipping white spaces, checking sign, appending the runes to int. // If the converted number is overflowed or underflowed more than the 4 bytes number, return Min or Max number of int32. func myAtoi(s string) int { sign := 1 val := 0 pos := 0 for pos < len(s) && unicode.IsSpace(rune(s[pos])) { pos++ } if pos == len(s) { return 0 } if s[pos] == '-' || s[pos] == '+' { sign *= (int(s[pos]) - ',') * -1 pos++ } for pos < len(s) && unicode.IsDigit(rune(s[pos])) { val = val*10 + int(s[pos]-'0') if val*sign < math.MinInt32 { return math.MinInt32 } else if val*sign > math.MaxInt32 { return math.MaxInt32 } pos++ } return val * sign }
Go
ë³µì‚¬