Amazon coding 题解答

没有过了test,之后回来跟新


1 LRU

/**
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.


get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
*/


//you should have a data structure that you set the capacity,这个解超时了,也不对。没有考虑get要改变Priority

public class LRUCache {
    int numOfItems;
    int capacity;
    //This is to store the key value that has been visited before
    PriorityQueue<pair> queue;
    // //This is to store the key value pair
    // HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    //This should get a priority queue
    //值小的在栈顶,有益处吧

    public class pair{
      int key;
      int value;
      int timeBit;

      public void pair(int key, int value, int timeBit){
        this.key = key;
        this.value = value;
        this.timeBit = timeBit;
      }
    }

    //The way to implement the priority queue in
    public class pairComparator implements Comparator<pair>{
      public int compare(pair a, pair b){
        if(a.timeBit > b.timeBit)
          return 1;
        else if(a.timeBit < b.timeBit)
          return -1;
        return 0;
      }
    }

    public LRUCache(int capacity) {
        this.capacity = capacity;
        Comparator<pair> comparator = new pairComparator();
        queue = new PriorityQueue(capacity,comparator);
        numOfItems = 0;
    }

    //Loop if the cache contatins the value;
    public int get(int key) {
      //loop all the priority queue and find if the key is in the priority queue
      Iterator iterator = queue.iterator();
      while(iterator.hasNext()){
        pair tem = (pair)iterator.next();
        if(tem.key == key)
          return tem.value;
      }
    }

    public void set(int key, int value) {
        //deal with the corner condition that the number of the items equal to 0
        if(capacity <= 0)
          return;

        //The condition that the number of the items is less than the capacity
        if(numOfItems < capacity){
          Iterator iterator = queue.iterator();
          while(iterator.hasNext()){
              pair tem = (pair)iterator.next();
              tem.value++;
          }

          pair newPair = new pair(key, value, 1);
          queue.add(newPair);
          numOfItems++;
        }

        //the condition that the number of items is equal to the capacity
        else{
          queue.pop();
          Iterator iterator = queue.iterator();
          while(iterator.hasNext()){
              pair tem = (pair)iterator.next();
              tem.value++;
          }
          pair pair = new pair(key, value , 1);
          queue.add(pair);
          numOfItems++;
        }
    }
}


2 insert into a sorted circular list

public class circularList{
  ListNode head;
  ListNode tail;
  public circularList(){
    head = new ListNode(-1);
    tail = new ListNode(Max.integer);
    head.pr = tail;
    head.next = tail;
    tail.pre = head;
    tail.next = head;
  }
  public insert(int tem){
    ListNode curr = head;
    //find the first position where value is larger than the current value
    while(tem > curr.val)
    {
      curr = head.next;
    }
    ListNode node = new ListNode(tem);
    node.next = curr;
    node.pre = curr.pre;
    curr.pre.next = node;
    curr.pre = node;
  }
}

//This is a circular listnode in a circular linkedList
public class ListNode{
  ListNode pre;
  ListNode next;
  int val;
  //This is a constructor confuction
  public ListNode(int val){
    this.val = val;
  }
  public ListNode(ListNode pre, ListNode next, int val){
    this.pre = pre;
    this.next = next;
    this.val = val;
  }
}

mate in Maze, 没有通过

public boolean solve(int[][]maze){
  int rowNum = maze.length;
   // when the maze is empty, return true;
  if(rowNum <= 0)
    return true;
  int colNum = maze[0].length;
  if(colNum <=0)
    return true;
  int[][] solution = new int[rowNum][colNum];
  if(helper(maze, 0,0)){
    printSolution(solution);
    return true;
  }
  else{
    System.out.print("there is no such a solution")
    return false;
  }
}
//assume that the 0 in the matrix is the obscles, and 1 is the way we can pass by
public boolean helper(int[][]maze, int row, int col, int[][] solution){
  int rowNum = maze.length;
  int columnNum = maze[0].length;
  if(row >= rowNum || col >= columnNum)
    return false;
  if(row == rowNum - 1 && col == columnNum - 1 && maze[row][col]){
    return true;
    solution[row][col] = 1;
  }
  if(maze[row][col]){
    solution[row][col] = 1;
    if(helper(maze, row + 1, col))
      return true;
    if(helper(maze, row, col + 1))
      return true;
    maze[row][col] = 0;
    solution[row][col] = 0;
    return false;
  }
  return false;
}
//This is to print the solution
public void printSolution(int[][]solution){
  for(int[]row : array){
    System.out.println(Arrays.toString(row));
  }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值