From 38dbc6e4af5f7cc2a725fc4f8cf023f2154b22de Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 2 Oct 2025 09:48:51 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3697 --- .../README.md | 91 ++++++++++++++++++- .../README_EN.md | 83 ++++++++++++++++- .../Solution.cpp | 17 ++++ .../Solution.go | 14 +++ .../Solution.java | 20 ++++ .../Solution.py | 11 +++ .../Solution.ts | 14 +++ 7 files changed, 243 insertions(+), 7 deletions(-) create mode 100644 solution/3600-3699/3697.Compute Decimal Representation/Solution.cpp create mode 100644 solution/3600-3699/3697.Compute Decimal Representation/Solution.go create mode 100644 solution/3600-3699/3697.Compute Decimal Representation/Solution.java create mode 100644 solution/3600-3699/3697.Compute Decimal Representation/Solution.py create mode 100644 solution/3600-3699/3697.Compute Decimal Representation/Solution.ts diff --git a/solution/3600-3699/3697.Compute Decimal Representation/README.md b/solution/3600-3699/3697.Compute Decimal Representation/README.md index aee85a96fa70b..c45c01d806045 100644 --- a/solution/3600-3699/3697.Compute Decimal Representation/README.md +++ b/solution/3600-3699/3697.Compute Decimal Representation/README.md @@ -74,32 +74,115 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3697.Co -### 方法一 +### 方法一:模拟 + +我们可以不断地对 $n$ 进行取模和整除操作,取模得到的值乘以当前的位值 $p$ 就是一个 10 进制分量。如果取模的结果不为 $0$,我们就将这个分量加入答案中。然后我们将 $p$ 乘以 $10$,继续处理下一个位。 + +最后,我们将答案反转,使其按降序排列。 + +时间复杂度 $O(\log n)$,其中 $n$ 是输入的正整数。空间复杂度 $O(\log n)$,用于存储答案。 #### Python3 ```python - +class Solution: + def decimalRepresentation(self, n: int) -> List[int]: + ans = [] + p = 1 + while n: + n, v = divmod(n, 10) + if v: + ans.append(p * v) + p *= 10 + ans.reverse() + return ans ``` #### Java ```java - +class Solution { + public int[] decimalRepresentation(int n) { + List parts = new ArrayList<>(); + int p = 1; + while (n > 0) { + int v = n % 10; + n /= 10; + if (v != 0) { + parts.add(p * v); + } + p *= 10; + } + Collections.reverse(parts); + int[] ans = new int[parts.size()]; + for (int i = 0; i < parts.size(); ++i) { + ans[i] = parts.get(i); + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector decimalRepresentation(int n) { + vector ans; + long long p = 1; + while (n > 0) { + int v = n % 10; + n /= 10; + if (v != 0) { + ans.push_back(p * v); + } + p *= 10; + } + reverse(ans.begin(), ans.end()); + return ans; + } +}; ``` #### Go ```go +func decimalRepresentation(n int) []int { + ans := []int{} + p := 1 + for n > 0 { + v := n % 10 + n /= 10 + if v != 0 { + ans = append(ans, p*v) + } + p *= 10 + } + slices.Reverse(ans) + return ans +} +``` +#### TypeScript + +```ts +function decimalRepresentation(n: number): number[] { + const ans: number[] = []; + let p: number = 1; + while (n) { + const v = n % 10; + n = (n / 10) | 0; + if (v) { + ans.push(p * v); + } + p *= 10; + } + ans.reverse(); + return ans; +} ``` diff --git a/solution/3600-3699/3697.Compute Decimal Representation/README_EN.md b/solution/3600-3699/3697.Compute Decimal Representation/README_EN.md index b15532cd1ae11..2e62f213a7950 100644 --- a/solution/3600-3699/3697.Compute Decimal Representation/README_EN.md +++ b/solution/3600-3699/3697.Compute Decimal Representation/README_EN.md @@ -79,25 +79,102 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3697.Co #### Python3 ```python - +class Solution: + def decimalRepresentation(self, n: int) -> List[int]: + ans = [] + p = 1 + while n: + n, v = divmod(n, 10) + if v: + ans.append(p * v) + p *= 10 + ans.reverse() + return ans ``` #### Java ```java - +class Solution { + public int[] decimalRepresentation(int n) { + List parts = new ArrayList<>(); + int p = 1; + while (n > 0) { + int v = n % 10; + n /= 10; + if (v != 0) { + parts.add(p * v); + } + p *= 10; + } + Collections.reverse(parts); + int[] ans = new int[parts.size()]; + for (int i = 0; i < parts.size(); ++i) { + ans[i] = parts.get(i); + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector decimalRepresentation(int n) { + vector ans; + long long p = 1; + while (n > 0) { + int v = n % 10; + n /= 10; + if (v != 0) { + ans.push_back(p * v); + } + p *= 10; + } + reverse(ans.begin(), ans.end()); + return ans; + } +}; ``` #### Go ```go +func decimalRepresentation(n int) []int { + ans := []int{} + p := 1 + for n > 0 { + v := n % 10 + n /= 10 + if v != 0 { + ans = append(ans, p*v) + } + p *= 10 + } + slices.Reverse(ans) + return ans +} +``` +#### TypeScript + +```ts +function decimalRepresentation(n: number): number[] { + const ans: number[] = []; + let p: number = 1; + while (n) { + const v = n % 10; + n = (n / 10) | 0; + if (v) { + ans.push(p * v); + } + p *= 10; + } + ans.reverse(); + return ans; +} ``` diff --git a/solution/3600-3699/3697.Compute Decimal Representation/Solution.cpp b/solution/3600-3699/3697.Compute Decimal Representation/Solution.cpp new file mode 100644 index 0000000000000..751ca756c3635 --- /dev/null +++ b/solution/3600-3699/3697.Compute Decimal Representation/Solution.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector decimalRepresentation(int n) { + vector ans; + long long p = 1; + while (n > 0) { + int v = n % 10; + n /= 10; + if (v != 0) { + ans.push_back(p * v); + } + p *= 10; + } + reverse(ans.begin(), ans.end()); + return ans; + } +}; diff --git a/solution/3600-3699/3697.Compute Decimal Representation/Solution.go b/solution/3600-3699/3697.Compute Decimal Representation/Solution.go new file mode 100644 index 0000000000000..cb94eddcd9696 --- /dev/null +++ b/solution/3600-3699/3697.Compute Decimal Representation/Solution.go @@ -0,0 +1,14 @@ +func decimalRepresentation(n int) []int { + ans := []int{} + p := 1 + for n > 0 { + v := n % 10 + n /= 10 + if v != 0 { + ans = append(ans, p*v) + } + p *= 10 + } + slices.Reverse(ans) + return ans +} diff --git a/solution/3600-3699/3697.Compute Decimal Representation/Solution.java b/solution/3600-3699/3697.Compute Decimal Representation/Solution.java new file mode 100644 index 0000000000000..065f2dc8395b5 --- /dev/null +++ b/solution/3600-3699/3697.Compute Decimal Representation/Solution.java @@ -0,0 +1,20 @@ +class Solution { + public int[] decimalRepresentation(int n) { + List parts = new ArrayList<>(); + int p = 1; + while (n > 0) { + int v = n % 10; + n /= 10; + if (v != 0) { + parts.add(p * v); + } + p *= 10; + } + Collections.reverse(parts); + int[] ans = new int[parts.size()]; + for (int i = 0; i < parts.size(); ++i) { + ans[i] = parts.get(i); + } + return ans; + } +} diff --git a/solution/3600-3699/3697.Compute Decimal Representation/Solution.py b/solution/3600-3699/3697.Compute Decimal Representation/Solution.py new file mode 100644 index 0000000000000..d6e5d49c54726 --- /dev/null +++ b/solution/3600-3699/3697.Compute Decimal Representation/Solution.py @@ -0,0 +1,11 @@ +class Solution: + def decimalRepresentation(self, n: int) -> List[int]: + ans = [] + p = 1 + while n: + n, v = divmod(n, 10) + if v: + ans.append(p * v) + p *= 10 + ans.reverse() + return ans diff --git a/solution/3600-3699/3697.Compute Decimal Representation/Solution.ts b/solution/3600-3699/3697.Compute Decimal Representation/Solution.ts new file mode 100644 index 0000000000000..7c817de07dbae --- /dev/null +++ b/solution/3600-3699/3697.Compute Decimal Representation/Solution.ts @@ -0,0 +1,14 @@ +function decimalRepresentation(n: number): number[] { + const ans: number[] = []; + let p: number = 1; + while (n) { + const v = n % 10; + n = (n / 10) | 0; + if (v) { + ans.push(p * v); + } + p *= 10; + } + ans.reverse(); + return ans; +}