done 沃尔玛一个后端组的lru OA:内部类直接粘贴到class里面,调用方法在外面。


lru cache的调用,

public static List<Integer> solve(int capacity,List<String> ar){


    }

 

内部类直接粘贴到class里面,调用方法在外面。lru cach直接贴过去。耗时太久了//更正:其实应该是main和lru的class分开,看廖雪峰教程:

https://www.liaoxuefeng.com/wiki/1252599548343744/1376414781669409
抓作弊的浏览器,另一台电脑用mac剪切板复制粘贴就可以
equals()判断字符串是否相等
required variable found value说明arraylist不能赋值,给array赋值再粘贴过去就可以

我写的不对的代码:

import java.util.*;
import java.io.*;
import java.math.*;




class Outcome {

    /*
     * Implement method/function with name 'solve' below.
     * The function accepts following as parameters.
     *  1. capacity is of type int.
     *  2. ar is of type List<String>.
     * return List<Integer>.
     */
     
    private class Node {
            Node prev;
            Node next;
            int key;
            int value;
            
            public Node(int key, int value) {
                this.key = key;
                this.value = value;
                this.prev = null;
                this.next = null;
            }
        }
        
    private int capacity;
    private HashMap<Integer,Node> hs = new HashMap<Integer,Node>();
    private Node head = new Node(-1,-1);
    private Node tail = new Node(-1,-1);
    
    public Outcome() {
        this.capacity = capacity;
        tail.prev = head;
        head.next = tail;
    }
    
        /*
     * @param key: An integer
     * @return: An integer
     */
    public int get(int key) {
        if (!hs.containsKey(key)) {
            return -1;
        }
        
        //remove the selected node
        Node curt = hs.get(key);
        curt.prev.next = curt.next;
        curt.next.prev = curt.prev;
        //move_to_tail since it's visited
        move_to_tail(curt);
        //return
        return hs.get(key).value;
    }
    
        /*
     * @param key: An integer
     * @param value: An integer
     * @return: nothing
     */
    public void put(int key, int value) {
        //if exists, renew the key's value
        if (get(key) != -1) {
            hs.get(key).value = value;
            return ;//
        }
        //if full, remove head.next in order to put in the new node
        if (hs.size() == capacity) {
            hs.remove(head.next.key);
            head.next = head.next.next;
            head.next.prev = head;
        }
        //new insert
        Node insert = new Node(key,value);
        hs.put(key,insert);
        move_to_tail(insert);
    }
    
        private void move_to_tail(Node curt) {
        curt.prev = tail.prev;
        tail.prev = curt;
        curt.prev.next = curt;
        curt.next = tail;
    }

    public static List<Integer> solve(int capacity,List<String> ar){
        //Write your code here
        Outcome outcome = new Outcome();
        outcome.capacity = capacity;
        List<Integer> res = new LinkedList<>();
        
        for (int i = 0; i < ar.size(); i++) {
            if (ar.get(i) == "GET") {
                Integer getRes = Integer.valueOf(outcome.get(Integer.valueOf(ar.get(i++))));
                res.add(getRes);
            }else if (ar.get(i) == "PUT") {
                outcome.put(Integer.valueOf(ar.get(i++)), Integer.valueOf(ar.get(i++)));
            }
        }
        
        return res; //return type "List<Integer>".
    }

}

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getProperty("OUTPUT_FILE_PATH")));
        bufferedWriter.write("\n");
        bufferedWriter.close();
        bufferedWriter = new BufferedWriter(new FileWriter(System.getProperty("OUTPUT_FILE_PATH"),true));
        int capacity = Integer.parseInt(bufferedReader.readLine().trim());

        int arCount = Integer.parseInt(bufferedReader.readLine().trim());

        List<String> ar = new ArrayList<>();

        String[] artempItems = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

        for (int i = 0; i < arCount; i++) {
            String arItem = artempItems[i];
            ar.add(arItem);
        }

        List<Integer> outcome = Outcome.solve(capacity,ar);

        for(int i=0;i<outcome.size();i++) {
            bufferedWriter.write(outcome.get(i).toString());
            if(i<outcome.size()-1)
                bufferedWriter.write(" ");
        }

        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

 

posted @ 2021-08-10 08:19  苗妙苗  阅读(36)  评论(0编辑  收藏  举报