首页 / 算法 / amazon 汇总 算法
amazon 汇总 算法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了amazon 汇总 算法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3407字,纯文字阅读大概需要5分钟。
内容图文
![amazon 汇总 算法](/upload/InfoBanner/zyjiaocheng/1247/77c9e1c6574245a08489d9e5eed7f825.jpg)
7. write a function cn random an array.
public class xiaodan_random { Random rand = new Random(); publicvoid swap(int[] array, int i, int j){ int buf = array[i]; array[i] = array[j]; array[j] = buf; } publicvoid 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); } } publicstaticvoid main(String[] args) { // TODO Auto-generated method stubint[] 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] +", "); } } publicstaticvoid 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; publicint count; public Node(String name){ this.name = name; count = 0; } } public LFU(){ queue = new PriorityQueue<Node>(MAX,new myCompartor()); map = new HashMap<String,Node>(); } publicsynchronizedvoid 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); } } publicvoid 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 publicint compare(Node o1, Node o2) { // TODO Auto-generated method stubreturn o1.count - o2.count; } } publicvoid 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 */publicstaticvoid 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(); } } }
原文:http://www.cnblogs.com/leetcode/p/3872968.html
内容总结
以上是互联网集市为您收集整理的amazon 汇总 算法全部内容,希望文章能够帮你解决amazon 汇总 算法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。