您的位置:首页 > 其它

Amazon coding 题解答

2016-02-14 00:45 351 查看
没有过了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));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: