Skip to content

feat: add solutions to lc problem: No.1317 #2844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ tags:

### 方法一:直接枚举

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -141,13 +141,30 @@ func getNoZeroIntegers(n int) []int {
}
```

#### TypeScript

```ts
function getNoZeroIntegers(n: number): number[] {
for (let a = 1; ; ++a) {
const b = n - a;
if (!`${a}${b}`.includes('0')) {
return [a, b];
}
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二
### 方法二:直接枚举(另一种写法)

在方法一中,我们将 $a$ 和 $b$ 转换为字符串并且连接起来,然后判断是否包含字符 `'0'`。这里我们可以通过一个函数 $f(x)$ 来判断 $x$ 是否包含字符 `'0'`,然后直接枚举 $a$,判断 $a$ 和 $b = n - a$ 是否都不包含字符 `'0'`,如果是,则找到了答案,返回 $[a, b]$。

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

<!-- tabs:start -->

Expand Down Expand Up @@ -238,6 +255,27 @@ func getNoZeroIntegers(n int) []int {
}
```

#### TypeScript

```ts
function getNoZeroIntegers(n: number): number[] {
const f = (x: number): boolean => {
for (; x; x = (x / 10) | 0) {
if (x % 10 === 0) {
return false;
}
}
return true;
};
for (let a = 1; ; ++a) {
const b = n - a;
if (f(a) && f(b)) {
return [a, b];
}
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ Note that there are other valid answers as [8, 3] that can be accepted.

<!-- solution:start -->

### Solution 1
### Solution 1: Direct Enumeration

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]$.

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)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -121,13 +125,30 @@ func getNoZeroIntegers(n int) []int {
}
```

#### TypeScript

```ts
function getNoZeroIntegers(n: number): number[] {
for (let a = 1; ; ++a) {
const b = n - a;
if (!`${a}${b}`.includes('0')) {
return [a, b];
}
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2
### Solution 2: Direct Enumeration (Alternative Approach)

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]$.

The time complexity is $O(n \times \log n)$, where $n$ is the integer given in the problem. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -218,6 +239,27 @@ func getNoZeroIntegers(n int) []int {
}
```

#### TypeScript

```ts
function getNoZeroIntegers(n: number): number[] {
const f = (x: number): boolean => {
for (; x; x = (x / 10) | 0) {
if (x % 10 === 0) {
return false;
}
}
return true;
};
for (let a = 1; ; ++a) {
const b = n - a;
if (f(a) && f(b)) {
return [a, b];
}
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function getNoZeroIntegers(n: number): number[] {
for (let a = 1; ; ++a) {
const b = n - a;
if (!`${a}${b}`.includes('0')) {
return [a, b];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function getNoZeroIntegers(n: number): number[] {
const f = (x: number): boolean => {
for (; x; x = (x / 10) | 0) {
if (x % 10 === 0) {
return false;
}
}
return true;
};
for (let a = 1; ; ++a) {
const b = n - a;
if (f(a) && f(b)) {
return [a, b];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ tags:
```cpp
class Solution {
public:
long long maximumValueSum(vector<int>& nums, int k,
vector<vector<int>>& edges) {
long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {
long long totalSum = 0;
int count = 0;
int positiveMin = INT_MAX;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ It can be shown that 9 is the maximum achievable sum of values.
```cpp
class Solution {
public:
long long maximumValueSum(vector<int>& nums, int k,
vector<vector<int>>& edges) {
long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {
long long totalSum = 0;
int count = 0;
int positiveMin = INT_MAX;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class Solution {
public:
long long maximumValueSum(vector<int>& nums, int k,
vector<vector<int>>& edges) {
long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {
long long totalSum = 0;
int count = 0;
int positiveMin = INT_MAX;
Expand Down