Skip to content

Commit 725ab64

Browse files
authored
feat: add solutions to lc problems: No.2899~2901 (#1813)
* No.2899.Last Visited Integers * No.2900.Longest Unequal Adjacent Groups Subsequence I * No.2901.Longest Unequal Adjacent Groups Subsequence II
1 parent cead117 commit 725ab64

File tree

14 files changed

+845
-6
lines changed

14 files changed

+845
-6
lines changed

solution/2800-2899/2899.Last Visited Integers/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,31 @@ function lastVisitedIntegers(words: string[]): number[] {
178178
}
179179
```
180180

181+
### **Rust**
182+
183+
```rust
184+
impl Solution {
185+
pub fn last_visited_integers(words: Vec<String>) -> Vec<i32> {
186+
let mut nums: Vec<i32> = Vec::new();
187+
let mut ans: Vec<i32> = Vec::new();
188+
let mut k = 0;
189+
190+
for w in words {
191+
if w == "prev" {
192+
k += 1;
193+
let i = nums.len() as i32 - k;
194+
ans.push(if i < 0 { -1 } else { nums[i as usize] });
195+
} else {
196+
k = 0;
197+
nums.push(w.parse::<i32>().unwrap());
198+
}
199+
}
200+
201+
ans
202+
}
203+
}
204+
```
205+
181206
### **...**
182207

183208
```

solution/2800-2899/2899.Last Visited Integers/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,31 @@ function lastVisitedIntegers(words: string[]): number[] {
168168
}
169169
```
170170

171+
### **Rust**
172+
173+
```rust
174+
impl Solution {
175+
pub fn last_visited_integers(words: Vec<String>) -> Vec<i32> {
176+
let mut nums: Vec<i32> = Vec::new();
177+
let mut ans: Vec<i32> = Vec::new();
178+
let mut k = 0;
179+
180+
for w in words {
181+
if w == "prev" {
182+
k += 1;
183+
let i = nums.len() as i32 - k;
184+
ans.push(if i < 0 { -1 } else { nums[i as usize] });
185+
} else {
186+
k = 0;
187+
nums.push(w.parse::<i32>().unwrap());
188+
}
189+
}
190+
191+
ans
192+
}
193+
}
194+
```
195+
171196
### **...**
172197

173198
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn last_visited_integers(words: Vec<String>) -> Vec<i32> {
3+
let mut nums: Vec<i32> = Vec::new();
4+
let mut ans: Vec<i32> = Vec::new();
5+
let mut k = 0;
6+
7+
for w in words {
8+
if w == "prev" {
9+
k += 1;
10+
let i = nums.len() as i32 - k;
11+
ans.push(if i < 0 { -1 } else { nums[i as usize] });
12+
} else {
13+
k = 0;
14+
nums.push(w.parse::<i32>().unwrap());
15+
}
16+
}
17+
18+
ans
19+
}
20+
}

solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,24 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number
140140
}
141141
```
142142

143+
### **Rust**
144+
145+
```rust
146+
impl Solution {
147+
pub fn get_words_in_longest_subsequence(n: i32, words: Vec<String>, groups: Vec<i32>) -> Vec<String> {
148+
let mut ans = vec![];
149+
150+
for i in 0..n {
151+
if i == 0 || groups[i as usize] != groups[(i - 1) as usize] {
152+
ans.push(words[i as usize].clone());
153+
}
154+
}
155+
156+
ans
157+
}
158+
}
159+
```
160+
143161
### **...**
144162

145163
```

solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md

+18
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number
131131
}
132132
```
133133

134+
### **Rust**
135+
136+
```rust
137+
impl Solution {
138+
pub fn get_words_in_longest_subsequence(n: i32, words: Vec<String>, groups: Vec<i32>) -> Vec<String> {
139+
let mut ans = vec![];
140+
141+
for i in 0..n {
142+
if i == 0 || groups[i as usize] != groups[(i - 1) as usize] {
143+
ans.push(words[i as usize].clone());
144+
}
145+
}
146+
147+
ans
148+
}
149+
}
150+
```
151+
134152
### **...**
135153

136154
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn get_words_in_longest_subsequence(n: i32, words: Vec<String>, groups: Vec<i32>) -> Vec<String> {
3+
let mut ans = vec![];
4+
5+
for i in 0..n {
6+
if i == 0 || groups[i as usize] != groups[(i - 1) as usize] {
7+
ans.push(words[i as usize].clone());
8+
}
9+
}
10+
11+
ans
12+
}
13+
}

0 commit comments

Comments
 (0)