-
理解位运算及使用场景
2015-11-20 13:33我写过类似的博文,果然是英雄所见略同🐶
http://www.flcoder.com/articles/2014/11/10/1415608470246.html -
算法练习:求最大值和最小值
2015-11-20 12:46调用及测试类
var code =
import java.util.List;public class Operation {
public static void main(String[] args) { ArrayQueue<Integer> queue = new ArrayQueue<Integer>(10); for(int i = 0 ;i< 10;i++){ queue.push(i); } System.out.println(queue.capacity); System.out.println(queue.peak()); System.out.println(queue.poll()); queue.push(new Integer(10)); queue.push(new Integer(11)); queue.push(new Integer(12)); queue.push(new Integer(13)); queue.push(new Integer(14)); queue.push(new Integer(15)); System.out.println(queue.data); System.out.println("最大值:"+Operation.max(queue.data)); System.out.println("最小值:"+Operation.min(queue.data)); } public static int max(List<Integer> list){ int max = list.get(0); for(Integer cur: list){ max = max <cur? cur:max; } return max; } public static int min(List<Integer> list){ int min = list.get(0); for(Integer cur: list){ min = min >cur? cur:min; } return min; }
}
-
算法练习:求最大值和最小值
2015-11-20 12:45var code=" import java.util.ArrayList;
import java.util.List;
public class ArrayQueue implements Queue {
List data;
int capacity;public ArrayQueue(int capacity) { data = new ArrayList<T>(); this.capacity = capacity; } public void push(T t) { if (this.capacity == data.size()) { data.remove(0); } data.add(t); } public T poll() { T t = data.remove(0); return t; } public int size() { return data.size(); } public T peak() { return data.get(data.size() - 1); } public boolean isEmpty() { return 0 == data.size(); } public boolean isFull() { return capacity == data.size(); } public static void main(String[] args) { ArrayQueue<Integer> queue = new ArrayQueue<Integer>(10); for (int i = 0; i < 10; i++) { queue.push(i); } System.out.println(queue.capacity); System.out.println(queue.peak()); System.out.println(queue.poll()); queue.push(new Integer(11)); System.out.println(queue.data); }
}
"
-
算法练习:求最大值和最小值
2015-11-20 12:43我来个可读性好点的,思路是使用固定长度(长度可自定义,按这里的要求设为 10 就可以)队列数据结构,先进先出。
var code = "code"public interface Queue<T> { public void push(T t); public T poll(); public int size(); public T peak(); public boolean isEmpty(); public boolean isFull(); }