File tree Expand file tree Collapse file tree 7 files changed +243
-7
lines changed
solution/3600-3699/3697.Compute Decimal Representation Expand file tree Collapse file tree 7 files changed +243
-7
lines changed Original file line number Diff line number Diff line change @@ -74,32 +74,115 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3697.Co
74
74
75
75
<!-- solution:start -->
76
76
77
- ### 方法一
77
+ ### 方法一:模拟
78
+
79
+ 我们可以不断地对 $n$ 进行取模和整除操作,取模得到的值乘以当前的位值 $p$ 就是一个 10 进制分量。如果取模的结果不为 $0$,我们就将这个分量加入答案中。然后我们将 $p$ 乘以 $10$,继续处理下一个位。
80
+
81
+ 最后,我们将答案反转,使其按降序排列。
82
+
83
+ 时间复杂度 $O(\log n)$,其中 $n$ 是输入的正整数。空间复杂度 $O(\log n)$,用于存储答案。
78
84
79
85
<!-- tabs:start -->
80
86
81
87
#### Python3
82
88
83
89
``` python
84
-
90
+ class Solution :
91
+ def decimalRepresentation (self , n : int ) -> List[int ]:
92
+ ans = []
93
+ p = 1
94
+ while n:
95
+ n, v = divmod (n, 10 )
96
+ if v:
97
+ ans.append(p * v)
98
+ p *= 10
99
+ ans.reverse()
100
+ return ans
85
101
```
86
102
87
103
#### Java
88
104
89
105
``` java
90
-
106
+ class Solution {
107
+ public int [] decimalRepresentation (int n ) {
108
+ List<Integer > parts = new ArrayList<> ();
109
+ int p = 1 ;
110
+ while (n > 0 ) {
111
+ int v = n % 10 ;
112
+ n /= 10 ;
113
+ if (v != 0 ) {
114
+ parts. add(p * v);
115
+ }
116
+ p *= 10 ;
117
+ }
118
+ Collections . reverse(parts);
119
+ int [] ans = new int [parts. size()];
120
+ for (int i = 0 ; i < parts. size(); ++ i) {
121
+ ans[i] = parts. get(i);
122
+ }
123
+ return ans;
124
+ }
125
+ }
91
126
```
92
127
93
128
#### C++
94
129
95
130
``` cpp
96
-
131
+ class Solution {
132
+ public:
133
+ vector<int > decimalRepresentation(int n) {
134
+ vector<int > ans;
135
+ long long p = 1;
136
+ while (n > 0) {
137
+ int v = n % 10;
138
+ n /= 10;
139
+ if (v != 0) {
140
+ ans.push_back(p * v);
141
+ }
142
+ p * = 10;
143
+ }
144
+ reverse(ans.begin(), ans.end());
145
+ return ans;
146
+ }
147
+ };
97
148
```
98
149
99
150
#### Go
100
151
101
152
```go
153
+ func decimalRepresentation(n int) []int {
154
+ ans := []int{}
155
+ p := 1
156
+ for n > 0 {
157
+ v := n % 10
158
+ n /= 10
159
+ if v != 0 {
160
+ ans = append(ans, p*v)
161
+ }
162
+ p *= 10
163
+ }
164
+ slices.Reverse(ans)
165
+ return ans
166
+ }
167
+ ```
102
168
169
+ #### TypeScript
170
+
171
+ ``` ts
172
+ function decimalRepresentation(n : number ): number [] {
173
+ const ans: number [] = [];
174
+ let p: number = 1 ;
175
+ while (n ) {
176
+ const v = n % 10 ;
177
+ n = (n / 10 ) | 0 ;
178
+ if (v ) {
179
+ ans .push (p * v );
180
+ }
181
+ p *= 10 ;
182
+ }
183
+ ans .reverse ();
184
+ return ans ;
185
+ }
103
186
```
104
187
105
188
<!-- tabs: end -->
Original file line number Diff line number Diff line change @@ -79,25 +79,102 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3697.Co
79
79
#### Python3
80
80
81
81
``` python
82
-
82
+ class Solution :
83
+ def decimalRepresentation (self , n : int ) -> List[int ]:
84
+ ans = []
85
+ p = 1
86
+ while n:
87
+ n, v = divmod (n, 10 )
88
+ if v:
89
+ ans.append(p * v)
90
+ p *= 10
91
+ ans.reverse()
92
+ return ans
83
93
```
84
94
85
95
#### Java
86
96
87
97
``` java
88
-
98
+ class Solution {
99
+ public int [] decimalRepresentation (int n ) {
100
+ List<Integer > parts = new ArrayList<> ();
101
+ int p = 1 ;
102
+ while (n > 0 ) {
103
+ int v = n % 10 ;
104
+ n /= 10 ;
105
+ if (v != 0 ) {
106
+ parts. add(p * v);
107
+ }
108
+ p *= 10 ;
109
+ }
110
+ Collections . reverse(parts);
111
+ int [] ans = new int [parts. size()];
112
+ for (int i = 0 ; i < parts. size(); ++ i) {
113
+ ans[i] = parts. get(i);
114
+ }
115
+ return ans;
116
+ }
117
+ }
89
118
```
90
119
91
120
#### C++
92
121
93
122
``` cpp
94
-
123
+ class Solution {
124
+ public:
125
+ vector<int > decimalRepresentation(int n) {
126
+ vector<int > ans;
127
+ long long p = 1;
128
+ while (n > 0) {
129
+ int v = n % 10;
130
+ n /= 10;
131
+ if (v != 0) {
132
+ ans.push_back(p * v);
133
+ }
134
+ p * = 10;
135
+ }
136
+ reverse(ans.begin(), ans.end());
137
+ return ans;
138
+ }
139
+ };
95
140
```
96
141
97
142
#### Go
98
143
99
144
```go
145
+ func decimalRepresentation(n int) []int {
146
+ ans := []int{}
147
+ p := 1
148
+ for n > 0 {
149
+ v := n % 10
150
+ n /= 10
151
+ if v != 0 {
152
+ ans = append(ans, p*v)
153
+ }
154
+ p *= 10
155
+ }
156
+ slices.Reverse(ans)
157
+ return ans
158
+ }
159
+ ```
100
160
161
+ #### TypeScript
162
+
163
+ ``` ts
164
+ function decimalRepresentation(n : number ): number [] {
165
+ const ans: number [] = [];
166
+ let p: number = 1 ;
167
+ while (n ) {
168
+ const v = n % 10 ;
169
+ n = (n / 10 ) | 0 ;
170
+ if (v ) {
171
+ ans .push (p * v );
172
+ }
173
+ p *= 10 ;
174
+ }
175
+ ans .reverse ();
176
+ return ans ;
177
+ }
101
178
```
102
179
103
180
<!-- tabs: end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<int > decimalRepresentation (int n) {
4
+ vector<int > ans;
5
+ long long p = 1 ;
6
+ while (n > 0 ) {
7
+ int v = n % 10 ;
8
+ n /= 10 ;
9
+ if (v != 0 ) {
10
+ ans.push_back (p * v);
11
+ }
12
+ p *= 10 ;
13
+ }
14
+ reverse (ans.begin (), ans.end ());
15
+ return ans;
16
+ }
17
+ };
Original file line number Diff line number Diff line change
1
+ func decimalRepresentation (n int ) []int {
2
+ ans := []int {}
3
+ p := 1
4
+ for n > 0 {
5
+ v := n % 10
6
+ n /= 10
7
+ if v != 0 {
8
+ ans = append (ans , p * v )
9
+ }
10
+ p *= 10
11
+ }
12
+ slices .Reverse (ans )
13
+ return ans
14
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int [] decimalRepresentation (int n ) {
3
+ List <Integer > parts = new ArrayList <>();
4
+ int p = 1 ;
5
+ while (n > 0 ) {
6
+ int v = n % 10 ;
7
+ n /= 10 ;
8
+ if (v != 0 ) {
9
+ parts .add (p * v );
10
+ }
11
+ p *= 10 ;
12
+ }
13
+ Collections .reverse (parts );
14
+ int [] ans = new int [parts .size ()];
15
+ for (int i = 0 ; i < parts .size (); ++i ) {
16
+ ans [i ] = parts .get (i );
17
+ }
18
+ return ans ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def decimalRepresentation (self , n : int ) -> List [int ]:
3
+ ans = []
4
+ p = 1
5
+ while n :
6
+ n , v = divmod (n , 10 )
7
+ if v :
8
+ ans .append (p * v )
9
+ p *= 10
10
+ ans .reverse ()
11
+ return ans
Original file line number Diff line number Diff line change
1
+ function decimalRepresentation ( n : number ) : number [ ] {
2
+ const ans : number [ ] = [ ] ;
3
+ let p : number = 1 ;
4
+ while ( n ) {
5
+ const v = n % 10 ;
6
+ n = ( n / 10 ) | 0 ;
7
+ if ( v ) {
8
+ ans . push ( p * v ) ;
9
+ }
10
+ p *= 10 ;
11
+ }
12
+ ans . reverse ( ) ;
13
+ return ans ;
14
+ }
You can’t perform that action at this time.
0 commit comments