Skip to content

Commit 06533ad

Browse files
authored
feat: add solutions to lc problem: No.1317 (doocs#2844)
1 parent 818daa9 commit 06533ad

File tree

7 files changed

+112
-11
lines changed

7 files changed

+112
-11
lines changed

solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README.md

+41-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ tags:
8080

8181
### 方法一:直接枚举
8282

83-
从 $1$ 开始枚举 $a$,判断 $a$ 和 $n - a$ 是否满足条件,如果满足则返回
83+
从 $1$ 开始枚举 $a$,那么 $b = n - a$。对于每个 $a$ 和 $b$,我们将它们转换为字符串并且连接起来,然后判断是否包含字符 `'0'`,如果不包含,那么就找到了答案,返回 $[a, b]$
8484

85-
时间复杂度 $O(n\times \log n)$,空间复杂度 $O(1)$。其中 $n$ 为题目给定的整数
85+
时间复杂度 $O(n \times \log n)$,其中 $n$ 为题目给定的整数。空间复杂度 $O(\log n)$
8686

8787
<!-- tabs:start -->
8888

@@ -141,13 +141,30 @@ func getNoZeroIntegers(n int) []int {
141141
}
142142
```
143143

144+
#### TypeScript
145+
146+
```ts
147+
function getNoZeroIntegers(n: number): number[] {
148+
for (let a = 1; ; ++a) {
149+
const b = n - a;
150+
if (!`${a}${b}`.includes('0')) {
151+
return [a, b];
152+
}
153+
}
154+
}
155+
```
156+
144157
<!-- tabs:end -->
145158

146159
<!-- solution:end -->
147160

148161
<!-- solution:start -->
149162

150-
### 方法二
163+
### 方法二:直接枚举(另一种写法)
164+
165+
在方法一中,我们将 $a$ 和 $b$ 转换为字符串并且连接起来,然后判断是否包含字符 `'0'`。这里我们可以通过一个函数 $f(x)$ 来判断 $x$ 是否包含字符 `'0'`,然后直接枚举 $a$,判断 $a$ 和 $b = n - a$ 是否都不包含字符 `'0'`,如果是,则找到了答案,返回 $[a, b]$。
166+
167+
时间复杂度 $O(n \times \log n)$,其中 $n$ 为题目给定的整数。空间复杂度 $O(1)$。
151168

152169
<!-- tabs:start -->
153170

@@ -238,6 +255,27 @@ func getNoZeroIntegers(n int) []int {
238255
}
239256
```
240257

258+
#### TypeScript
259+
260+
```ts
261+
function getNoZeroIntegers(n: number): number[] {
262+
const f = (x: number): boolean => {
263+
for (; x; x = (x / 10) | 0) {
264+
if (x % 10 === 0) {
265+
return false;
266+
}
267+
}
268+
return true;
269+
};
270+
for (let a = 1; ; ++a) {
271+
const b = n - a;
272+
if (f(a) && f(b)) {
273+
return [a, b];
274+
}
275+
}
276+
}
277+
```
278+
241279
<!-- tabs:end -->
242280

243281
<!-- solution:end -->

solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README_EN.md

+44-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ Note that there are other valid answers as [8, 3] that can be accepted.
6262

6363
<!-- solution:start -->
6464

65-
### Solution 1
65+
### Solution 1: Direct Enumeration
66+
67+
Starting from $1$, we enumerate $a$, then $b = n - a$. For each $a$ and $b$, we convert them to strings and concatenate them, then check if they contain the character '0'. If they do not contain '0', we have found the answer and return $[a, b]$.
68+
69+
The time complexity is $O(n \times \log n)$, where $n$ is the integer given in the problem. The space complexity is $O(\log n)$.
6670

6771
<!-- tabs:start -->
6872

@@ -121,13 +125,30 @@ func getNoZeroIntegers(n int) []int {
121125
}
122126
```
123127

128+
#### TypeScript
129+
130+
```ts
131+
function getNoZeroIntegers(n: number): number[] {
132+
for (let a = 1; ; ++a) {
133+
const b = n - a;
134+
if (!`${a}${b}`.includes('0')) {
135+
return [a, b];
136+
}
137+
}
138+
}
139+
```
140+
124141
<!-- tabs:end -->
125142

126143
<!-- solution:end -->
127144

128145
<!-- solution:start -->
129146

130-
### Solution 2
147+
### Solution 2: Direct Enumeration (Alternative Approach)
148+
149+
In Solution 1, we converted $a$ and $b$ into strings and concatenated them, then checked if they contained the character '0'. Here, we can use a function $f(x)$ to check whether $x$ contains the character '0', and then directly enumerate $a$, checking whether both $a$ and $b = n - a$ do not contain the character '0'. If they do not, we have found the answer and return $[a, b]$.
150+
151+
The time complexity is $O(n \times \log n)$, where $n$ is the integer given in the problem. The space complexity is $O(1)$.
131152

132153
<!-- tabs:start -->
133154

@@ -218,6 +239,27 @@ func getNoZeroIntegers(n int) []int {
218239
}
219240
```
220241

242+
#### TypeScript
243+
244+
```ts
245+
function getNoZeroIntegers(n: number): number[] {
246+
const f = (x: number): boolean => {
247+
for (; x; x = (x / 10) | 0) {
248+
if (x % 10 === 0) {
249+
return false;
250+
}
251+
}
252+
return true;
253+
};
254+
for (let a = 1; ; ++a) {
255+
const b = n - a;
256+
if (f(a) && f(b)) {
257+
return [a, b];
258+
}
259+
}
260+
}
261+
```
262+
221263
<!-- tabs:end -->
222264

223265
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function getNoZeroIntegers(n: number): number[] {
2+
for (let a = 1; ; ++a) {
3+
const b = n - a;
4+
if (!`${a}${b}`.includes('0')) {
5+
return [a, b];
6+
}
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function getNoZeroIntegers(n: number): number[] {
2+
const f = (x: number): boolean => {
3+
for (; x; x = (x / 10) | 0) {
4+
if (x % 10 === 0) {
5+
return false;
6+
}
7+
}
8+
return true;
9+
};
10+
for (let a = 1; ; ++a) {
11+
const b = n - a;
12+
if (f(a) && f(b)) {
13+
return [a, b];
14+
}
15+
}
16+
}

solution/3000-3099/3068.Find the Maximum Sum of Node Values/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ tags:
119119
```cpp
120120
class Solution {
121121
public:
122-
long long maximumValueSum(vector<int>& nums, int k,
123-
vector<vector<int>>& edges) {
122+
long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {
124123
long long totalSum = 0;
125124
int count = 0;
126125
int positiveMin = INT_MAX;

solution/3000-3099/3068.Find the Maximum Sum of Node Values/README_EN.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ It can be shown that 9 is the maximum achievable sum of values.
111111
```cpp
112112
class Solution {
113113
public:
114-
long long maximumValueSum(vector<int>& nums, int k,
115-
vector<vector<int>>& edges) {
114+
long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {
116115
long long totalSum = 0;
117116
int count = 0;
118117
int positiveMin = INT_MAX;

solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class Solution {
22
public:
3-
long long maximumValueSum(vector<int>& nums, int k,
4-
vector<vector<int>>& edges) {
3+
long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {
54
long long totalSum = 0;
65
int count = 0;
76
int positiveMin = INT_MAX;

0 commit comments

Comments
 (0)