LeetCode - 8. string conversion integer (Swift Language)


Atoi you to implement a function, it can convert a string to an integer.

First, the function will begin with a space character discard useless if necessary, until the find to the first non-space character so far.
When we find the first non-space character is a positive or negative number, the combination of the symbols as much as possible with consecutive numbers up later, as the sign of integer; if the first non-space character is figures, which directly after the continuous numeric characters are combined to form an integer.
In addition to the string after a valid integer part may also exist extra characters, these characters can be ignored, they should not affect a function.
Note: if the character string in the first non-space character is not a valid integer character string is empty or contains only white space character string, then you will not need to be a function of conversion.
In any case, if the function can not effectively convert, 0 is returned.
Description:
We assume that the size of the environment can store 32-bit signed integer, then the value range of [-231 231--1]. If the value exceeds this range, qing return INT_MAX (231 - 1) or INT_MIN (-231).

Example 1:

1
2
Input: "42" 
Output: 42

Example 2:

1
2
3
4
Input: "-42" 
Output: -42
Explanation: a first non-blank character '-', it is a negative sign.
We will all digital consecutive negative number and later combined as much as possible, and finally get -42.

Example 3:

1
2
3
Input: "41931 with words" 
Output: 41931
explanation: the digital conversion off '1', since it is not the next character is a number.

Example 4:

1
2
3
4
Input: "words and 987" 
Output: 0
Explanation: a first non-blank character 'w', but it is not a positive number or negative number.
Therefore, the conversion can not be performed effectively.

Example 5:

1
2
3
4
Input: "-91283472332" 
Output: -2147483648
explanation: the number "-91283472332" Over the range of 32-bit signed integer.
So return INT_MIN (-231).
  • Note:
    Suppose we have an environment can store the 32-bit signed integer, then the value range of [-231 231--1]. Please According to this hypothesis, if integer overflow after reverse it returns 0.

Swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class LeetCode8 {

/* 检查用例
"42"
"+-2"
" -42"
"words and 987"
"0-1"
*/
func myAtoi(_ str: String) -> Int {

// intermediate transition value can only be nil type
var ISNUM:? Int = nil
// final gross
var Results: Int = 0
// whether negative?
isMinus var:? Bool = nil for A in str { the let S = String (A) // NUM == nil whether the character has been // isMinus == nil whether there had been operator if isNum == nil && isMinus == && nil (S == "" || S == "-" || S == "+") { // operator // disguised space taken if s == "-" || s == "+" { isMinus = S == "-" } Continue } // Int able to turn indicates 0 ...















let ii = isMinus ?? false ? -1 : 1
results = isNum ?? 0
results = results * 10 + i * ii
isNum = results
if results > Int32.max {
return Int(Int32.max)
}
if results < Int32.min {
return Int(Int32.min)
}
}else{
break
}
}
return results
}
}

Original: Large Column  LeetCode - 8. string conversion integer (Swift Language)


Guess you like

Origin www.cnblogs.com/chinatrump/p/11589036.html