From 36253f1240df2e4db21b5822b52a001077b5397c Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 6 Jul 2024 12:47:51 +0800 Subject: [PATCH] feat: update solutions to lc problem: No.0942 No.0942.DI String Match --- .../0900-0999/0942.DI String Match/README.md | 68 ++++++++++--------- .../0942.DI String Match/README_EN.md | 68 ++++++++++--------- .../0942.DI String Match/Solution.go | 12 ++-- .../0942.DI String Match/Solution.py | 7 +- .../0942.DI String Match/Solution.rs | 23 ++++--- .../0942.DI String Match/Solution.ts | 18 +++-- 6 files changed, 98 insertions(+), 98 deletions(-) diff --git a/solution/0900-0999/0942.DI String Match/README.md b/solution/0900-0999/0942.DI String Match/README.md index e0cc78978275a..6766f0052aab8 100644 --- a/solution/0900-0999/0942.DI String Match/README.md +++ b/solution/0900-0999/0942.DI String Match/README.md @@ -65,7 +65,13 @@ tags: -### 方法一 +### 方法一:贪心 + +我们可以使用两个指针 `low` 和 `high` 分别表示当前的最小值和最大值,然后遍历字符串 `s`,如果当前字符是 `I`,那么我们就将 `low` 加入到结果数组中,并且 `low` 自增 1;如果当前字符是 `D`,那么我们就将 `high` 加入到结果数组中,并且 `high` 自减 1。 + +最后,我们将 `low` 加入到结果数组中,返回结果数组即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 `s` 的长度。 @@ -74,11 +80,10 @@ tags: ```python class Solution: def diStringMatch(self, s: str) -> List[int]: - n = len(s) - low, high = 0, n + low, high = 0, len(s) ans = [] - for i in range(n): - if s[i] == 'I': + for c in s: + if c == "I": ans.append(low) low += 1 else: @@ -134,12 +139,10 @@ public: #### Go ```go -func diStringMatch(s string) []int { - n := len(s) - low, high := 0, n - var ans []int - for i := 0; i < n; i++ { - if s[i] == 'I' { +func diStringMatch(s string) (ans []int) { + low, high := 0, len(s) + for _, c := range s { + if c == 'I' { ans = append(ans, low) low++ } else { @@ -148,7 +151,7 @@ func diStringMatch(s string) []int { } } ans = append(ans, low) - return ans + return } ``` @@ -156,19 +159,17 @@ func diStringMatch(s string) []int { ```ts function diStringMatch(s: string): number[] { - const n = s.length; - const res = new Array(n + 1); - let low = 0; - let high = n; - for (let i = 0; i < n; i++) { - if (s[i] === 'I') { - res[i] = low++; + const ans: number[] = []; + let [low, high] = [0, s.length]; + for (const c of s) { + if (c === 'I') { + ans.push(low++); } else { - res[i] = high--; + ans.push(high--); } } - res[n] = low; - return res; + ans.push(low); + return ans; } ``` @@ -177,21 +178,22 @@ function diStringMatch(s: string): number[] { ```rust impl Solution { pub fn di_string_match(s: String) -> Vec { - let s = s.as_bytes(); - let n = s.len(); - let mut res = Vec::with_capacity(n + 1); - let (mut low, mut high) = (-1, (n + 1) as i32); - for i in 0..n { - res.push(if s[i] == b'I' { + let mut low = 0; + let mut high = s.len() as i32; + let mut ans = Vec::with_capacity(s.len() + 1); + + for c in s.chars() { + if c == 'I' { + ans.push(low); low += 1; - low } else { + ans.push(high); high -= 1; - high - }); + } } - res.push(low + 1); - res + + ans.push(low); + ans } } ``` diff --git a/solution/0900-0999/0942.DI String Match/README_EN.md b/solution/0900-0999/0942.DI String Match/README_EN.md index 53c6fd96b3ee6..3bccf78c45cde 100644 --- a/solution/0900-0999/0942.DI String Match/README_EN.md +++ b/solution/0900-0999/0942.DI String Match/README_EN.md @@ -53,7 +53,13 @@ tags: -### Solution 1 +### Solution 1: Greedy Algorithm + +We can use two pointers `low` and `high` to represent the current minimum and maximum values, respectively. Then, we traverse the string `s`. If the current character is `I`, we add `low` to the result array, and increment `low` by 1; if the current character is `D`, we add `high` to the result array, and decrement `high` by 1. + +Finally, we add `low` to the result array and return the result array. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string `s`. @@ -62,11 +68,10 @@ tags: ```python class Solution: def diStringMatch(self, s: str) -> List[int]: - n = len(s) - low, high = 0, n + low, high = 0, len(s) ans = [] - for i in range(n): - if s[i] == 'I': + for c in s: + if c == "I": ans.append(low) low += 1 else: @@ -122,12 +127,10 @@ public: #### Go ```go -func diStringMatch(s string) []int { - n := len(s) - low, high := 0, n - var ans []int - for i := 0; i < n; i++ { - if s[i] == 'I' { +func diStringMatch(s string) (ans []int) { + low, high := 0, len(s) + for _, c := range s { + if c == 'I' { ans = append(ans, low) low++ } else { @@ -136,7 +139,7 @@ func diStringMatch(s string) []int { } } ans = append(ans, low) - return ans + return } ``` @@ -144,19 +147,17 @@ func diStringMatch(s string) []int { ```ts function diStringMatch(s: string): number[] { - const n = s.length; - const res = new Array(n + 1); - let low = 0; - let high = n; - for (let i = 0; i < n; i++) { - if (s[i] === 'I') { - res[i] = low++; + const ans: number[] = []; + let [low, high] = [0, s.length]; + for (const c of s) { + if (c === 'I') { + ans.push(low++); } else { - res[i] = high--; + ans.push(high--); } } - res[n] = low; - return res; + ans.push(low); + return ans; } ``` @@ -165,21 +166,22 @@ function diStringMatch(s: string): number[] { ```rust impl Solution { pub fn di_string_match(s: String) -> Vec { - let s = s.as_bytes(); - let n = s.len(); - let mut res = Vec::with_capacity(n + 1); - let (mut low, mut high) = (-1, (n + 1) as i32); - for i in 0..n { - res.push(if s[i] == b'I' { + let mut low = 0; + let mut high = s.len() as i32; + let mut ans = Vec::with_capacity(s.len() + 1); + + for c in s.chars() { + if c == 'I' { + ans.push(low); low += 1; - low } else { + ans.push(high); high -= 1; - high - }); + } } - res.push(low + 1); - res + + ans.push(low); + ans } } ``` diff --git a/solution/0900-0999/0942.DI String Match/Solution.go b/solution/0900-0999/0942.DI String Match/Solution.go index 5a0f67bb74c93..d2cb36c905c81 100644 --- a/solution/0900-0999/0942.DI String Match/Solution.go +++ b/solution/0900-0999/0942.DI String Match/Solution.go @@ -1,9 +1,7 @@ -func diStringMatch(s string) []int { - n := len(s) - low, high := 0, n - var ans []int - for i := 0; i < n; i++ { - if s[i] == 'I' { +func diStringMatch(s string) (ans []int) { + low, high := 0, len(s) + for _, c := range s { + if c == 'I' { ans = append(ans, low) low++ } else { @@ -12,5 +10,5 @@ func diStringMatch(s string) []int { } } ans = append(ans, low) - return ans + return } \ No newline at end of file diff --git a/solution/0900-0999/0942.DI String Match/Solution.py b/solution/0900-0999/0942.DI String Match/Solution.py index 43e458784fa0f..6f990c8da6c94 100644 --- a/solution/0900-0999/0942.DI String Match/Solution.py +++ b/solution/0900-0999/0942.DI String Match/Solution.py @@ -1,10 +1,9 @@ class Solution: def diStringMatch(self, s: str) -> List[int]: - n = len(s) - low, high = 0, n + low, high = 0, len(s) ans = [] - for i in range(n): - if s[i] == 'I': + for c in s: + if c == "I": ans.append(low) low += 1 else: diff --git a/solution/0900-0999/0942.DI String Match/Solution.rs b/solution/0900-0999/0942.DI String Match/Solution.rs index 3b1d3b05b7987..a43b09586c373 100644 --- a/solution/0900-0999/0942.DI String Match/Solution.rs +++ b/solution/0900-0999/0942.DI String Match/Solution.rs @@ -1,19 +1,20 @@ impl Solution { pub fn di_string_match(s: String) -> Vec { - let s = s.as_bytes(); - let n = s.len(); - let mut res = Vec::with_capacity(n + 1); - let (mut low, mut high) = (-1, (n + 1) as i32); - for i in 0..n { - res.push(if s[i] == b'I' { + let mut low = 0; + let mut high = s.len() as i32; + let mut ans = Vec::with_capacity(s.len() + 1); + + for c in s.chars() { + if c == 'I' { + ans.push(low); low += 1; - low } else { + ans.push(high); high -= 1; - high - }); + } } - res.push(low + 1); - res + + ans.push(low); + ans } } diff --git a/solution/0900-0999/0942.DI String Match/Solution.ts b/solution/0900-0999/0942.DI String Match/Solution.ts index eada064c1d1c0..e1971d829eed0 100644 --- a/solution/0900-0999/0942.DI String Match/Solution.ts +++ b/solution/0900-0999/0942.DI String Match/Solution.ts @@ -1,15 +1,13 @@ function diStringMatch(s: string): number[] { - const n = s.length; - const res = new Array(n + 1); - let low = 0; - let high = n; - for (let i = 0; i < n; i++) { - if (s[i] === 'I') { - res[i] = low++; + const ans: number[] = []; + let [low, high] = [0, s.length]; + for (const c of s) { + if (c === 'I') { + ans.push(low++); } else { - res[i] = high--; + ans.push(high--); } } - res[n] = low; - return res; + ans.push(low); + return ans; }