Skip to content

Commit cb9f1f9

Browse files
authored
feat: add solutions to lc problem: No.2899 (#1809)
No.2899.Last Visited Integers
1 parent bf5008a commit cb9f1f9

File tree

7 files changed

+281
-6
lines changed

7 files changed

+281
-6
lines changed

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

+97-3
Original file line numberDiff line numberDiff line change
@@ -54,34 +54,128 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
**方法一:模拟**
58+
59+
我们直接根据题意模拟即可。在实现上,我们使用一个数组 $nums$ 来存储遍历过的整数,使用一个整数 $k$ 来记录当前连续的 $prev$ 字符串数目。如果当前字符串是 $prev$,那么我们就从 $nums$ 中取出第 $|nums| - k$ 个整数,如果不存在,那么就返回 $-1$。
60+
61+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $words$ 的长度。
62+
5763
<!-- tabs:start -->
5864

5965
### **Python3**
6066

6167
<!-- 这里可写当前语言的特殊实现逻辑 -->
6268

6369
```python
64-
70+
class Solution:
71+
def lastVisitedIntegers(self, words: List[str]) -> List[int]:
72+
nums = []
73+
ans = []
74+
k = 0
75+
for w in words:
76+
if w == "prev":
77+
k += 1
78+
i = len(nums) - k
79+
ans.append(-1 if i < 0 else nums[i])
80+
else:
81+
k = 0
82+
nums.append(int(w))
83+
return ans
6584
```
6685

6786
### **Java**
6887

6988
<!-- 这里可写当前语言的特殊实现逻辑 -->
7089

7190
```java
72-
91+
class Solution {
92+
public List<Integer> lastVisitedIntegers(List<String> words) {
93+
List<Integer> nums = new ArrayList<>();
94+
List<Integer> ans = new ArrayList<>();
95+
int k = 0;
96+
for (var w : words) {
97+
if ("prev".equals(w)) {
98+
++k;
99+
int i = nums.size() - k;
100+
ans.add(i < 0 ? -1 : nums.get(i));
101+
} else {
102+
k = 0;
103+
nums.add(Integer.valueOf(w));
104+
}
105+
}
106+
return ans;
107+
}
108+
}
73109
```
74110

75111
### **C++**
76112

77113
```cpp
78-
114+
class Solution {
115+
public:
116+
vector<int> lastVisitedIntegers(vector<string>& words) {
117+
vector<int> nums;
118+
vector<int> ans;
119+
int k = 0;
120+
for (auto& w : words) {
121+
if (w == "prev") {
122+
++k;
123+
int i = nums.size() - k;
124+
ans.push_back(i < 0 ? -1 : nums[i]);
125+
} else {
126+
k = 0;
127+
nums.push_back(stoi(w));
128+
}
129+
}
130+
return ans;
131+
}
132+
};
79133
```
80134
81135
### **Go**
82136
83137
```go
138+
func lastVisitedIntegers(words []string) (ans []int) {
139+
nums := []int{}
140+
k := 0
141+
for _, w := range words {
142+
if w == "prev" {
143+
k++
144+
i := len(nums) - k
145+
if i < 0 {
146+
ans = append(ans, -1)
147+
} else {
148+
ans = append(ans, nums[i])
149+
}
150+
} else {
151+
k = 0
152+
x, _ := strconv.Atoi(w)
153+
nums = append(nums, x)
154+
}
155+
}
156+
return
157+
}
158+
```
84159

160+
### **TypeScript**
161+
162+
```ts
163+
function lastVisitedIntegers(words: string[]): number[] {
164+
const nums: number[] = [];
165+
const ans: number[] = [];
166+
let k = 0;
167+
for (const w of words) {
168+
if (w === 'prev') {
169+
++k;
170+
const i = nums.length - k;
171+
ans.push(i < 0 ? -1 : nums[i]);
172+
} else {
173+
k = 0;
174+
nums.push(+w);
175+
}
176+
}
177+
return ans;
178+
}
85179
```
86180

87181
### **...**

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

+97-3
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,124 @@ For &quot;prev&quot; at index = 4, last visited integer will be 1 as there are a
4848

4949
## Solutions
5050

51+
**Solution 1: Simulation**
52+
53+
We can directly simulate according to the problem statement. In the implementation, we use an array $nums$ to store the traversed integers, and an integer $k$ to record the current number of consecutive $prev$ strings. If the current string is $prev$, we take out the $|nums| - k-th$ integer from $nums$. If it does not exist, we return $-1$.
54+
55+
The time complexity is $O(n)$, where $n$ is the length of the array $words$. The space complexity is $O(n)$.
56+
5157
<!-- tabs:start -->
5258

5359
### **Python3**
5460

5561
```python
56-
62+
class Solution:
63+
def lastVisitedIntegers(self, words: List[str]) -> List[int]:
64+
nums = []
65+
ans = []
66+
k = 0
67+
for w in words:
68+
if w == "prev":
69+
k += 1
70+
i = len(nums) - k
71+
ans.append(-1 if i < 0 else nums[i])
72+
else:
73+
k = 0
74+
nums.append(int(w))
75+
return ans
5776
```
5877

5978
### **Java**
6079

6180
```java
62-
81+
class Solution {
82+
public List<Integer> lastVisitedIntegers(List<String> words) {
83+
List<Integer> nums = new ArrayList<>();
84+
List<Integer> ans = new ArrayList<>();
85+
int k = 0;
86+
for (var w : words) {
87+
if ("prev".equals(w)) {
88+
++k;
89+
int i = nums.size() - k;
90+
ans.add(i < 0 ? -1 : nums.get(i));
91+
} else {
92+
k = 0;
93+
nums.add(Integer.valueOf(w));
94+
}
95+
}
96+
return ans;
97+
}
98+
}
6399
```
64100

65101
### **C++**
66102

67103
```cpp
68-
104+
class Solution {
105+
public:
106+
vector<int> lastVisitedIntegers(vector<string>& words) {
107+
vector<int> nums;
108+
vector<int> ans;
109+
int k = 0;
110+
for (auto& w : words) {
111+
if (w == "prev") {
112+
++k;
113+
int i = nums.size() - k;
114+
ans.push_back(i < 0 ? -1 : nums[i]);
115+
} else {
116+
k = 0;
117+
nums.push_back(stoi(w));
118+
}
119+
}
120+
return ans;
121+
}
122+
};
69123
```
70124
71125
### **Go**
72126
73127
```go
128+
func lastVisitedIntegers(words []string) (ans []int) {
129+
nums := []int{}
130+
k := 0
131+
for _, w := range words {
132+
if w == "prev" {
133+
k++
134+
i := len(nums) - k
135+
if i < 0 {
136+
ans = append(ans, -1)
137+
} else {
138+
ans = append(ans, nums[i])
139+
}
140+
} else {
141+
k = 0
142+
x, _ := strconv.Atoi(w)
143+
nums = append(nums, x)
144+
}
145+
}
146+
return
147+
}
148+
```
74149

150+
### **TypeScript**
151+
152+
```ts
153+
function lastVisitedIntegers(words: string[]): number[] {
154+
const nums: number[] = [];
155+
const ans: number[] = [];
156+
let k = 0;
157+
for (const w of words) {
158+
if (w === 'prev') {
159+
++k;
160+
const i = nums.length - k;
161+
ans.push(i < 0 ? -1 : nums[i]);
162+
} else {
163+
k = 0;
164+
nums.push(+w);
165+
}
166+
}
167+
return ans;
168+
}
75169
```
76170

77171
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<int> lastVisitedIntegers(vector<string>& words) {
4+
vector<int> nums;
5+
vector<int> ans;
6+
int k = 0;
7+
for (auto& w : words) {
8+
if (w == "prev") {
9+
++k;
10+
int i = nums.size() - k;
11+
ans.push_back(i < 0 ? -1 : nums[i]);
12+
} else {
13+
k = 0;
14+
nums.push_back(stoi(w));
15+
}
16+
}
17+
return ans;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func lastVisitedIntegers(words []string) (ans []int) {
2+
nums := []int{}
3+
k := 0
4+
for _, w := range words {
5+
if w == "prev" {
6+
k++
7+
i := len(nums) - k
8+
if i < 0 {
9+
ans = append(ans, -1)
10+
} else {
11+
ans = append(ans, nums[i])
12+
}
13+
} else {
14+
k = 0
15+
x, _ := strconv.Atoi(w)
16+
nums = append(nums, x)
17+
}
18+
}
19+
return
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public List<Integer> lastVisitedIntegers(List<String> words) {
3+
List<Integer> nums = new ArrayList<>();
4+
List<Integer> ans = new ArrayList<>();
5+
int k = 0;
6+
for (var w : words) {
7+
if ("prev".equals(w)) {
8+
++k;
9+
int i = nums.size() - k;
10+
ans.add(i < 0 ? -1 : nums.get(i));
11+
} else {
12+
k = 0;
13+
nums.add(Integer.valueOf(w));
14+
}
15+
}
16+
return ans;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def lastVisitedIntegers(self, words: List[str]) -> List[int]:
3+
nums = []
4+
ans = []
5+
k = 0
6+
for w in words:
7+
if w == "prev":
8+
k += 1
9+
i = len(nums) - k
10+
ans.append(-1 if i < 0 else nums[i])
11+
else:
12+
k = 0
13+
nums.append(int(w))
14+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function lastVisitedIntegers(words: string[]): number[] {
2+
const nums: number[] = [];
3+
const ans: number[] = [];
4+
let k = 0;
5+
for (const w of words) {
6+
if (w === 'prev') {
7+
++k;
8+
const i = nums.length - k;
9+
ans.push(i < 0 ? -1 : nums[i]);
10+
} else {
11+
k = 0;
12+
nums.push(+w);
13+
}
14+
}
15+
return ans;
16+
}

0 commit comments

Comments
 (0)