-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringToInteger.scala
42 lines (35 loc) · 1.17 KB
/
StringToInteger.scala
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
package StringToInteger
object Solution {
def myAtoi(str: String): Int = {
var p = str.trim
var r = BigInt(0)
if (p.isEmpty()) 0
else if (p.length() == 1) {
if (p(0) < '0' || p(0) > '9') 0 else p(0) - '0'
}
else {
if ((p(0) == '-' || p(0) == '+')&& (p(1) < '0' || p(1) > '9')) 0
else {
var tag = 1
if (p(0) == '-') { tag = -1; p = p.tail }
if (p(0) == '+') {p = p.tail}
while(p.nonEmpty && p(0) <= '9' && p(0) >= '0') {
r = r * 10 + p(0) - '0'
if (r*tag > Int.MaxValue) { r = Int.MaxValue; p = "" }
if (r * tag < Int.MinValue) { r = Int.MinValue; p = ""}
p = p.tail
}
r.toInt * tag
}
}
}
def main(args: Array[String]) = {
println(myAtoi("42"))
println(myAtoi("-42"))
println(myAtoi("+42fdsfdsds"))
println(myAtoi(" -42fdsfdsds"))
println(myAtoi("-fdsfdsds"))
println(myAtoi("-fdsfdsds 4343"))
println(myAtoi("45fdsfdsds 4343"))
}
}