Skip to content

Commit 2931dbf

Browse files
authored
Added solution for 2899
1 parent 0ef4a87 commit 2931dbf

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

02899. Last Visited Integers.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
// Create an output vector and a num vector. Num vector will contain list of numbers and a marker variable will keep a track of which was the last 'prev' value that was added in output vector.
3+
pub fn last_visited_integers(words: Vec<String>) -> Vec<i32> {
4+
let mut output = Vec::new();
5+
let mut nums: Vec<i32> = Vec::new();
6+
let mut marker = -1;
7+
for word in words {
8+
if word == "prev" {
9+
if marker > -1 {
10+
let s = nums[marker as usize];
11+
marker -= 1;
12+
output.push(s);
13+
} else {
14+
output.push(-1);
15+
marker = -1;
16+
}
17+
} else {
18+
nums.push(word.parse::<i32>().unwrap());
19+
marker = (nums.len() as i32) - 1;
20+
}
21+
}
22+
return output;
23+
}
24+
}

0 commit comments

Comments
 (0)