aboutsummaryrefslogtreecommitdiffstats
path: root/989
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2023-02-15 14:34:06 +0000
committerterminaldweller <thabogre@gmail.com>2023-02-15 14:34:06 +0000
commit5b13a159ae118bbcf2946588805e119c8791465d (patch)
tree32b9959b6c7d2f96c4635980d48e4b28017bb796 /989
parent67 (diff)
downloadleetcode-5b13a159ae118bbcf2946588805e119c8791465d.tar.gz
leetcode-5b13a159ae118bbcf2946588805e119c8791465d.zip
989
Diffstat (limited to '989')
-rw-r--r--989/go.mod3
-rw-r--r--989/main.go67
2 files changed, 70 insertions, 0 deletions
diff --git a/989/go.mod b/989/go.mod
new file mode 100644
index 0000000..92068a8
--- /dev/null
+++ b/989/go.mod
@@ -0,0 +1,3 @@
+module 989
+
+go 1.19
diff --git a/989/main.go b/989/main.go
new file mode 100644
index 0000000..3bbc590
--- /dev/null
+++ b/989/main.go
@@ -0,0 +1,67 @@
+package main
+
+import (
+ "fmt"
+ "math"
+)
+
+func getNthDigit(num, k int) int {
+ result := num
+ if k-1 > 0 {
+ result -= num - (num/int(math.Pow10(k-1)))*int(math.Pow10(k-1))
+ }
+
+ if math.Pow10(k) <= float64(num) {
+ result -= (num / int(math.Pow10(k))) * int(math.Pow10(k))
+ }
+ // result := num - (num/int(math.Pow10(k)))*int(math.Pow10(k)) - (num/int(math.Pow10(k-2)))*int(math.Pow10(k-2))
+
+ result /= int(math.Pow10(k - 1))
+ // fmt.Println(num, k, result)
+ return result
+}
+
+func addToArrayForm(num []int, k int) []int {
+ length := int(math.Max(float64(len(num))+1., 5.))
+ result := make([]int, length)
+ carry := false
+ num_index := len(num) - 1
+
+ var sum int
+
+ for i := length - 1; i >= 0; i-- {
+ if num_index >= 0 {
+ sum = num[num_index] + getNthDigit(k, (length-i))
+ } else {
+ sum = getNthDigit(k, (length - i))
+ }
+ if carry {
+ sum++
+ }
+ if sum >= 10 {
+ carry = true
+ } else {
+ carry = false
+ }
+ result[i] = sum % 10
+ num_index--
+ }
+
+ if carry {
+ result[0] = 1
+ }
+
+ for j := 0; j < length; j++ {
+ if result[j] != 0 {
+ return result[j:]
+ }
+ }
+ return result
+}
+
+func main() {
+ fmt.Println(addToArrayForm([]int{1, 2, 0, 0}, 34))
+ fmt.Println(addToArrayForm([]int{2, 7, 4}, 181))
+ fmt.Println(addToArrayForm([]int{2, 1, 5}, 806))
+ fmt.Println(addToArrayForm([]int{0}, 806))
+}