Go/Leet Code
Array and String - Plus One in Go
Gopythor
2022. 3. 26. 05:43
728x90
반응형
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:
- 1 <= digits.length <= 100
- 0 <= digits[i] <= 9
- digits does not contain any leading 0's.
My code
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
}
- This problem is just adding one to given number.
- Each number is allocated to each array.
- As we calculate add, I incremente end of array(unit digit).
- Buffer will be used as round up.
- For loop will run from end to first.
- First if condition checks buffer is true.
- When is true, it will increament with changing buffer to false.
- Second if condition Checks array[i] is 10 or not.
- When array[i] is 10, that value will be 0, and buffer will be true.
- After for loop, third if condition will check buffer is true or not
- When buffer is true, temp slice will be made and append 1.
- After this, original array will be added.
sample 0 ms submission & sample 2000 KB submission
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
}
- L will get a value which is added by 1(considering one more space for round up).
- It checks array has the value whether it is smaller than 9 or not.
- If first array is smaller than 9, L will decrese and have same length with original array.
- if first array is same with 9, L has same value as allocated.
- In for loop, i and j will have end of arrays.
- sum[j] will have reminder of 10 after adding c.
- c will have quotient after adding c.
- j will decrease.
- After for loop, if confidion will be checked and if it will be greater than 0, sum[0] will have c.
- return sum.
sample 1 ms submission
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
}
- This code looks so simple.
- For loop is running from end to start.
- If condition checks whether end of digits is greater than 9 or not.
- If less than 9, end of digits will increase and return digits.
- if the value is same with 9, the value will be 0.
- because it was not returned, digits[i] will be checked again.
- after for loop ends, result slice will be made and digits will be appended.
- result will be returned.
sample 2 ms submission
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
}
- carry is used for adding.
- for loop is running from the end.
- If condition will check the value is same with 9 or not.
- when the value is 9, carry will be 1 again( I don't think carry does not need to be allocated.)
- and number will increase.
- if the value is not same with 9, digits will increase as many as carry and return digits.
- when for loop ends, temp slice will be made and digits will be appended.
- and return temp.
Modification
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
}
- carry just declared once and no changes.
- declearing temp and adppending digits are merged.
sample 3 ms submission & sample 2100 KB submission
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
}
- This code looks complicated.
- Index will have length of digits.
- num will have end of array added by 1.
- and it return to end of array.
- for loop will run when num is 10.
- end of array will be 0 after minus 10.
- index will decrease
- if condition checks whether index is 0.
- when is not 0, array will increase and allocate it to num.
- num also gives its value to array.
- anytime num is not 10, it will return digits
- or not, it will do same looping.
- when index is 0, digits will have array added one array to the start.
728x90
반응형