You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
Example 1:
Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].
Example 2:
Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
Incrementing by one gives 4321 + 1 = 4322.
Thus, the result should be [4,3,2,2].
Example 3:
Input: digits = [9]
Output: [1,0]
Explanation: The array represents the integer 9.
Incrementing by one gives 9 + 1 = 10.
Thus, the result should be [1,0].
Constraints:
func plusOne(digits []int) []int {
length := len(digits)
digits[length-1]++
buffer := false
for i := length - 1; i >= 0; i-- {
if buffer {
digits[i]++
buffer = false
}
if digits[i] == 10 {
digits[i] = 0
buffer = true
}
}
if buffer {
temp := make([]int, 0)
temp = append(temp, 1)
temp = append(temp, digits...)
return temp
}
return digits
}
func plusOne(digits []int) []int {
l := len(digits)+1
for _, d := range digits {
if d < 9 {
l--
break
}
}
sum := make([]int, l)
c := 1
for i,j := len(digits)-1, len(sum)-1; i >= 0; i-- {
//fmt.Println(i, j, digits[i], sum[j],c)
sum[j] = (digits[i] + c)%10
c = (digits[i] + c)/10
j--
}
if c > 0 {
sum[0] = c
}
return sum
}
func plusOne(digits []int) []int {
for i := len(digits) - 1; i >= 0; i-- {
if digits[i] < 9 {
digits[i]++
return digits
}
digits[i] = 0
}
result := append([]int{1}, digits...)
return result
}
func plusOne(digits []int) []int {
// Inital carry =1 since we want to Plus One
carry := 1
n := len(digits)
// traverse array in reverse since we do addition that way (add to last digit first)
for i := n - 1; i >= 0; i-- {
// if digit is 9, there will be carry situation, current digit becones 0 and we carry 1
if digits[i] == 9 {
carry = 1
digits[i] = 0
} else {
// if number is <9, no carry situation, we can simply plus 1 and return
digits[i] = digits[i] + carry
return digits
}
}
// case when input is [9,9,9], in this case number of digits in array increase due to extra carry digit
temp := []int{1}
if carry == 1 {
temp = append(temp, digits...)
}
return temp
}
func plusOne(digits []int) []int {
// Inital carry =1 since we want to Plus One
carry := 1
n := len(digits)
// traverse array in reverse since we do addition that way (add to last digit first)
for i := n - 1; i >= 0; i-- {
// if digit is 9, there will be carry situation, current digit becones 0 and we carry 1
if digits[i] == 9 {
digits[i] = 0
} else {
// if number is <9, no carry situation, we can simply plus 1 and return
digits[i] = digits[i] + carry
return digits
}
}
// case when input is [9,9,9], in this case number of digits in array increase due to extra carry digit
temp := append([]int{1}, digits...)
return temp
}
func plusOne(digits []int) []int {
index := len(digits)
num := digits[index - 1] + 1;
digits[index - 1] = num;
for num >= 10 {
digits[index - 1] -= 10;
index -= 1;
if index == 0 {
digits = append([]int{1}, digits...);
break;
} else {
num = digits[index - 1] + 1;
digits[index - 1] = num;
}
}
return digits
}
[Go] Array and string - Diagonal Traverse (1) | 2022.03.27 |
---|---|
Array and string - Introduction to 2D Array from Java to Go (1) | 2022.03.27 |
Array and String - Largest Number At Least Twice of Others in Go (0) | 2022.03.24 |
Array and String - Find Pivot Index in Go (0) | 2022.03.24 |
Array and string - Introduction to Dynamic Array from Java to Go (0) | 2022.03.23 |
댓글 영역