-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathPlusOne.kt
executable file
·34 lines (29 loc) · 908 Bytes
/
PlusOne.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
/**
* Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
* You may assume the integer do not contain any leading zero, except the number 0 itself.
* The digits are stored such that the most significant digit is at the head of the list.
*
* Accepted.
*/
class PlusOne {
fun plusOne(digits: IntArray): IntArray {
var flag = false
digits[digits.size - 1]++
for (i in digits.indices.reversed()) {
if (flag) digits[i]++
if (digits[i] >= 10) {
flag = true
digits[i] %= 10
} else {
flag = false
}
}
if (flag) {
val result = IntArray(digits.size + 1)
result[0] = 1
System.arraycopy(digits, 0, result, 1, digits.size)
return result
}
return digits
}
}