Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add solutions to lc problem: No.2899 #1809

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
acbin merged 1 commit into main from dev
Oct 15, 2023
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
100 changes: 97 additions & 3 deletions solution/2800-2899/2899.Last Visited Integers/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,128 @@

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

**方法一:模拟**

我们直接根据题意模拟即可。在实现上,我们使用一个数组 $nums$ 来存储遍历过的整数,使用一个整数 $k$ 来记录当前连续的 $prev$ 字符串数目。如果当前字符串是 $prev,ドル那么我们就从 $nums$ 中取出第 $|nums| - k$ 个整数,如果不存在,那么就返回 $-1$。

时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 是数组 $words$ 的长度。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def lastVisitedIntegers(self, words: List[str]) -> List[int]:
nums = []
ans = []
k = 0
for w in words:
if w == "prev":
k += 1
i = len(nums) - k
ans.append(-1 if i < 0 else nums[i])
else:
k = 0
nums.append(int(w))
return ans
```

### **Java**

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

```java

class Solution {
public List<Integer> lastVisitedIntegers(List<String> words) {
List<Integer> nums = new ArrayList<>();
List<Integer> ans = new ArrayList<>();
int k = 0;
for (var w : words) {
if ("prev".equals(w)) {
++k;
int i = nums.size() - k;
ans.add(i < 0 ? -1 : nums.get(i));
} else {
k = 0;
nums.add(Integer.valueOf(w));
}
}
return ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
vector<int> lastVisitedIntegers(vector<string>& words) {
vector<int> nums;
vector<int> ans;
int k = 0;
for (auto& w : words) {
if (w == "prev") {
++k;
int i = nums.size() - k;
ans.push_back(i < 0 ? -1 : nums[i]);
} else {
k = 0;
nums.push_back(stoi(w));
}
}
return ans;
}
};
```

### **Go**

```go
func lastVisitedIntegers(words []string) (ans []int) {
nums := []int{}
k := 0
for _, w := range words {
if w == "prev" {
k++
i := len(nums) - k
if i < 0 {
ans = append(ans, -1)
} else {
ans = append(ans, nums[i])
}
} else {
k = 0
x, _ := strconv.Atoi(w)
nums = append(nums, x)
}
}
return
}
```

### **TypeScript**

```ts
function lastVisitedIntegers(words: string[]): number[] {
const nums: number[] = [];
const ans: number[] = [];
let k = 0;
for (const w of words) {
if (w === 'prev') {
++k;
const i = nums.length - k;
ans.push(i < 0 ? -1 : nums[i]);
} else {
k = 0;
nums.push(+w);
}
}
return ans;
}
```

### **...**
Expand Down
100 changes: 97 additions & 3 deletions solution/2800-2899/2899.Last Visited Integers/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,124 @@ For &quot;prev&quot; at index = 4, last visited integer will be 1 as there are a

## Solutions

**Solution 1: Simulation**

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$.

The time complexity is $O(n),ドル where $n$ is the length of the array $words$. The space complexity is $O(n)$.

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def lastVisitedIntegers(self, words: List[str]) -> List[int]:
nums = []
ans = []
k = 0
for w in words:
if w == "prev":
k += 1
i = len(nums) - k
ans.append(-1 if i < 0 else nums[i])
else:
k = 0
nums.append(int(w))
return ans
```

### **Java**

```java

class Solution {
public List<Integer> lastVisitedIntegers(List<String> words) {
List<Integer> nums = new ArrayList<>();
List<Integer> ans = new ArrayList<>();
int k = 0;
for (var w : words) {
if ("prev".equals(w)) {
++k;
int i = nums.size() - k;
ans.add(i < 0 ? -1 : nums.get(i));
} else {
k = 0;
nums.add(Integer.valueOf(w));
}
}
return ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
vector<int> lastVisitedIntegers(vector<string>& words) {
vector<int> nums;
vector<int> ans;
int k = 0;
for (auto& w : words) {
if (w == "prev") {
++k;
int i = nums.size() - k;
ans.push_back(i < 0 ? -1 : nums[i]);
} else {
k = 0;
nums.push_back(stoi(w));
}
}
return ans;
}
};
```

### **Go**

```go
func lastVisitedIntegers(words []string) (ans []int) {
nums := []int{}
k := 0
for _, w := range words {
if w == "prev" {
k++
i := len(nums) - k
if i < 0 {
ans = append(ans, -1)
} else {
ans = append(ans, nums[i])
}
} else {
k = 0
x, _ := strconv.Atoi(w)
nums = append(nums, x)
}
}
return
}
```

### **TypeScript**

```ts
function lastVisitedIntegers(words: string[]): number[] {
const nums: number[] = [];
const ans: number[] = [];
let k = 0;
for (const w of words) {
if (w === 'prev') {
++k;
const i = nums.length - k;
ans.push(i < 0 ? -1 : nums[i]);
} else {
k = 0;
nums.push(+w);
}
}
return ans;
}
```

### **...**
Expand Down
19 changes: 19 additions & 0 deletions solution/2800-2899/2899.Last Visited Integers/Solution.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
vector<int> lastVisitedIntegers(vector<string>& words) {
vector<int> nums;
vector<int> ans;
int k = 0;
for (auto& w : words) {
if (w == "prev") {
++k;
int i = nums.size() - k;
ans.push_back(i < 0 ? -1 : nums[i]);
} else {
k = 0;
nums.push_back(stoi(w));
}
}
return ans;
}
};
20 changes: 20 additions & 0 deletions solution/2800-2899/2899.Last Visited Integers/Solution.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
func lastVisitedIntegers(words []string) (ans []int) {
nums := []int{}
k := 0
for _, w := range words {
if w == "prev" {
k++
i := len(nums) - k
if i < 0 {
ans = append(ans, -1)
} else {
ans = append(ans, nums[i])
}
} else {
k = 0
x, _ := strconv.Atoi(w)
nums = append(nums, x)
}
}
return
}
18 changes: 18 additions & 0 deletions solution/2800-2899/2899.Last Visited Integers/Solution.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public List<Integer> lastVisitedIntegers(List<String> words) {
List<Integer> nums = new ArrayList<>();
List<Integer> ans = new ArrayList<>();
int k = 0;
for (var w : words) {
if ("prev".equals(w)) {
++k;
int i = nums.size() - k;
ans.add(i < 0 ? -1 : nums.get(i));
} else {
k = 0;
nums.add(Integer.valueOf(w));
}
}
return ans;
}
}
14 changes: 14 additions & 0 deletions solution/2800-2899/2899.Last Visited Integers/Solution.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def lastVisitedIntegers(self, words: List[str]) -> List[int]:
nums = []
ans = []
k = 0
for w in words:
if w == "prev":
k += 1
i = len(nums) - k
ans.append(-1 if i < 0 else nums[i])
else:
k = 0
nums.append(int(w))
return ans
16 changes: 16 additions & 0 deletions solution/2800-2899/2899.Last Visited Integers/Solution.ts
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function lastVisitedIntegers(words: string[]): number[] {
const nums: number[] = [];
const ans: number[] = [];
let k = 0;
for (const w of words) {
if (w === 'prev') {
++k;
const i = nums.length - k;
ans.push(i < 0 ? -1 : nums[i]);
} else {
k = 0;
nums.push(+w);
}
}
return ans;
}

AltStyle によって変換されたページ (->オリジナル) /