标签:style blog http java color strong io for
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(); } } }
标签:style blog http java color strong io for
原文地址:http://www.cnblogs.com/leetcode/p/3872968.html