From 79d384fcb3e2a24f9f19772a3b750e2b89d6b252 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 19 May 2024 21:19:27 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1317 No.1317.Convert Integer to the Sum of Two No-Zero Integers --- .../README.md | 44 ++++++++++++++++-- .../README_EN.md | 46 ++++++++++++++++++- .../Solution.ts | 8 ++++ .../Solution2.ts | 16 +++++++ .../README.md | 3 +- .../README_EN.md | 3 +- .../Solution.cpp | 3 +- 7 files changed, 112 insertions(+), 11 deletions(-) create mode 100644 solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution.ts create mode 100644 solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution2.ts diff --git a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README.md b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README.md index 95a71c37bca2d..df2390fb2d575 100644 --- a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README.md +++ b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README.md @@ -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)$。 @@ -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]; + } + } +} +``` + -### 方法二 +### 方法二:直接枚举(另一种写法) + +在方法一中,我们将 $a$ 和 $b$ 转换为字符串并且连接起来,然后判断是否包含字符 `'0'`。这里我们可以通过一个函数 $f(x)$ 来判断 $x$ 是否包含字符 `'0'`,然后直接枚举 $a$,判断 $a$ 和 $b = n - a$ 是否都不包含字符 `'0'`,如果是,则找到了答案,返回 $[a, b]$。 + +时间复杂度 $O(n \times \log n)$,其中 $n$ 为题目给定的整数。空间复杂度 $O(1)$。 @@ -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]; + } + } +} +``` + diff --git a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README_EN.md b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README_EN.md index 440f2650d8888..d377b891d9ab4 100644 --- a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README_EN.md +++ b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README_EN.md @@ -62,7 +62,11 @@ Note that there are other valid answers as [8, 3] that can be accepted. -### 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)$. @@ -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]; + } + } +} +``` + -### 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)$. @@ -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]; + } + } +} +``` + diff --git a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution.ts b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution.ts new file mode 100644 index 0000000000000..0089abb4afd98 --- /dev/null +++ b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution.ts @@ -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]; + } + } +} diff --git a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution2.ts b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution2.ts new file mode 100644 index 0000000000000..80d2915e9b2f8 --- /dev/null +++ b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution2.ts @@ -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]; + } + } +} diff --git a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README.md b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README.md index f3a353daa7e49..83e99e58f23d9 100644 --- a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README.md +++ b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README.md @@ -119,8 +119,7 @@ tags: ```cpp class Solution { public: - long long maximumValueSum(vector& nums, int k, - vector>& edges) { + long long maximumValueSum(vector& nums, int k, vector>& edges) { long long totalSum = 0; int count = 0; int positiveMin = INT_MAX; diff --git a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README_EN.md b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README_EN.md index 0ba1ae79c8814..02f7391356a66 100644 --- a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README_EN.md +++ b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README_EN.md @@ -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& nums, int k, - vector>& edges) { + long long maximumValueSum(vector& nums, int k, vector>& edges) { long long totalSum = 0; int count = 0; int positiveMin = INT_MAX; diff --git a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.cpp b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.cpp index 3b048350a7666..76da2b6d44889 100644 --- a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.cpp +++ b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.cpp @@ -1,7 +1,6 @@ class Solution { public: - long long maximumValueSum(vector& nums, int k, - vector>& edges) { + long long maximumValueSum(vector& nums, int k, vector>& edges) { long long totalSum = 0; int count = 0; int positiveMin = INT_MAX;