amazon 汇总 算法

 

7. write a function cn random an array.

复制代码
public class xiaodan_random {
    Random rand = new Random();

    public void swap(int[] array, int i, int j){
        int buf = array[i];
        array[i] = array[j];
        array[j] = buf;
    }
    
    public void random(int[] array){
        for(int i=0; i<array.length; i++){
            int ran = rand.nextInt(array.length);
           // System.out.println("this random number == "+ ran);
            swap(array,i,ran);
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] array = {1,2,3,4,5};
        xiaodan_random application = new xiaodan_random();
        while(true){
        application.random(array);
        
        System.out.println("result :");
        for(int i =0; i< array.length; i++)
            System.out.print(array[i]+ " ");
            System.out.println();
    }
    }

}
复制代码

13.  know a range. liner run time sort a array.

(radix sort)

Problem Statement
Given an integer array of length N, containing values in the range 1,2,3…N^2. Sort the array in O(N) time.

 

复制代码
//http://www.geeksforgeeks.org/sort-n-numbers-range-0-n2-1-linear-time/

public class RadixSort {
 
    public void radixSort(int arr[], int maxDigits){
        int exp = 1;//10^0;
        for(int i =0; i < maxDigits; i++){
            ArrayList bucketList[] = new ArrayList[10];
            for(int k=0; k < 10; k++){
                bucketList[k] = new ArrayList();
            }
            for(int j =0; j < arr.length; j++){
                int number = (arr[j]/exp)%10;
                bucketList[number].add(arr[j]);
            }
            exp *= 10;
            int index =0;
            for(int k=0; k < 10; k++){
                for(int num: bucketList[k]){
                    arr[index] = num;
                    index++;
                }
            }
        }
 
        System.out.println("Sorted numbers");
        for(int i =0; i < arr.length; i++){
            System.out.print(arr[i] +", ");
        }
    }
 
    public static void main(String[] argv){
        int n = 5;
        int arr[] = {1,4,2,3,5,10,8};
        new RadixSort().radixSort(arr, 2);
    }
}
复制代码

 

10 

设计一个系访问这个系可以添加item, 可以返回10分内被访

次数最多的排序 (LFU)

复制代码
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Scanner;


// fixed size priority queue;

public class LFU {
    
    private final int MAX = 3;

    PriorityQueue<Node> queue = new PriorityQueue<Node>();
    HashMap<String,Node> map = new  HashMap<String,Node>();
    
    class Node{
        String name;
        public int count;
        public Node(String name){
            this.name = name;
            count = 0;
        }
    }
    
    public LFU(){
        queue = new PriorityQueue<Node>(MAX,new myCompartor());
        map = new  HashMap<String,Node>();
    }
    
    public synchronized void  put(String str){
        if(!map.containsKey(str)){
            Node node = new Node(str);
            node.count = 1;
            map.put(str, node);
            queue_add(node);
        }else{
            //map contains the node, delete from heap frist and add again 
            Node node = map.get(str);
            queue.remove(node);
            node.count += 1;
            queue_add(node);
        }
    }
    
    public void queue_add(Node node){
        if(queue.size()>=MAX){
            if(queue.peek().count < node.count){
                queue.poll();
                queue.add(node);
            }
        }else{
            queue.add(node);
        }
    }
    

    class myCompartor implements Comparator<Node>{

        @Override
        public int compare(Node o1, Node o2) {
            // TODO Auto-generated method stub
            return o1.count - o2.count;
        }
        
    }
        
    
    public void print(){
        Iterator<Node> itr = queue.iterator();
        while(itr.hasNext()){
            Node node = itr.next();
            System.out.println(node.name + " " + node.count);
        }
        System.out.println();
    }
    

    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LFU lfu = new LFU();
        while(true){
            Scanner san = new Scanner(System.in); 
            String str = san.next();
            lfu.put(str);
            lfu.print();
        }

    }

}
复制代码

 

posted on 2014-07-28 13:37  brave_bo  阅读(209)  评论(0)    收藏  举报

< 2025年6月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 1 2 3 4 5
6 7 8 9 10 11 12

导航

统计

点击右上角即可分享
微信分享提示