All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2801_2900.s2899_last_visited_integers.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g2801_2900.s2899_last_visited_integers;

// #Easy #Array #String #Simulation #2023_12_20_Time_2_ms_(96.41%)_Space_44.5_MB_(5.24%)

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List lastVisitedIntegers(List words) {
        List prevEle = new ArrayList<>();
        List res = new ArrayList<>();
        int count = 0;
        for (int i = 0; i < words.size(); i++) {
            if (!words.get(i).equals("prev")) {
                count = 0;
                prevEle.add(words.get(i));
                continue;
            }
            if (count >= prevEle.size()) {
                res.add(-1);
            } else {
                res.add(Integer.parseInt(prevEle.get(prevEle.size() - count - 1)));
            }
            count++;
        }
        return res;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy