Skip to content

Commit 1f6e6bd

Browse files
committed
feat: add solutions to lc problems: No.0389,0607,1678,1768
- No.0389.Find the Difference - No.0607.Sales Person - No.1678.Goal Parser Interpretation - No.1768.Merge Strings Alternately
1 parent 2aabab7 commit 1f6e6bd

File tree

12 files changed

+248
-10
lines changed

12 files changed

+248
-10
lines changed

solution/0300-0399/0389.Find the Difference/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,31 @@ class Solution {
8686
}
8787
```
8888

89+
### **Rust**
90+
91+
```rust
92+
impl Solution {
93+
pub fn find_the_difference(s: String, t: String) -> char {
94+
let s = s.as_bytes();
95+
let t = t.as_bytes();
96+
let n = s.len();
97+
let mut count = [0; 26];
98+
for i in 0..n {
99+
count[(s[i] - b'a') as usize] -= 1;
100+
count[(t[i] - b'a') as usize] += 1;
101+
}
102+
let mut res = *t.last().unwrap();
103+
for i in 0..26 {
104+
if count[i] == 1 {
105+
res = (i as u8) + b'a';
106+
break;
107+
}
108+
}
109+
char::from(res)
110+
}
111+
}
112+
```
113+
89114
### **...**
90115

91116
```

solution/0300-0399/0389.Find the Difference/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,31 @@ class Solution {
7474
}
7575
```
7676

77+
### **Rust**
78+
79+
```rust
80+
impl Solution {
81+
pub fn find_the_difference(s: String, t: String) -> char {
82+
let s = s.as_bytes();
83+
let t = t.as_bytes();
84+
let n = s.len();
85+
let mut count = [0; 26];
86+
for i in 0..n {
87+
count[(s[i] - b'a') as usize] -= 1;
88+
count[(t[i] - b'a') as usize] += 1;
89+
}
90+
let mut res = *t.last().unwrap();
91+
for i in 0..26 {
92+
if count[i] == 1 {
93+
res = (i as u8) + b'a';
94+
break;
95+
}
96+
}
97+
char::from(res)
98+
}
99+
}
100+
```
101+
77102
### **...**
78103

79104
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn find_the_difference(s: String, t: String) -> char {
3+
let s = s.as_bytes();
4+
let t = t.as_bytes();
5+
let n = s.len();
6+
let mut count = [0; 26];
7+
for i in 0..n {
8+
count[(s[i] - b'a') as usize] -= 1;
9+
count[(t[i] - b'a') as usize] += 1;
10+
}
11+
let mut res = *t.last().unwrap();
12+
for i in 0..26 {
13+
if count[i] == 1 {
14+
res = (i as u8) + b'a';
15+
break;
16+
}
17+
}
18+
char::from(res)
19+
}
20+
}

solution/0600-0699/0607.Sales Person/README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ Orders 表:
121121
### **SQL**
122122

123123
```sql
124-
# Write your MySQL query statement below
125124
SELECT name
126125
FROM salesperson
127126
WHERE sales_id
@@ -133,4 +132,21 @@ NOT IN (
133132
);
134133
```
135134

135+
```sql
136+
SELECT
137+
name
138+
FROM
139+
SalesPerson AS s
140+
WHERE
141+
0 = (
142+
SELECT
143+
COUNT(*)
144+
FROM
145+
Orders AS o
146+
JOIN Company AS c ON o.com_id = c.com_id
147+
WHERE
148+
o.sales_id = s.sales_id AND c.name = 'RED'
149+
);
150+
```
151+
136152
<!-- tabs:end -->

solution/0600-0699/0607.Sales Person/README_EN.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ According to orders 3 and 4 in the Orders table, it is easy to tell that only sa
116116
### **SQL**
117117

118118
```sql
119-
# Write your MySQL query statement below
120119
SELECT name
121120
FROM salesperson
122121
WHERE sales_id
@@ -128,4 +127,21 @@ NOT IN (
128127
);
129128
```
130129

130+
```sql
131+
SELECT
132+
name
133+
FROM
134+
SalesPerson AS s
135+
WHERE
136+
0 = (
137+
SELECT
138+
COUNT(*)
139+
FROM
140+
Orders AS o
141+
JOIN Company AS c ON o.com_id = c.com_id
142+
WHERE
143+
o.sales_id = s.sales_id AND c.name = 'RED'
144+
);
145+
```
146+
131147
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
# Write your MySQL query statement below
21
SELECT name
32
FROM salesperson
4-
WHERE sales_id
5-
NOT IN (
6-
SELECT s.sales_id FROM orders o
7-
INNER JOIN salesperson s ON o.sales_id = s.sales_id
8-
INNER JOIN company c ON o.com_id = c.com_id
9-
WHERE c.name = 'RED'
10-
);
3+
WHERE sales_id NOT IN (
4+
SELECT s.sales_id
5+
FROM orders o
6+
INNER JOIN salesperson s ON o.sales_id = s.sales_id
7+
INNER JOIN company c ON o.com_id = c.com_id
8+
WHERE c.name = 'RED'
9+
);

solution/1600-1699/1678.Goal Parser Interpretation/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,33 @@ func interpret(command string) string {
158158
}
159159
```
160160

