-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReverseInteger.kt
36 lines (33 loc) · 944 Bytes
/
ReverseInteger.kt
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
class ReverseInteger {
fun reverse(x: Int): Int{
if(x==0) return 0
val number = x.toString().reversed()
var newString = ""
var invalid = true
for (i in number){
if (invalid){
if (i == '0'){
continue
} else {
invalid = false
newString = newString + i
}
} else {
if(i!='-'){
newString = newString + i
}
}
}
if(isInIntRange(newString.toLong())){
return if (isNegative(x)) "-$newString".toInt() else newString.toInt()
} else {
return 0
}
}
private fun isNegative(x: Int): Boolean{
return x<0
}
private fun isInIntRange(value: Long) : Boolean {
return if(value>2147483647 || value<-2147483648) false else true
}
}