Skip to content

feat: update solutions to lc problem: No.0942 #3210

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
Jul 6, 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
68 changes: 35 additions & 33 deletions solution/0900-0999/0942.DI String Match/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:贪心

我们可以使用两个指针 `low` 和 `high` 分别表示当前的最小值和最大值,然后遍历字符串 `s`,如果当前字符是 `I`,那么我们就将 `low` 加入到结果数组中,并且 `low` 自增 1;如果当前字符是 `D`,那么我们就将 `high` 加入到结果数组中,并且 `high` 自减 1。

最后,我们将 `low` 加入到结果数组中,返回结果数组即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 `s` 的长度。

<!-- tabs:start -->

Expand All @@ -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:
Expand Down Expand Up @@ -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 {
Expand All @@ -148,27 +151,25 @@ func diStringMatch(s string) []int {
}
}
ans = append(ans, low)
return ans
return
}
```

#### TypeScript

```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;
}
```

Expand All @@ -177,21 +178,22 @@ function diStringMatch(s: string): number[] {
```rust
impl Solution {
pub fn di_string_match(s: String) -> Vec<i32> {
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
}
}
```
Expand Down
68 changes: 35 additions & 33 deletions solution/0900-0999/0942.DI String Match/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ tags:

<!-- solution:start -->

### 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`.

<!-- tabs:start -->

Expand All @@ -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:
Expand Down Expand Up @@ -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 {
Expand All @@ -136,27 +139,25 @@ func diStringMatch(s string) []int {
}
}
ans = append(ans, low)
return ans
return
}
```

#### TypeScript

```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;
}
```

Expand All @@ -165,21 +166,22 @@ function diStringMatch(s: string): number[] {
```rust
impl Solution {
pub fn di_string_match(s: String) -> Vec<i32> {
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
}
}
```
Expand Down
12 changes: 5 additions & 7 deletions solution/0900-0999/0942.DI String Match/Solution.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -12,5 +10,5 @@ func diStringMatch(s string) []int {
}
}
ans = append(ans, low)
return ans
return
}
7 changes: 3 additions & 4 deletions solution/0900-0999/0942.DI String Match/Solution.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
23 changes: 12 additions & 11 deletions solution/0900-0999/0942.DI String Match/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
impl Solution {
pub fn di_string_match(s: String) -> Vec<i32> {
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
}
}
18 changes: 8 additions & 10 deletions solution/0900-0999/0942.DI String Match/Solution.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Loading