161+
### **Rust**
162+
163+
```rust
164+
impl Solution {
165+
pub fn interpret(command: String) -> String {
166+
const ss: [&str; 3] = ["G", "o", "al"];
167+
let n = command.len();
168+
let bs = command.as_bytes();
169+
let mut res = String::new();
170+
let mut i = 0;
171+
while i < n {
172+
if bs[i] == b'G' {
173+
res.push_str(ss[0]);
174+
i += 1;
175+
} else if bs[i + 1] == b')' {
176+
res.push_str(ss[1]);
177+
i += 2
178+
} else {
179+
res.push_str(ss[2]);
180+
i += 4
181+
}
182+
}
183+
res
184+
}
185+
}
186+
```
187+
161188
### **...**
162189

163190
```

solution/1600-1699/1678.Goal Parser Interpretation/README_EN.md

+27
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,33 @@ func interpret(command string) string {
151151
}
152152
```
153153

154+
### **Rust**
155+
156+
```rust
157+
impl Solution {
158+
pub fn interpret(command: String) -> String {
159+
const ss: [&str; 3] = ["G", "o", "al"];
160+
let n = command.len();
161+
let bs = command.as_bytes();
162+
let mut res = String::new();
163+
let mut i = 0;
164+
while i < n {
165+
if bs[i] == b'G' {
166+
res.push_str(ss[0]);
167+
i += 1;
168+
} else if bs[i + 1] == b')' {
169+
res.push_str(ss[1]);
170+
i += 2
171+
} else {
172+
res.push_str(ss[2]);
173+
i += 4
174+
}
175+
}
176+
res
177+
}
178+
}
179+
```
180+
154181
### **...**
155182

156183
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn interpret(command: String) -> String {
3+
const ss: [&str; 3] = ["G", "o", "al"];
4+
let n = command.len();
5+
let bs = command.as_bytes();
6+
let mut res = String::new();
7+
let mut i = 0;
8+
while i < n {
9+
if bs[i] == b'G' {
10+
res.push_str(ss[0]);
11+
i += 1;
12+
} else if bs[i + 1] == b')' {
13+
res.push_str(ss[1]);
14+
i += 2
15+
} else {
16+
res.push_str(ss[2]);
17+
i += 4
18+
}
19+
}
20+
res
21+
}
22+
}

solution/1700-1799/1768.Merge Strings Alternately/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@ public:
121121
};
122122
```
123123
124+
### **Rust**
125+
126+
```rust
127+
impl Solution {
128+
pub fn merge_alternately(word1: String, word2: String) -> String {
129+
let s1 = word1.as_bytes();
130+
let s2 = word2.as_bytes();
131+
let n = s1.len().max(s2.len());
132+
let mut res = vec![];
133+
for i in 0..n {
134+
if s1.get(i).is_some() {
135+
res.push(s1[i]);
136+
}
137+
if s2.get(i).is_some() {
138+
res.push(s2[i]);
139+
}
140+
}
141+
String::from_utf8(res).unwrap()
142+
}
143+
}
144+
```
145+
124146
### **...**
125147

126148
```

solution/1700-1799/1768.Merge Strings Alternately/README_EN.md

+22
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,28 @@ public:
135135
};
136136
```
137137
138+
### **Rust**
139+
140+
```rust
141+
impl Solution {
142+
pub fn merge_alternately(word1: String, word2: String) -> String {
143+
let s1 = word1.as_bytes();
144+
let s2 = word2.as_bytes();
145+
let n = s1.len().max(s2.len());
146+
let mut res = vec![];
147+
for i in 0..n {
148+
if s1.get(i).is_some() {
149+
res.push(s1[i]);
150+
}
151+
if s2.get(i).is_some() {
152+
res.push(s2[i]);
153+
}
154+
}
155+
String::from_utf8(res).unwrap()
156+
}
157+
}
158+
```
159+
138160
### **...**
139161

140162
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
impl Solution {
2+
pub fn merge_alternately(word1: String, word2: String) -> String {
3+
let s1 = word1.as_bytes();
4+
let s2 = word2.as_bytes();
5+
let n = s1.len().max(s2.len());
6+
let mut res = vec![];
7+
for i in 0..n {
8+
if s1.get(i).is_some() {
9+
res.push(s1[i]);
10+
}
11+
if s2.get(i).is_some() {
12+
res.push(s2[i]);
13+
}
14+
}
15+
String::from_utf8(res).unwrap()
16+
}
17+
}

0 commit comments

Comments
 (0)