-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlusOne.kt
29 lines (27 loc) · 956 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
package problems
class PlusOne {
//I could have used LinkedList but since return type is IntArray it is inefficient to convert linedlist to IntArray all the time.
//Only full carry requires extra space, so I can recycle given digit array.
fun plusOne(digits: IntArray): IntArray {
var isCarry = 0
for (i in digits.size - 1 downTo 0) {
val added = if (i == digits.size - 1) digits[i] + isCarry + 1 else digits[i] + isCarry
if (added >= 10) {
isCarry = 1
digits[i] = added - 10
} else {
isCarry = 0
digits[i] = added
}
}
if (isCarry == 1) {
val carryDigit = IntArray(digits.size + 1)
carryDigit[0] = 1
for (i in 1 until digits.size) {
carryDigit[i] = digits[i - 1]
}
return carryDigit
}
return digits
}
}