We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b4dc59f commit 9ae580aCopy full SHA for 9ae580a
Easy/Last Visited Integers.java
@@ -0,0 +1,17 @@
1
+class Solution {
2
+ public List<Integer> lastVisitedIntegers(List<String> words) {
3
+ List<Integer> result = new ArrayList<>();
4
+ List<Integer> numbers = new ArrayList<>();
5
+ int cursor = -1;
6
+ for (String word : words) {
7
+ if (word.equals("prev")) {
8
+ result.add(cursor == -1 ? -1 : numbers.get(cursor));
9
+ cursor = cursor == -1 ? -1 : (cursor - 1);
10
+ } else {
11
+ numbers.add(Integer.parseInt(word));
12
+ cursor = numbers.size() - 1;
13
+ }
14
15
+ return result;
16
17
+}
0 commit comments