-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPlus One.swift
38 lines (32 loc) · 951 Bytes
/
Plus One.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
//
// Plus One.swift
// ProblemSolving_LeetCode
//
// Created by Avneesh on 14/03/21.
//
import Foundation
/*
Given a non-empty array of decimal digits representing a non-negative integer, increment one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.
*/
class Solution {
func plusOne(_ digits: [Int]) -> [Int] {
let arrSize = digits.count
var result = digits
var index = arrSize - 1
if digits[index] == 9 {
while (index >= 0 && digits[index] == 9) {
result[index] = 0
index -= 1
}
if index == -1 {
result.insert(1, at: 0)
} else {
result[index] += 1
}
} else {
result[index] += 1
}
return result
}
}