diff --git a/solution/0900-0999/0989.Add to Array-Form of Integer/README.md b/solution/0900-0999/0989.Add to Array-Form of Integer/README.md index 4fd9b4c813827..917aded36ba01 100644 --- a/solution/0900-0999/0989.Add to Array-Form of Integer/README.md +++ b/solution/0900-0999/0989.Add to Array-Form of Integer/README.md @@ -71,7 +71,11 @@ tags: -### 方法一 +### 方法一:模拟 + +我们可以从数组的最后一位开始,将数组的每一位与 $k$ 相加,然后将 $k$ 除以 $10$,并将余数作为当前位的值,将商作为进位。一直循环,直到数组遍历完并且 $k = 0$。最后将答案数组反转即可。 + +时间复杂度 $O(n)$,其中 $n$ 表示 $\textit{num}$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。 @@ -80,13 +84,12 @@ tags: ```python class Solution: def addToArrayForm(self, num: List[int], k: int) -> List[int]: - i, carry = len(num) - 1, 0 ans = [] - while i >= 0 or k or carry: - carry += (0 if i < 0 else num[i]) + (k % 10) - carry, v = divmod(carry, 10) - ans.append(v) - k //= 10 + i = len(num) - 1 + while i >= 0 or k: + k += 0 if i < 0 else num[i] + k, x = divmod(k, 10) + ans.append(x) i -= 1 return ans[::-1] ``` @@ -96,14 +99,13 @@ class Solution: ```java class Solution { public List addToArrayForm(int[] num, int k) { - int i = num.length - 1, carry = 0; - LinkedList ans = new LinkedList<>(); - while (i >= 0 || k > 0 || carry > 0) { - carry += (i < 0 ? 0 : num[i--]) + k % 10; - ans.addFirst(carry % 10); - carry /= 10; + List ans = new ArrayList<>(); + for (int i = num.length - 1; i >= 0 || k > 0; --i) { + k += (i >= 0 ? num[i] : 0); + ans.add(k % 10); k /= 10; } + Collections.reverse(ans); return ans; } } @@ -115,15 +117,13 @@ class Solution { class Solution { public: vector addToArrayForm(vector& num, int k) { - int i = num.size() - 1, carry = 0; vector ans; - for (; i >= 0 || k || carry; --i) { - carry += (i < 0 ? 0 : num[i]) + k % 10; - ans.push_back(carry % 10); - carry /= 10; + for (int i = num.size() - 1; i >= 0 || k > 0; --i) { + k += (i >= 0 ? num[i] : 0); + ans.push_back(k % 10); k /= 10; } - reverse(ans.begin(), ans.end()); + ranges::reverse(ans); return ans; } }; @@ -132,22 +132,16 @@ public: #### Go ```go -func addToArrayForm(num []int, k int) []int { - i, carry := len(num)-1, 0 - ans := []int{} - for ; i >= 0 || k > 0 || carry > 0; i-- { +func addToArrayForm(num []int, k int) (ans []int) { + for i := len(num) - 1; i >= 0 || k > 0; i-- { if i >= 0 { - carry += num[i] + k += num[i] } - carry += k % 10 - ans = append(ans, carry%10) - carry /= 10 + ans = append(ans, k%10) k /= 10 } - for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 { - ans[i], ans[j] = ans[j], ans[i] - } - return ans + slices.Reverse(ans) + return } ``` @@ -155,17 +149,13 @@ func addToArrayForm(num []int, k int) []int { ```ts function addToArrayForm(num: number[], k: number): number[] { - let arr2 = [...String(k)].map(Number); - let ans = []; - let sum = 0; - while (num.length || arr2.length || sum) { - let a = num.pop() || 0, - b = arr2.pop() || 0; - sum += a + b; - ans.unshift(sum % 10); - sum = Math.floor(sum / 10); + const ans: number[] = []; + for (let i = num.length - 1; i >= 0 || k > 0; --i) { + k += i >= 0 ? num[i] : 0; + ans.push(k % 10); + k = Math.floor(k / 10); } - return ans; + return ans.reverse(); } ``` @@ -173,51 +163,23 @@ function addToArrayForm(num: number[], k: number): number[] { ```rust impl Solution { - pub fn add_to_array_form(num: Vec, mut k: i32) -> Vec { - let n = num.len(); - let mut res = vec![]; - let mut i = 0; - let mut sum = 0; - while i < n || sum != 0 || k != 0 { - sum += num.get(n - i - 1).unwrap_or(&0); - sum += k % 10; - res.push(sum % 10); - - i += 1; + pub fn add_to_array_form(num: Vec, k: i32) -> Vec { + let mut ans = Vec::new(); + let mut k = k; + let mut i = num.len() as i32 - 1; + + while i >= 0 || k > 0 { + if i >= 0 { + k += num[i as usize]; + } + ans.push(k % 10); k /= 10; - sum /= 10; + i -= 1; } - res.reverse(); - res - } -} -``` - - - - - - -### 方法二 - - - -#### TypeScript - -```ts -function addToArrayForm(num: number[], k: number): number[] { - const n = num.length; - const res = []; - let sum = 0; - for (let i = 0; i < n || sum !== 0 || k !== 0; i++) { - sum += num[n - i - 1] ?? 0; - sum += k % 10; - res.push(sum % 10); - k = Math.floor(k / 10); - sum = Math.floor(sum / 10); + ans.reverse(); + ans } - return res.reverse(); } ``` diff --git a/solution/0900-0999/0989.Add to Array-Form of Integer/README_EN.md b/solution/0900-0999/0989.Add to Array-Form of Integer/README_EN.md index 3e8c25c9fe657..b1cc71079ec0d 100644 --- a/solution/0900-0999/0989.Add to Array-Form of Integer/README_EN.md +++ b/solution/0900-0999/0989.Add to Array-Form of Integer/README_EN.md @@ -66,7 +66,11 @@ tags: -### Solution 1 +### Solution 1: Simulation + +We can start from the last digit of the array and add each digit of the array to $k$. Then, divide $k$ by $10$, and use the remainder as the current digit's value, with the quotient as the carry. Continue this process until the array is fully traversed and $k = 0$. Finally, reverse the answer array. + +The time complexity is $O(n)$, where $n$ is the length of $\textit{num}$. Ignoring the space consumption of the answer array, the space complexity is $O(1)$. @@ -75,13 +79,12 @@ tags: ```python class Solution: def addToArrayForm(self, num: List[int], k: int) -> List[int]: - i, carry = len(num) - 1, 0 ans = [] - while i >= 0 or k or carry: - carry += (0 if i < 0 else num[i]) + (k % 10) - carry, v = divmod(carry, 10) - ans.append(v) - k //= 10 + i = len(num) - 1 + while i >= 0 or k: + k += 0 if i < 0 else num[i] + k, x = divmod(k, 10) + ans.append(x) i -= 1 return ans[::-1] ``` @@ -91,14 +94,13 @@ class Solution: ```java class Solution { public List addToArrayForm(int[] num, int k) { - int i = num.length - 1, carry = 0; - LinkedList ans = new LinkedList<>(); - while (i >= 0 || k > 0 || carry > 0) { - carry += (i < 0 ? 0 : num[i--]) + k % 10; - ans.addFirst(carry % 10); - carry /= 10; + List ans = new ArrayList<>(); + for (int i = num.length - 1; i >= 0 || k > 0; --i) { + k += (i >= 0 ? num[i] : 0); + ans.add(k % 10); k /= 10; } + Collections.reverse(ans); return ans; } } @@ -110,15 +112,13 @@ class Solution { class Solution { public: vector addToArrayForm(vector& num, int k) { - int i = num.size() - 1, carry = 0; vector ans; - for (; i >= 0 || k || carry; --i) { - carry += (i < 0 ? 0 : num[i]) + k % 10; - ans.push_back(carry % 10); - carry /= 10; + for (int i = num.size() - 1; i >= 0 || k > 0; --i) { + k += (i >= 0 ? num[i] : 0); + ans.push_back(k % 10); k /= 10; } - reverse(ans.begin(), ans.end()); + ranges::reverse(ans); return ans; } }; @@ -127,22 +127,16 @@ public: #### Go ```go -func addToArrayForm(num []int, k int) []int { - i, carry := len(num)-1, 0 - ans := []int{} - for ; i >= 0 || k > 0 || carry > 0; i-- { +func addToArrayForm(num []int, k int) (ans []int) { + for i := len(num) - 1; i >= 0 || k > 0; i-- { if i >= 0 { - carry += num[i] + k += num[i] } - carry += k % 10 - ans = append(ans, carry%10) - carry /= 10 + ans = append(ans, k%10) k /= 10 } - for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 { - ans[i], ans[j] = ans[j], ans[i] - } - return ans + slices.Reverse(ans) + return } ``` @@ -150,17 +144,13 @@ func addToArrayForm(num []int, k int) []int { ```ts function addToArrayForm(num: number[], k: number): number[] { - let arr2 = [...String(k)].map(Number); - let ans = []; - let sum = 0; - while (num.length || arr2.length || sum) { - let a = num.pop() || 0, - b = arr2.pop() || 0; - sum += a + b; - ans.unshift(sum % 10); - sum = Math.floor(sum / 10); + const ans: number[] = []; + for (let i = num.length - 1; i >= 0 || k > 0; --i) { + k += i >= 0 ? num[i] : 0; + ans.push(k % 10); + k = Math.floor(k / 10); } - return ans; + return ans.reverse(); } ``` @@ -168,51 +158,23 @@ function addToArrayForm(num: number[], k: number): number[] { ```rust impl Solution { - pub fn add_to_array_form(num: Vec, mut k: i32) -> Vec { - let n = num.len(); - let mut res = vec![]; - let mut i = 0; - let mut sum = 0; - while i < n || sum != 0 || k != 0 { - sum += num.get(n - i - 1).unwrap_or(&0); - sum += k % 10; - res.push(sum % 10); - - i += 1; + pub fn add_to_array_form(num: Vec, k: i32) -> Vec { + let mut ans = Vec::new(); + let mut k = k; + let mut i = num.len() as i32 - 1; + + while i >= 0 || k > 0 { + if i >= 0 { + k += num[i as usize]; + } + ans.push(k % 10); k /= 10; - sum /= 10; + i -= 1; } - res.reverse(); - res - } -} -``` - - - - - - -### Solution 2 - - - -#### TypeScript - -```ts -function addToArrayForm(num: number[], k: number): number[] { - const n = num.length; - const res = []; - let sum = 0; - for (let i = 0; i < n || sum !== 0 || k !== 0; i++) { - sum += num[n - i - 1] ?? 0; - sum += k % 10; - res.push(sum % 10); - k = Math.floor(k / 10); - sum = Math.floor(sum / 10); + ans.reverse(); + ans } - return res.reverse(); } ``` diff --git a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.cpp b/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.cpp index c13bcf685c42a..75020e8919720 100644 --- a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.cpp +++ b/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.cpp @@ -1,15 +1,13 @@ class Solution { public: vector addToArrayForm(vector& num, int k) { - int i = num.size() - 1, carry = 0; vector ans; - for (; i >= 0 || k || carry; --i) { - carry += (i < 0 ? 0 : num[i]) + k % 10; - ans.push_back(carry % 10); - carry /= 10; + for (int i = num.size() - 1; i >= 0 || k > 0; --i) { + k += (i >= 0 ? num[i] : 0); + ans.push_back(k % 10); k /= 10; } - reverse(ans.begin(), ans.end()); + ranges::reverse(ans); return ans; } -}; \ No newline at end of file +}; diff --git a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.go b/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.go index b5179f7112072..fe09c27577d42 100644 --- a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.go +++ b/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.go @@ -1,17 +1,11 @@ -func addToArrayForm(num []int, k int) []int { - i, carry := len(num)-1, 0 - ans := []int{} - for ; i >= 0 || k > 0 || carry > 0; i-- { +func addToArrayForm(num []int, k int) (ans []int) { + for i := len(num) - 1; i >= 0 || k > 0; i-- { if i >= 0 { - carry += num[i] + k += num[i] } - carry += k % 10 - ans = append(ans, carry%10) - carry /= 10 + ans = append(ans, k%10) k /= 10 } - for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 { - ans[i], ans[j] = ans[j], ans[i] - } - return ans -} \ No newline at end of file + slices.Reverse(ans) + return +} diff --git a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.java b/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.java index 7d282c33fa936..757cc101f4636 100644 --- a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.java +++ b/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.java @@ -1,13 +1,12 @@ class Solution { public List addToArrayForm(int[] num, int k) { - int i = num.length - 1, carry = 0; - LinkedList ans = new LinkedList<>(); - while (i >= 0 || k > 0 || carry > 0) { - carry += (i < 0 ? 0 : num[i--]) + k % 10; - ans.addFirst(carry % 10); - carry /= 10; + List ans = new ArrayList<>(); + for (int i = num.length - 1; i >= 0 || k > 0; --i) { + k += (i >= 0 ? num[i] : 0); + ans.add(k % 10); k /= 10; } + Collections.reverse(ans); return ans; } -} \ No newline at end of file +} diff --git a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.py b/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.py index 178526d773133..11ac3332b8bcf 100644 --- a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.py +++ b/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.py @@ -1,11 +1,10 @@ class Solution: def addToArrayForm(self, num: List[int], k: int) -> List[int]: - i, carry = len(num) - 1, 0 ans = [] - while i >= 0 or k or carry: - carry += (0 if i < 0 else num[i]) + (k % 10) - carry, v = divmod(carry, 10) - ans.append(v) - k //= 10 + i = len(num) - 1 + while i >= 0 or k: + k += 0 if i < 0 else num[i] + k, x = divmod(k, 10) + ans.append(x) i -= 1 return ans[::-1] diff --git a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.rs b/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.rs index ea04dd2bf9a5d..b064f99ecc70a 100644 --- a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.rs +++ b/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.rs @@ -1,19 +1,19 @@ impl Solution { - pub fn add_to_array_form(num: Vec, mut k: i32) -> Vec { - let n = num.len(); - let mut res = vec![]; - let mut i = 0; - let mut sum = 0; - while i < n || sum != 0 || k != 0 { - sum += num.get(n - i - 1).unwrap_or(&0); - sum += k % 10; - res.push(sum % 10); + pub fn add_to_array_form(num: Vec, k: i32) -> Vec { + let mut ans = Vec::new(); + let mut k = k; + let mut i = num.len() as i32 - 1; - i += 1; + while i >= 0 || k > 0 { + if i >= 0 { + k += num[i as usize]; + } + ans.push(k % 10); k /= 10; - sum /= 10; + i -= 1; } - res.reverse(); - res + + ans.reverse(); + ans } } diff --git a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.ts b/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.ts index ce67313ec8fba..c6d63e5393494 100644 --- a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.ts +++ b/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.ts @@ -1,13 +1,9 @@ function addToArrayForm(num: number[], k: number): number[] { - let arr2 = [...String(k)].map(Number); - let ans = []; - let sum = 0; - while (num.length || arr2.length || sum) { - let a = num.pop() || 0, - b = arr2.pop() || 0; - sum += a + b; - ans.unshift(sum % 10); - sum = Math.floor(sum / 10); + const ans: number[] = []; + for (let i = num.length - 1; i >= 0 || k > 0; --i) { + k += i >= 0 ? num[i] : 0; + ans.push(k % 10); + k = Math.floor(k / 10); } - return ans; + return ans.reverse(); } diff --git a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution2.ts b/solution/0900-0999/0989.Add to Array-Form of Integer/Solution2.ts deleted file mode 100644 index b30c3171765fc..0000000000000 --- a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution2.ts +++ /dev/null @@ -1,13 +0,0 @@ -function addToArrayForm(num: number[], k: number): number[] { - const n = num.length; - const res = []; - let sum = 0; - for (let i = 0; i < n || sum !== 0 || k !== 0; i++) { - sum += num[n - i - 1] ?? 0; - sum += k % 10; - res.push(sum % 10); - k = Math.floor(k / 10); - sum = Math.floor(sum / 10); - } - return res.reverse(); -} diff --git a/solution/0900-0999/0991.Broken Calculator/README.md b/solution/0900-0999/0991.Broken Calculator/README.md index c87a703ac0728..de80e2af28210 100644 --- a/solution/0900-0999/0991.Broken Calculator/README.md +++ b/solution/0900-0999/0991.Broken Calculator/README.md @@ -68,9 +68,9 @@ tags: ### 方法一:逆向计算 -我们可以采用逆向计算的方式,从 `target` 开始,如果 `target` 是奇数,则 `target++`,否则 `target >>= 1`,累加操作次数,直到 `target` 小于等于 `startValue`,此时的操作次数加上 `startValue - target` 即为最终结果。 +我们可以采用逆向计算的方式,从 $\textit{target}$ 开始,如果 $\textit{target}$ 是奇数,那么 $\textit{target} = \textit{target} + 1$,否则 $\textit{target} = \textit{target} / 2$,累加操作次数,直到 $\textit{target} \leq \textit{startValue}$,此时的操作次数加上 $\textit{startValue} - \textit{target}$ 即为最终结果。 -时间复杂度 $O(\log n)$,其中 $n$ 为 `target`。空间复杂度 $O(1)$。 +时间复杂度 $O(\log n)$,其中 $n$ 为 $\textit{target}$。空间复杂度 $O(1)$。 @@ -148,6 +148,23 @@ func brokenCalc(startValue int, target int) (ans int) { } ``` +#### TypeScript + +```ts +function brokenCalc(startValue: number, target: number): number { + let ans = 0; + for (; startValue < target; ++ans) { + if (target & 1) { + ++target; + } else { + target >>= 1; + } + } + ans += startValue - target; + return ans; +} +``` + diff --git a/solution/0900-0999/0991.Broken Calculator/README_EN.md b/solution/0900-0999/0991.Broken Calculator/README_EN.md index c30183b147852..33e7b39712a68 100644 --- a/solution/0900-0999/0991.Broken Calculator/README_EN.md +++ b/solution/0900-0999/0991.Broken Calculator/README_EN.md @@ -64,7 +64,11 @@ tags: -### Solution 1 +### Solution 1: Reverse Calculation + +We can use a reverse calculation method, starting from $\textit{target}$. If $\textit{target}$ is odd, then $\textit{target} = \textit{target} + 1$, otherwise $\textit{target} = \textit{target} / 2$. We accumulate the number of operations until $\textit{target} \leq \textit{startValue}$. The final result is the number of operations plus $\textit{startValue} - \textit{target}$. + +The time complexity is $O(\log n)$, where $n$ is $\textit{target}$. The space complexity is $O(1)$. @@ -142,6 +146,23 @@ func brokenCalc(startValue int, target int) (ans int) { } ``` +#### TypeScript + +```ts +function brokenCalc(startValue: number, target: number): number { + let ans = 0; + for (; startValue < target; ++ans) { + if (target & 1) { + ++target; + } else { + target >>= 1; + } + } + ans += startValue - target; + return ans; +} +``` + diff --git a/solution/0900-0999/0991.Broken Calculator/Solution.ts b/solution/0900-0999/0991.Broken Calculator/Solution.ts new file mode 100644 index 0000000000000..a9a57917272a3 --- /dev/null +++ b/solution/0900-0999/0991.Broken Calculator/Solution.ts @@ -0,0 +1,12 @@ +function brokenCalc(startValue: number, target: number): number { + let ans = 0; + for (; startValue < target; ++ans) { + if (target & 1) { + ++target; + } else { + target >>= 1; + } + } + ans += startValue - target; + return ans; +}