From b650b738aa992406fcf9deaf807b9a4731542cd8 Mon Sep 17 00:00:00 2001 From: liupeng Date: Wed, 18 Nov 2020 11:36:39 +0800 Subject: [PATCH 1/6] T0 add datastructure code --- src/basic/io/ServerThread.java | 48 ++ src/basic/io/SocketServer.java | 25 + src/concurrency/Prize.java | 40 ++ src/concurrency/Test.java | 168 ++++++ src/concurrency/Testforeach.java | 30 + .../BinarySearchTree/BinarySearchTree.java | 167 ++++++ .../BinarySearchTree/TreeNode.java | 19 + src/datastructure/Fibonacci.java | 30 - src/datastructure/Stack.java | 80 --- src/datastructure/backtrack/EightQueen.java | 90 +++ src/datastructure/graph/Graph.java | 134 +++++ src/datastructure/heap/MinHeap.java | 164 ++++++ src/datastructure/list/LinkedList.java | 351 +++++++++++ src/datastructure/list/Node.java | 34 ++ src/datastructure/queue/LinkedQueue.java | 101 ++++ src/datastructure/queue/Node.java | 34 ++ .../queue/OptimizationStackQueue.java | 61 ++ src/datastructure/queue/SeqQueue.java | 111 ++++ src/datastructure/queue/StackQueue.java | 70 +++ src/datastructure/sort/BinaryInsertSort.java | 45 ++ src/datastructure/sort/BubbleSort.java | 67 +++ src/datastructure/sort/HeapSort.java | 64 ++ src/datastructure/sort/MergeSort.java | 85 +++ src/datastructure/sort/QuickSort.java | 84 +++ .../sort/QuickSort_PartitionOnly.java | 49 ++ src/datastructure/sort/RadixSort.java | 60 ++ src/datastructure/sort/ShellSort.java | 47 ++ .../sort/StraightInsertionSort.java | 30 + .../sort/StraightSelectSort.java | 34 ++ src/datastructure/stack/LinkedListStack.java | 58 ++ src/datastructure/stack/LinkedStack.java | 167 ++++++ src/datastructure/stack/Node.java | 43 ++ src/datastructure/stack/SeqStack.java | 72 +++ .../test/BinarySearchTreeTest.java | 36 ++ src/datastructure/test/BinaryTreeTest.java | 85 +++ src/datastructure/test/LinkedListTest.java | 71 +++ src/datastructure/test/LinkedQueueTest.java | 28 + src/datastructure/test/LinkedStackTest.java | 48 ++ src/datastructure/test/MinHeapTest.java | 25 + .../test/OptimizationStackQueueTest.java | 27 + src/datastructure/test/SeqQueueTest.java | 27 + src/datastructure/test/SortTest.java | 87 +++ src/datastructure/test/StackQueueTest.java | 27 + src/datastructure/test/TestString.java | 32 + src/datastructure/tree/BinaryTree.java | 550 ++++++++++++++++++ src/datastructure/tree/Node.java | 49 ++ src/designpattern/adapter/Adapter.java | 2 +- src/designpattern/myadapter/Voltage220.java | 9 + src/designpattern/myadapter/Voltage5.java | 5 + .../myadapter/VoltageAdapter.java | 16 + 50 files changed, 3675 insertions(+), 111 deletions(-) create mode 100644 src/basic/io/ServerThread.java create mode 100644 src/basic/io/SocketServer.java create mode 100644 src/concurrency/Prize.java create mode 100644 src/concurrency/Test.java create mode 100644 src/concurrency/Testforeach.java create mode 100644 src/datastructure/BinarySearchTree/BinarySearchTree.java create mode 100644 src/datastructure/BinarySearchTree/TreeNode.java delete mode 100644 src/datastructure/Fibonacci.java delete mode 100644 src/datastructure/Stack.java create mode 100644 src/datastructure/backtrack/EightQueen.java create mode 100644 src/datastructure/graph/Graph.java create mode 100644 src/datastructure/heap/MinHeap.java create mode 100644 src/datastructure/list/LinkedList.java create mode 100644 src/datastructure/list/Node.java create mode 100644 src/datastructure/queue/LinkedQueue.java create mode 100644 src/datastructure/queue/Node.java create mode 100644 src/datastructure/queue/OptimizationStackQueue.java create mode 100644 src/datastructure/queue/SeqQueue.java create mode 100644 src/datastructure/queue/StackQueue.java create mode 100644 src/datastructure/sort/BinaryInsertSort.java create mode 100644 src/datastructure/sort/BubbleSort.java create mode 100644 src/datastructure/sort/HeapSort.java create mode 100644 src/datastructure/sort/MergeSort.java create mode 100644 src/datastructure/sort/QuickSort.java create mode 100644 src/datastructure/sort/QuickSort_PartitionOnly.java create mode 100644 src/datastructure/sort/RadixSort.java create mode 100644 src/datastructure/sort/ShellSort.java create mode 100644 src/datastructure/sort/StraightInsertionSort.java create mode 100644 src/datastructure/sort/StraightSelectSort.java create mode 100644 src/datastructure/stack/LinkedListStack.java create mode 100644 src/datastructure/stack/LinkedStack.java create mode 100644 src/datastructure/stack/Node.java create mode 100644 src/datastructure/stack/SeqStack.java create mode 100644 src/datastructure/test/BinarySearchTreeTest.java create mode 100644 src/datastructure/test/BinaryTreeTest.java create mode 100644 src/datastructure/test/LinkedListTest.java create mode 100644 src/datastructure/test/LinkedQueueTest.java create mode 100644 src/datastructure/test/LinkedStackTest.java create mode 100644 src/datastructure/test/MinHeapTest.java create mode 100644 src/datastructure/test/OptimizationStackQueueTest.java create mode 100644 src/datastructure/test/SeqQueueTest.java create mode 100644 src/datastructure/test/SortTest.java create mode 100644 src/datastructure/test/StackQueueTest.java create mode 100644 src/datastructure/test/TestString.java create mode 100644 src/datastructure/tree/BinaryTree.java create mode 100644 src/datastructure/tree/Node.java create mode 100644 src/designpattern/myadapter/Voltage220.java create mode 100644 src/designpattern/myadapter/Voltage5.java create mode 100644 src/designpattern/myadapter/VoltageAdapter.java diff --git a/src/basic/io/ServerThread.java b/src/basic/io/ServerThread.java new file mode 100644 index 0000000..12afc32 --- /dev/null +++ b/src/basic/io/ServerThread.java @@ -0,0 +1,48 @@ +package basic.io; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.net.Socket; + +public class ServerThread implements Runnable { + + private Socket client = null; + + public ServerThread(Socket client) { + this.client = client; + } + + @Override + public void run() { + try { + //获取Socket的输出流,用来向客户端发送数据 + PrintStream out = new PrintStream(client.getOutputStream()); + //获取Socket的输入流,用来接收从客户端发送过来的数据 + BufferedReader buf = new BufferedReader(new InputStreamReader(client.getInputStream())); + boolean flag =true; + while(flag){ + //接收从客户端发送过来的数据 + String str = buf.readLine(); + if(str == null || "".equals(str)){ + flag = false; + }else{ + if("bye".equals(str)){ + flag = false; + }else{ + //将接收到的字符串前面加上echo,发送到对应的客户端 + out.println("echo:" + str); + } + } + } + out.close(); + client.close(); + + } catch (IOException e) { + e.printStackTrace(); + } finally { + } + + } +} diff --git a/src/basic/io/SocketServer.java b/src/basic/io/SocketServer.java new file mode 100644 index 0000000..c3a3449 --- /dev/null +++ b/src/basic/io/SocketServer.java @@ -0,0 +1,25 @@ +package basic.io; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; + +public class SocketServer { + + public static void main(String[] args) throws IOException { + + //服务端 + ServerSocket server = new ServerSocket(8009); + Socket client = null; + boolean f= true; + while (f){ + //等待客户端的连接 + client = server.accept(); + + System.out.println("与客户端连接成功!"); + + + } + + } +} diff --git a/src/concurrency/Prize.java b/src/concurrency/Prize.java new file mode 100644 index 0000000..7d45eac --- /dev/null +++ b/src/concurrency/Prize.java @@ -0,0 +1,40 @@ +package concurrency; + +public class Prize { + private int id;//奖品id + private String prize_name;//奖品名称 + private int prize_amount;//奖品(剩余)数量 + private int prize_weight;//奖品权重 + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getPrize_name() { + return prize_name; + } + + public void setPrize_name(String prize_name) { + this.prize_name = prize_name; + } + + public int getPrize_amount() { + return prize_amount; + } + + public void setPrize_amount(int prize_amount) { + this.prize_amount = prize_amount; + } + + public int getPrize_weight() { + return prize_weight; + } + + public void setPrize_weight(int prize_weight) { + this.prize_weight = prize_weight; + } +} diff --git a/src/concurrency/Test.java b/src/concurrency/Test.java new file mode 100644 index 0000000..90ff016 --- /dev/null +++ b/src/concurrency/Test.java @@ -0,0 +1,168 @@ +package concurrency; + +import java.util.*; + +public class Test { + + + + + public static int getPrizeIndex(List prizes) { + int random = -1; + try{ + //计算总权重 + double sumWeight = 0; + for(Prize p : prizes){ + sumWeight += p.getPrize_weight(); + } + + //产生随机数 + double randomNumber = Math.random(); + + + //根据随机数在所有奖品分布的区域并确定所抽奖品 + double d1 = 0; + double d2 = 0; + for(int i=0;i= d1 && randomNumber <= d2){ + // System.out.println("randomNumber:"+randomNumber+"~~~~~~~d1:"+d1+"~~~~~~~d2:"+d2); + random = i; + break; + } + } + }catch(Exception e){ + System.out.println("生成抽奖随机数出错,出错原因:" +e.getMessage()); + } + return random; + } + + public static void main(String[] agrs) { + + int s = 0; + int[] result=new int[3]; + + List prizes = new ArrayList(); + + + Prize p2 = new Prize(); + p2.setPrize_name("上海紫园1号别墅"); + p2.setPrize_weight(0); + prizes.add(p2); + + Prize p3 = new Prize(); + p3.setPrize_name("奥迪a9"); + p3.setPrize_weight(5); + prizes.add(p3); + + Prize p4 = new Prize(); + p4.setPrize_name("双色球彩票"); + p4.setPrize_weight(5); + prizes.add(p4); + + System.out.println("抽奖开始"); + for (s = 0; s < 15; s++) + { + int selected=getPrizeIndex(prizes); + /* System.out.println("第"+i+"次抽中的奖品为:"+prizes.get(selected).getPrize_name()); + System.out.println(selected);*/ + result[selected]++; + // System.out.println("--------------------------------"); + + + } + System.out.println("抽奖结束"); + System.out.println("每种奖品抽到的数量为:"); + System.out.println("一等奖:"+result[0]); + System.out.println("二等奖:"+result[1]); + System.out.println("三等奖:"+result[2]); + + + /* String conf = "sms:2#call:4#auto:4"; + //解析分流规则 + List pool = new ArrayList<>(); + String[] rules = conf.split("#"); + for (String r : rules) { + String[] rule = r.split(":"); + for (int i=0;i node.data) { + node.right = remove(target, node.right); + // 找到待删除结点,且其左右子树不为空 + } else if (node.left != null && node.right != null) { + // 找到以待删除结点右子树的中序遍历第一个结点(最小结点) + + + tmp = node.right; + while (tmp.left != null) { + tmp = tmp.left; + } + + // 用最小结点补位待删除结点 + node.data = tmp.data; + + // 删除待删除结点右子树上补位结点 + node.right = remove(node.data, node.right); + } else { + if (node.left == null) { + node = node.right; + } else { + node = node.left; + } + } + } + return node; + } + + /** + * @description 中序遍历二叉搜索树,递归算法,升序排序 + * @author rico + * @created 2017年6月3日 下午3:52:54 + * @param root + */ + public void inOrder(TreeNode node) { + if (node != null) { + inOrder(node.left); + System.out.print(root.data + " "); + inOrder(node.right); + } + } + + /** + * @description 打印二叉搜索树 + * @author rico + * @created 2017年6月3日 下午6:08:42 + * @param node + */ + public void printTree(TreeNode node) { + if (node != null) { + System.out.print(node.data); + if (node.left != null || node.right != null) { + System.out.print("("); + printTree(node.left); + System.out.print(","); + printTree(node.right); + System.out.print(")"); + } + } + } + + /** + * @description 访问二叉搜索树的根结点 + * @author rico + * @created 2017年6月3日 下午3:54:49 + * @return + */ + public TreeNode getRoot() { + return root; + } +} diff --git a/src/datastructure/BinarySearchTree/TreeNode.java b/src/datastructure/BinarySearchTree/TreeNode.java new file mode 100644 index 0000000..110f050 --- /dev/null +++ b/src/datastructure/BinarySearchTree/TreeNode.java @@ -0,0 +1,19 @@ + +package datastructure.BinarySearchTree; + +public class TreeNode { + + public int data; + public TreeNode left; + public TreeNode right; + + public TreeNode(int data){ + this.data = data; + } + + @Override + public String toString() { + return "TreeNode [data=" + data + "]"; + } + +} diff --git a/src/datastructure/Fibonacci.java b/src/datastructure/Fibonacci.java deleted file mode 100644 index cc46430..0000000 --- a/src/datastructure/Fibonacci.java +++ /dev/null @@ -1,30 +0,0 @@ -package datastructure; - -interface Generator { - T next(); -} - -public class Fibonacci implements Generator { - - private int count = 0; - - @Override - public Integer next() { - return fibonacci(count++); - } - - public int fibonacci(int n) { - if (n < 2) { - return 1; - } - return fibonacci(n - 2) + fibonacci(n - 1); - } - - public static void main(String[] args) { - Fibonacci fibonacci = new Fibonacci(); - int size = 18; - for (int i = 0; i < size; i++) { - System.out.print(fibonacci.next() + " "); - } - } -} diff --git a/src/datastructure/Stack.java b/src/datastructure/Stack.java deleted file mode 100644 index 36d3b6a..0000000 --- a/src/datastructure/Stack.java +++ /dev/null @@ -1,80 +0,0 @@ -package datastructure; - -/** - * 使用Java泛型实现栈 - * - * @author liu yuning - * - * @param - */ -public class Stack { - - private static class Node { - private U value; - private Node next; - - public Node() { - value = null; - next = null; - } - - public Node(U value, Node next) { - this.value = value; - this.next = next; - } - - /** - * 判断栈是否为空 - * - * @return true 如果为空;否则,false - */ - public boolean empty() { - return value == null && next == null; - } - } - - /** - * 定义栈顶元素 - */ - private Node top = new Node(); - - /** - * 入栈操作 - * - * @param value - * 入栈元素的值 - */ - public void push(T value) { - top = new Node(value, top); - } - - /** - * 出栈操作 - * - * @return 栈顶元素的值 - */ - public T pop() { - T value = top.value; - if (!top.empty()) { - top = top.next; - } - return value; - } - - public static void main(String[] args) { - Stack stack = new Stack(); - String string = "This is a test for stack"; - - System.out.println("入栈元素(String)依次为:"); - for (String nodeIn : string.split(" ")) { - stack.push(nodeIn); - System.out.println(nodeIn); - } - - System.out.println("出栈元素(String)依次为:"); - String nodeOut; - while ((nodeOut = stack.pop()) != null) { - System.out.println(nodeOut); - } - } -} diff --git a/src/datastructure/backtrack/EightQueen.java b/src/datastructure/backtrack/EightQueen.java new file mode 100644 index 0000000..d396774 --- /dev/null +++ b/src/datastructure/backtrack/EightQueen.java @@ -0,0 +1,90 @@ +package datastructure.backtrack; + +import java.util.Date; + +/** + * Title: ˻ʺ(ݹ㷨) Description: 88ĹϰڷŰ˸ʺʹ䲻ܻ๥ + * ʺ󶼲ܴͬһСͬһлͬһбϣжְڷ + * + * @author rico + * @created 2017531 4:54:17 + */ +public class EightQueen { + + private static final short N = 8; // ʹó壬֮Nʺ + private static int count = 0; // + + public static void main(String[] args) { + Date begin = new Date(); + // ʼ̣ȫ0 + short chess[][] = new short[N][N]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + chess[i][j] = 0; + } + } + + putQueenAtRow(chess, 0); + Date end = new Date(); + System.out.println(" " + N + " ʺ⣬ʱ" + + String.valueOf(end.getTime() - begin.getTime()) + "룬" + + count); + } + + private static void putQueenAtRow(short[][] chess, int row) { + // ݹֹжϣrow==N˵Ѿɹڷ8ʺ ֹݹ + if (row == N) { + count++; + System.out.println(" " + count + " ֽ⣺"); + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + System.out.print(chess[i][j] + " "); + } + System.out.println(); + } + return; + } + + short[][] chessTemp = chess.clone(); + + /** + * һеÿһλóŷŻʺ Ȼ״̬ȫִеݹ麯ڷһлʺ + */ + for (int i = 0; i < N; i++) { + // ڷһеĻʺ֮ǰҪһаڷŵļ¼ֹȾ + for (int j = 0; j < N; j++) + chessTemp[row][j] = 0; + + chessTemp[row][i] = 1; + + if (isSafety(chessTemp, row, i)) { + putQueenAtRow(chessTemp, row + 1); +// System.out.println("-----------"); +// for (int k = 0; k < N; k++) { +// for (int j = 0; j < N; j++) { +// System.out.print(chess[k][j] + " "); +// } +// System.out.println(); +// } + int []nums ={2,7,11,3,1,8}; + + } + } + } + + private static boolean isSafety(short[][] chess, int row, int col) { + // жϡϡǷȫ + int step = 1; + while (row - step >= 0) { + if (chess[row - step][col] == 1) // + return false; + if (col - step >= 0 && chess[row - step][col - step] == 1) // + return false; + if (col + step < N && chess[row - step][col + step] == 1) // + return false; + + step++; + } + return true; + } +} \ No newline at end of file diff --git a/src/datastructure/graph/Graph.java b/src/datastructure/graph/Graph.java new file mode 100644 index 0000000..67897bd --- /dev/null +++ b/src/datastructure/graph/Graph.java @@ -0,0 +1,134 @@ +package datastructure.graph; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + + +public class Graph { + + // We use Hashmap to store the edges in the graph + private Map> map = new HashMap<>(); + + // This function adds a new vertex to the graph + public void addVertex(T s) { + map.put(s, new LinkedList()); + } + + // This function adds the edge + // between source to destination + public void addEdge(T source, + T destination, + boolean bidirectional) { + + if (!map.containsKey(source)) + addVertex(source); + + if (!map.containsKey(destination)) + addVertex(destination); + + map.get(source).add(destination); + if (bidirectional == true) { + map.get(destination).add(source); + } + } + + // This function gives the count of vertices + public void getVertexCount() { + System.out.println("The graph has " + + map.keySet().size() + + " vertex"); + } + + // This function gives the count of edges + public void getEdgesCount(boolean bidirection) { + int count = 0; + for (T v : map.keySet()) { + count += map.get(v).size(); + } + if (bidirection == true) { + count = count / 2; + } + System.out.println("The graph has " + + count + + " edges."); + } + + // This function gives whether + // a vertex is present or not. + public void hasVertex(T s) { + if (map.containsKey(s)) { + System.out.println("The graph contains " + + s + " as a vertex."); + } else { + System.out.println("The graph does not contain " + + s + " as a vertex."); + } + } + + // This function gives whether an edge is present or not. + public void hasEdge(T s, T d) { + if (map.get(s).contains(d)) { + System.out.println("The graph has an edge between " + + s + " and " + d + "."); + } else { + System.out.println("The graph has no edge between " + + s + " and " + d + "."); + } + } + + // Prints the adjancency list of each vertex. + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + for (T v : map.keySet()) { + builder.append(v.toString() + ": "); + for (T w : map.get(v)) { + builder.append(w.toString() + " "); + } + builder.append("\n"); + } + + return (builder.toString()); + } + + + public static void main(String args[]) { + + // Object of graph is created. + Graph g = new Graph(); + + // edges are added. + // Since the graph is bidirectional, + // so boolean bidirectional is passed as true. + g.addEdge(0, 1, true); + g.addEdge(0, 4, true); + g.addEdge(1, 2, true); + g.addEdge(1, 3, true); + g.addEdge(1, 4, true); + g.addEdge(2, 3, true); + g.addEdge(3, 4, true); + + // print the graph. + System.out.println("Graph:\n" + + g.toString()); + + // gives the no of vertices in the graph. + g.getVertexCount(); + + // gives the no of edges in the graph. + g.getEdgesCount(true); + + // tells whether the edge is present or not. + g.hasEdge(3, 4); + + // tells whether vertex is present or not + g.hasVertex(5); + } + + +} + + diff --git a/src/datastructure/heap/MinHeap.java b/src/datastructure/heap/MinHeap.java new file mode 100644 index 0000000..2465677 --- /dev/null +++ b/src/datastructure/heap/MinHeap.java @@ -0,0 +1,164 @@ +package datastructure.heap; + +/** + * Title: С ȫܷشȡС/Ԫ + * Description: + * ѵĹ + * ѵĴӡ(ǰӦ) + * ѵIJ(뵽βϵΪС) + * ѵɾ(ɾѶԪزöβԪµΪС) + * (ʱ临ӶȣO(nlgn),ռ临ӶO(1),ȶ)һ + * @author rico + * @created 2017524 9:23:22 + */ +public class MinHeap { + + private int[] heap; // Ԫȫʽ + private int size; // Ԫصĸ + + /** + * 캯 + * + * @description һСΪsizeС + * @author rico + * @created 2017524 8:19:46 + * @param size + */ + public MinHeap(int maxSize) { + heap = new int[maxSize]; + } + + /** + * 캯 + * + * @description 鹹С + * @author rico + * @created 2017524 8:18:56 + * @param arr + */ + public MinHeap(int[] arr, int maxSize) { + heap = new int[maxSize > arr.length ? maxSize : arr.length]; + System.arraycopy(arr, 0, heap, 0, arr.length); + size = arr.length; + + int pos = (size - 2) / 2; // λãķ֧ڵ(Ҷڵĸ) + while (pos >= 0) { //εÿ֧ڵ + shiftDown(pos, size - 1); + pos--; + } + } + + /** + * @description µΪС(ӲСѵΪС)ǰΪС + * @author rico + * @created 2017524 7:52:39 + * @param start + * @param end + */ + private void shiftDown(int start, int end) { + int i = start; // ʼλã֧ڵ + int j = 2 * start + 1; // ÷֧ڵӽڵ + int temp = heap[i]; + while (j <= end) { // ӽڵ㲻ܳend(Χ) + if (j < end) { + j = heap[j] > heap[j + 1] ? j + 1 : j; // ѡнСǸ + } + if (temp < heap[j]) { // СĺӴڸףκδ + break; + } else { // 滻ڵֵ + heap[i] = heap[j]; + i = j; + j = 2 * j + 1; + } + } + heap[i] = temp; // һλ + } + + /** + * @description ϵΪС(ԭСѣԪغȷ仹С) + * @author rico + * @created 2017524 9:09:37 + * @param start + */ + private void shiftUp(int start) { + int j = start; + int i = (j - 1) / 2; // ʼλã֧ڵ + int temp = heap[j]; + while (j > 0) { // ӽڵ벻Ϊ + if (temp >= heap[i]) { //ԭСѣֻȽŮ븸׵Ĺϵ + break; + } else { + heap[j] = heap[i]; + j = i; + i = (j - 1) / 2; + } + } + heap[j] = temp; // һλ + } + + /** + * @description СѲԪ(Dz뵽Сѵ) + * @author rico + * @created 2017524 8:22:58 + * @param data + */ + public void insert(int data){ + if (size < heap.length) { + heap[size++] = data; // β + shiftUp(size-1); // ¶ϵ + } + } + + + /** + * @description ɾѶԪأԶѵһԪ + * @author rico + * @created 2017524 9:11:46 + */ + public void remove() { + if (size > 0) { + heap[0] = heap[size-1]; // ɾѶԪأβԪػѶ + size --; // ѴСһ + shiftDown(0, size-1); // µΪС + } + } + + + /** + * @description :ÿνСԪؽ + * @author rico + * @created 2017524 9:42:31 + */ + public void sort(){ + for (int i = size - 1; i >= 0; i--) { + int temp = heap[0]; + heap[0] = heap[i]; + heap[i] = temp; + + shiftDown(0, i-1); + } + + for (int i = size-1; i >= 0; i--) { + System.out.print(heap[i] + " "); + } + } + + /** + * @description ӡΪ i С + * @author rico + * @created 2017524 8:17:16 + * @param i + */ + public void printMinHeap(int i) { + if (size > i) { + System.out.print(heap[i]); + if (2 * i + 1 < size || 2 * i + 2 < size) { + System.out.print("("); + printMinHeap(2 * i + 1); + System.out.print(","); + printMinHeap(2 * i + 2); + System.out.print(")"); + } + } + } +} diff --git a/src/datastructure/list/LinkedList.java b/src/datastructure/list/LinkedList.java new file mode 100644 index 0000000..ee5da49 --- /dev/null +++ b/src/datastructure/list/LinkedList.java @@ -0,0 +1,351 @@ +package datastructure.list; + +/** + * Title: Javaʵ + * Description: ṹҪأ ͷhead + Сsize + * ɾ + * ǷΪ + * ĴС + * Ĵӡ + * ɾظڵ + * KԪ + * ķת + * ĵ + * мڵ + * Ƿл + * ڵɾ(֪ͷ) + * Ƿཻ + * Ľ + * + * @author rico + */ +public class LinkedList { + + private Node head; // ͷ + private int size; // С + + public LinkedList() { + head = new Node(null); + } + + public Node getHead() { + return head; + } + + /** + * @description ָλõԪ(0 - size),½ڵ + * @author rico + * @param data + * @param index + * @throws Exception + */ + public Node add(E data, int index) throws Exception { + if (index > size) { + throw new Exception("Χ..."); + } + + Node cur = head; + for (int i = 0; i < index; i++) { + cur = cur.next; + } + Node node = new Node(data); // Ԫ + cur.next = node; + size++; + return node; + } + + /** + * @description ĩβԪ,½ڵ + * @author rico + * @param data + * @throws Exception + */ + public Node add(E data) throws Exception { + return add(data, size); + } + + /** + * @description β½ڵ + * @author rico + * @param node + */ + public void add(Node node){ + Node cur = head; + while(cur.next != null){ + cur = cur.next; + } + cur.next = node; + + while(node != null){ + size ++; + node = node.next; + } + } + + /** + * @description ɾָλõԪ(0 ~ size-1) + * @author rico + * @param index + * @return + * @throws Exception + */ + public E remove(int index) throws Exception { + if (index > size - 1 || index < 0) { + throw new Exception("Χ..."); + } + + Node cur = head; + for (int i = 0; i < index; i++) { + cur = cur.next; + } + + Node temp = cur.next; + cur.next = temp.next; + temp.next = null; + + size--; + return temp.data; + } + + /** + * @description ĩβɾԪ + * @author rico + * @return + * @throws Exception + */ + public E remove() throws Exception { + return remove(size - 1); + } + + /** + * @description ɾеظԪ(ѭ + ѭ) + * @author rico ʱ临ӶȣO(n^2) + */ + public void removeDuplicateNodes() { + Node cur = head.next; + while (cur != null) { // ѭ + Node temp = cur; + while (temp != null && temp.next != null) { // ѭ + if (cur.data.equals(temp.next.data)) { + Node duplicateNode = temp.next; + temp.next = duplicateNode.next; + duplicateNode.next = null; + size --; + } + temp = temp.next; + } + cur = cur.next; + } + } + + /** + * @description ҳеKԪ(˫ָ뷨,K-1) + * @author rico + * @param k + * @return ʱ临ӶȣO(n) + */ + public Node getEndK(int k) { + Node pre = head.next; + Node post = head.next; + for (int i = 1; i < k; i++) { // prek-1 + if (pre != null) { + pre = pre.next; + } + } + if (pre != null) { + // preߵĩʱpostָKڵ + while (pre != null && pre.next != null) { + pre = pre.next; + post = post.next; + } + return post; + } + return null; + } + + /** + * @description ת + * @author rico + */ + public void reverseLinkedList() { + Node cur = head.next; // ԭ + Node pre = null; // ת + + while (cur != null) { // ԭеÿڵзת + Node next = cur.next; // ¼ǰڵһڵ + cur.next = pre; // ǰڵָת + pre = cur; // ·ת + cur = next; // µǰڵ + } + head.next = pre; // ԭͷָת + } + + /** + * @description жϵǷΪ + * @author rico + * @return + */ + public boolean isEmpty() { + return size == 0; + } + + /** + * @description ӡ + * @author rico + */ + public void print() { + Node cur = head.next; + while (cur != null) { + System.out.print(cur.data + " "); + cur = cur.next; + } + System.out.println(); + } + + /** + * @description βͷ(ݹ鷨) + * @author rico + * @param head + */ + public void reversePrint(Node head) { + if (head.next != null) { + reversePrint(head.next); // "ȥ" + System.out.print(head.next.data + " "); // ""ʼӡ + } + } + + /** + * @description Ѱҵемڵ(˫ָ뷨) + * @author rico + */ + public void printMiddleNodes() { + Node index1 = head.next; // ָ + Node index2 = head.next; // ָ + if (head.next == null) { + System.out.println(index1.data); + } + while (index2 != null && index2.next != null + && index2.next.next != null) { + index1 = index1.next; + index2 = index2.next.next; + } + System.out.print(index1.data); // һмڵ + if (index2.next != null) { // Ϊżʱӡڶмڵ + System.out.println(index1.next.data); + } + } + + /** + * @description жϵǷл(˫ָ뷨) + * @author rico + * @return + */ + public boolean hasLoop() { + Node index1 = head.next; // ָ + Node index2 = head.next; // ָ + while (index2 != null && index2.next != null + && index2.next.next != null) { + index1 = index1.next; + index2 = index2.next.next; + if (index1 == index2) { + return true; + } + } + return false; + } + + /** + * @description ڲ֪ͷǰ£ɾָڵ + * @author rico + * @param node + * @return + */ + public boolean deleteNodeWithoutHead(Node node) { + if (node == null || node.next == null) { // ָڵΪջΪβڵʱ޷ɾ + return false; + } + + Node next = node.next; + + // ̽ڵݸƵǰڵ + node.data = next.data; + node.next = next.next; + + // ̽ڵ + next.next = null; + next.data = null; + return true; + } + + /** + * @description жϵǰĿǷཻ(ཻȡβڵǷͬ) + * @author rico + * @param head + * @return + */ + public boolean isIntersect(LinkedList list2) { + Node cur1 = head.next; // ǰ + Node cur2 = list2.getHead().next; // Ŀ + + // һΪգ򷵻 false + if(cur1 == null || cur2 == null){ + return false; + } + + // һβڵ + while(cur1.next != null){ + cur1 = cur1.next; + } + + // ڶβڵ + while(cur2.next != null){ + cur2 = cur2.next; + } + + return cur1 == cur2; // ཻȡβڵǷͬ + } + + /** + * @description Ľ(ཻnull) + * @author rico + * @param head + * @return + */ + public Node getIntersectionPoint(LinkedList list2) { + Node cur1 = head.next; // ǰ + Node cur2 = list2.getHead().next; // Ŀ + + if(this.isIntersect(list2)){ // жǷཻ + // óȽϳƶstep + int step = Math.abs(list2.size - this.size); + if(list2.size > this.size){ + while(step > 0){ + cur2 = cur2.next; + step --; + } + }else if(list2.size < this.size){ + while(step > 0){ + cur1 = cur1.next; + step --; + } + } + + //ָͬʱƶһָͬһڵ㣬Ϊ + while(cur1 != cur2){ + cur1 = cur1.next; + cur2 = cur2.next; + } + return cur1; + } + return null; + } + + /** + * @description ij + * @author rico + * @return + */ + public int size(){ + return size; + } +} diff --git a/src/datastructure/list/Node.java b/src/datastructure/list/Node.java new file mode 100644 index 0000000..66e2a35 --- /dev/null +++ b/src/datastructure/list/Node.java @@ -0,0 +1,34 @@ +package datastructure.list; + +/** + * Title: + * Description: ĻԪ + * + * @author rico + * @created 201746 9:55:58 + */ + public class Node { + //ɼ + Node next; + T data; + + /** + * 캯 + * + * @description һ½ڵ + * @author rico + * @created 201746 9:56:56 + * @param data + * Ԫ + * @param next + * ԪϽڵ + */ + public Node(T data) { + this.data = data; + } + + @Override + public String toString() { + return data.toString(); + } +} diff --git a/src/datastructure/queue/LinkedQueue.java b/src/datastructure/queue/LinkedQueue.java new file mode 100644 index 0000000..dbc0d84 --- /dev/null +++ b/src/datastructure/queue/LinkedQueue.java @@ -0,0 +1,101 @@ +package datastructure.queue; + + +/** + * Title: Ķʵ + * Description: ͷ(ͷ㲻洢ֵ,ӲO(1))βָ(ɾO(1)) + * @author rico + * @created 2017519 8:49:34 + */ + +public class LinkedQueue { + + private Node head; // ͷ + private Node rear; // βָ + private int size; // дС + + public LinkedQueue(){ + head = rear = new Node(null); + } + + /** + * @description Ԫصβ + * @author rico + * @created 2017519 8:52:20 + * @param data + */ + public void put(E data){ + Node node = new Node(data); + rear.next = node; + rear = node; + size ++; + } + + /** + * @description ɾͷضͷԪصֵ + * @author rico + * @created 2017519 8:52:24 + * @return + */ + public E pop(){ + if(!isEmpty()){ + E e = null; + Node temp = head.next; + head.next = temp.next; + e = temp.data; + + temp.data = null; + temp.next = null; + size--; + return e; + } + return null; + } + + /** + * @description ضͷԪصֵ + * @author rico + * @created 2017519 8:52:28 + * @return + */ + public E peek() { + if (!isEmpty()) { + return head.next.data; + } + return null; + } + + + /** + * @description ǷΪ + * @author rico + * @created 2017519 8:52:33 + * @return + */ + public boolean isEmpty(){ + return size == 0; + } + + + /** + * @description дС + * @author rico + * @created 2017519 8:52:35 + * @return + */ + public int size() { + return size; + } + + @Override + public String toString() { + // TODO Auto-generated method stub + Node cur = head.next; + StringBuilder sb = new StringBuilder(); + while(cur != null){ + sb.append(cur.data).append(" "); + cur = cur.next; + } + return sb.toString(); + } +} diff --git a/src/datastructure/queue/Node.java b/src/datastructure/queue/Node.java new file mode 100644 index 0000000..011a3ba --- /dev/null +++ b/src/datastructure/queue/Node.java @@ -0,0 +1,34 @@ +package datastructure.queue; + +/** + * Title: + * Description: ԱĻԪ + * + * @author rico + * @created 201746 9:55:58 + */ + public class Node { + //ɼ + Node next; + T data; + + /** + * 캯 + * + * @description һ½ڵ + * @author rico + * @created 201746 9:56:56 + * @param data + * Ԫ + * @param next + * ԪϽڵ + */ + public Node(T data) { + this.data = data; + } + + @Override + public String toString() { + return data.toString(); + } +} diff --git a/src/datastructure/queue/OptimizationStackQueue.java b/src/datastructure/queue/OptimizationStackQueue.java new file mode 100644 index 0000000..0bddae5 --- /dev/null +++ b/src/datastructure/queue/OptimizationStackQueue.java @@ -0,0 +1,61 @@ +package datastructure.queue; + + +import datastructure.stack.LinkedStack; + +/** + * Title: ʹջģһ Description: һջר(ʼղִгӲ)һջר(ʼղִӲ) + * ֽⷨStackQueueҪ߲٣˷ջҪʱšһΣ + * + * @author rico + * @created 2017519 10:45:11 + */ +public class OptimizationStackQueue { + + private LinkedStack stack1; // ջ + private LinkedStack stack2; // ջ + + public OptimizationStackQueue() { + + stack1 = new LinkedStack(); + stack2 = new LinkedStack(); + } + + /** + * @description Ԫصβ,ֱӶstack1ִѹջ + * @author rico + * @created 2017519 10:47:59 + * @param e + */ + public void put(E e) { + stack1.push(e); + } + + /** + * @description ɾͷضͷԪصֵȼstack2ǷΪգ + * ΪգȽstack1еԪȫstack2ٶstack2ִеջ + * ֱӶstack2ִеջ + * @author rico + * @created 2017519 10:48:32 + * @return + */ + public E pop() { + if (stack2.isEmpty()) { + while (!stack1.isEmpty()) { + stack2.push(stack1.pop().getData()); + } + } + return stack2.pop().getData(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (!stack2.isEmpty()) { + sb.append(stack2.toString()); + } + return sb.append( + new StringBuilder(stack1.toString()).reverse().toString()) + .toString(); + } +} diff --git a/src/datastructure/queue/SeqQueue.java b/src/datastructure/queue/SeqQueue.java new file mode 100644 index 0000000..ad9f7d2 --- /dev/null +++ b/src/datastructure/queue/SeqQueue.java @@ -0,0 +1,111 @@ +package datastructure.queue; + +import java.util.Arrays; + + +/** + * Title: Ķʵ + * Description: + * @author rico + * @created 2017519 8:23:55 + */ +public class SeqQueue { + + + /** еĴ洢ṹ (@author: rico) */ + private Object[] queue; + private int size; + private int maxSize; // + + public SeqQueue(int maxSize){ + this.maxSize = maxSize; + queue = new Object[maxSize]; + } + + + /** + * @description Ԫصβ + * @author rico + * @created 2017519 8:25:32 + * @param data + */ + public void put(E data){ + if(!isFull()){ + queue[size] = data; + size ++; + } + } + + + /** + * @description ɾͷضͷԪصֵ + * @author rico + * @created 2017519 8:25:47 + * @return + */ + public E pop(){ + if (!isEmpty()) { + E temp = (E) queue[0]; + for (int i = 0; i < size - 1; i++) { + queue[i] = queue[i+1]; + } + queue[size-1] = null; + size--; + return temp; + } + return null; + } + + + /** + * @description ضͷԪ + * @author rico + * @created 2017519 8:26:01 + * @return + */ + public E peek(){ + if (!isEmpty()) { + return (E) queue[0]; + } + return null; + } + + + /** + * @description Ƿ + * @author rico + * @created 2017519 8:26:14 + * @return + */ + public boolean isFull(){ + return size == maxSize; + } + + + /** + * @description ǷΪ + * @author rico + * @created 2017519 8:26:25 + * @return + */ + public boolean isEmpty(){ + return size == 0; + } + + + /** + * @description еĴС + * @author rico + * @created 2017519 8:26:34 + * @return + */ + public int size(){ + return size; + } + + @Override + public String toString() { + // TODO Auto-generated method stub + return Arrays.toString(queue); + } +} diff --git a/src/datastructure/queue/StackQueue.java b/src/datastructure/queue/StackQueue.java new file mode 100644 index 0000000..4fd33ab --- /dev/null +++ b/src/datastructure/queue/StackQueue.java @@ -0,0 +1,70 @@ +package datastructure.queue; + +import datastructure.stack.LinkedStack; + + +/** + * Title: ʹջģһ + * Description: һջ洢ռ䣬һջʱ + * @author rico + * @created 2017519 10:45:11 + */ +public class StackQueue { + + private LinkedStack stack1; // 洢ռ + private LinkedStack stack2; //ʱ + + public StackQueue() { + stack1 = new LinkedStack(); + stack2 = new LinkedStack(); + } + + + /** + * @description Ԫصβȼstack2ǷΪգ + * ΪգֱӶstack1ִѹջ + * Ƚstack2еԪصstack1ٶstack1ִѹջ + * @author rico + * @created 2017519 10:47:59 + * @param e + */ + public void put(E e) { + if (!stack2.isEmpty()) { + while (stack2.size() > 0) { + stack1.push(stack2.pop().getData()); + } + } + stack1.push(e); + } + + + /** + * @description ɾͷضͷԪصֵȼstack2ǷΪգ + * ΪգȽstack1еsize-1Ԫصstack2ٶstack1ջԪִеջ + * ֱӶstack2ִеջ + * @author rico + * @created 2017519 10:48:32 + * @return + */ + public E pop() { + if (stack2.isEmpty()) { + if (!stack1.isEmpty()) { + while (stack1.size() > 1) { + stack2.push(stack1.pop().getData()); + } + return stack1.pop().getData(); + } + return null; + } else { + return stack2.pop().getData(); + } + } + + @Override + public String toString() { + if (!stack1.isEmpty()) { + return new StringBuilder(stack1.toString()).reverse().toString(); + } + return stack2.toString(); + } +} diff --git a/src/datastructure/sort/BinaryInsertSort.java b/src/datastructure/sort/BinaryInsertSort.java new file mode 100644 index 0000000..11438dc --- /dev/null +++ b/src/datastructure/sort/BinaryInsertSort.java @@ -0,0 +1,45 @@ +package datastructure.sort; + + +/** + * Title: е۰ڳʼ + * Description: ۰λãֱӲ;ֱӲǣߵҪ˳ + * ʱ临Ӷȣ۰ֱӲԼ˹ؼ֮ıȽϴƶûиı䡣ԣ + * ۰Ͳʱ临ӶͬON^2ڼ˱Ƚϴȷʵ൱㣬Ը㷨ȻֱӲá + * ռ临ӶȣO(1) + * ԣȶ + * ڲ(Ԫȫڴ) + * @author rico + * @created 2017525 12:03:23 + */ +public class BinaryInsertSort { + public static int[] binaryInsertSort(int[] target) { + if (target != null && target.length > 1) { + for (int i = 1; i < target.length; i++) { + int left = 0; + int right = i - 1; + int mid; + int temp = target[i]; + if(temp < target[right]){ // ǰֵСеֵʱʼҲλ + while(left <= right){ + mid = (left + right)/2; + if(target[mid] < temp){ + left = mid + 1; // С + }else if(target[mid] > temp){ + right = mid - 1; // С + }else{ // ֵеtarget[mid]ȣ֤ȶԵĴ + left = left + 1; + } + } + + // left˳ƶleftλò + for (int j = i; j > left; j--) { + target[j] = target[j-1]; + } + target[left] = temp; + } + } + } + return target; + } +} diff --git a/src/datastructure/sort/BubbleSort.java b/src/datastructure/sort/BubbleSort.java new file mode 100644 index 0000000..fe477a0 --- /dev/null +++ b/src/datastructure/sort/BubbleSort.java @@ -0,0 +1,67 @@ +package datastructure.sort; + +import java.util.Arrays; + +/** + * Title: еð һָŻðn-1αȽ + * Description:ΪԽԪػᾭɽ""еĶ(λ)ȷԳΪð + * ʱ临ӶȣO(n)ƽO(n^2)O(n^2) + * ռ临ӶȣO(1) + * ԣȶ + * ڲ(Ԫȫڴ) + * + * @author rico + * @created 2017520 10:40:00 + */ +public class BubbleSort { + + + /** + * @description ð(n-1αȽ) + * @author rico + */ + public static int[] bubbleSort(int[] target) { + int n = target.length; + if (target != null && n != 1) { + // Ҫn-1ɣÿһ˽ȽСԪƵǰ棬ȽϴԪȻ𽥳ˣð + for (int i = 0; i < n-1; i++) { + for (int j = n-1; j > i; j--) { + if(target[j] i; j--) { + if(target[j] < target[j-1]){ + int temp = target[j]; + target[j] = target[j-1]; + target[j-1] = temp; + exchange = true; + } + } + System.out.println(Arrays.toString(target)); + if (!exchange){ + return target; + } + } + } + return target; + } +} diff --git a/src/datastructure/sort/HeapSort.java b/src/datastructure/sort/HeapSort.java new file mode 100644 index 0000000..d6244bc --- /dev/null +++ b/src/datastructure/sort/HeapSort.java @@ -0,0 +1,64 @@ +package datastructure.sort; + +/** + * Title: (ѡ)() + * Description: ֽеΪѣȻÿνѶԪβԪؽСѵķΧֱС1 + * ʱ临ӶȣO(nlgn) + * ռ临ӶȣO(1) + * ԣȶ + * ڲ(Ԫȫڴ) + * @author rico + * @created 2017525 9:48:06 + */ +public class HeapSort { + + public static int[] heapSort(int[] target) { + if (target != null && target.length > 1) { + + // Ϊ + int pos = (target.length - 2) / 2; + while (pos >= 0) { + shiftDown(target, pos, target.length - 1); + pos--; + } + + // + for (int i = target.length-1; i > 0; i--) { + int temp = target[i]; + target[i] = target[0]; + target[0] = temp; + shiftDown(target, 0, i-1); + } + return target; + } + return target; + } + + + /** + * @description ϶µΪ + * @author rico + * @created 2017525 9:45:40 + * @param target + * @param start + * @param end + */ + private static void shiftDown(int[] target, int start, int end) { + int i = start; + int j = 2 * start + 1; + int temp = target[i]; + while (j <= end) { // + if (j < end && target[j + 1] > target[j]) { //ҳϴŮ + j = j + 1; + } + if (target[j] <= temp) { // ״Ů + break; + } else { + target[i] = target[j]; + i = j; + j = 2 * j + 1; + } + } + target[i] = temp; + } +} diff --git a/src/datastructure/sort/MergeSort.java b/src/datastructure/sort/MergeSort.java new file mode 100644 index 0000000..6ae0dea --- /dev/null +++ b/src/datastructure/sort/MergeSort.java @@ -0,0 +1,85 @@ +package datastructure.sort; + +import java.util.Arrays; + +/** + * Title: 鲢 Ϊ򵥵㷨һݹ㷨 + * Description:鲢̣ + * ""ָԭзֳɰУֱнеݹ + * ""ָźĸкϲԭ + * + * 鲢ҪǣҪһԭһĸռ + * + * 鲢ԭʼУΡƽκʱ临Ӷȶһ + * ʱ临ӶȣO(n)ƽO(n^2)O(n^2) + * ռ临ӶȣO(n) + * ԣȶ + * ڲ(Ԫȫڴ) + * + * @author rico + * @created 2017520 10:40:00 + */ +public class MergeSort { + + /** + * @description 鲢㷨(ݹ㷨)ȥֽ⣬ϲ + * @author rico + * @created 2017520 4:04:52 + * @param target + * @param left ʼλ + * @param right ֹλ + * @return + */ + public static int[] mergeSort(int[] target, int left, int right) { + + if(right > left){ // ݹֹ + int mid = (left + right)/2; + mergeSort(target, left, mid); // 鲢һ + mergeSort(target, mid+1, right); // 鲢ڶ + return merge(target,left,mid,right); // ϲгԭ + } + return target; + } + + + /** + * @description ·鲢㷨 + * @author rico + * @created 2017520 3:59:16 + * @param target ڴ洢鲢 + * @param left һĵһԪλ + * @param mid һһԪλ + * @param right ڶһԪλ + * @return + */ + public static int[] merge(int[] target, int left, int mid, int right){ + + // Ҫһԭһĸռ + int[] temp = Arrays.copyOf(target, target.length); + + // s1,s2Ǽָ룬index Ǵָ + int s1 = left; + int s2 = mid + 1; + int index = left; + + // δ꣬Ƚ + while(s1 <= mid && s2 <= right){ + if(temp[s1] <= temp[s2]){ // ȶ + target[index++] = temp[s1++]; + }else{ + target[index++] = temp[s2++]; + } + } + + //һδ꣬ + while(s1 <= mid){ + target[index++] = temp[s1++]; + } + + //ڶδ꣬ + while(s2 <= right){ + target[index++] = temp[s2++]; + } + return target; + } +} diff --git a/src/datastructure/sort/QuickSort.java b/src/datastructure/sort/QuickSort.java new file mode 100644 index 0000000..052b734 --- /dev/null +++ b/src/datastructure/sort/QuickSort.java @@ -0,0 +1,84 @@ +package datastructure.sort; + +import java.util.Arrays; + +/** + * Title: 交换排序中的快速排序,目前应用最为广泛的排序算法,是一个递归算法 + * Description:快速排序包括两个过程:划分 和 快排 + * "划分"是指将原序列按基准元素划分两个子序列 + * "快排"是指分别对子序列进行快排 + * + * 就平均计算时间而言,快速排序是所有内部排序方法中最好的一个 + * + * 对大规模数据排序时,快排是快的;对小规模数据排序时,快排是慢的,甚至慢于简单选择排序等简单排序方法 + * + * 快速排序依赖于原始序列,因此其时间复杂度从O(nlgn)到O(n^2)不等 + * 时间复杂度:最好情形O(nlgn),平均情形O(nlgn),最差情形O(n^2) + * + * 递归所消耗的栈空间 + * 空间复杂度:O(lgn) + * + * 可选任一元素作为基准元素 + * 稳 定 性:不稳定 + * + * + * 内部排序(在排序过程中数据元素完全在内存) + * + * @author rico + * @created 2017年5月20日 上午10:40:00 + */ +public class QuickSort { + + /** + * @description 快排算法(递归算法):在递去过程中就把问题解决了 + * @author rico + * @created 2017年5月20日 下午5:12:06 + * @param target + * @param left + * @param right + * @return + */ + public static int[] quickSort(int[] target, int left, int right) { + if(right > left){ // 递归终止条件 + int base_index = partition(target,left, right); // 原序列划分后基准元素的位置 + quickSort(target, left, base_index-1); // 对第一个子序列快速排序,不包含基准元素! + quickSort(target, base_index+1, right); // 对第二个子序列快速排序,不包含基准元素! + } + return target; + } + + + /** + * @description 序列划分,以第一个元素为基准元素 + * @author rico + * @created 2017年5月20日 下午5:10:54 + * @param target 序列 + * @param left 序列左端 + * @param right 序列右端 + * @return + */ + public static int partition(int[] target, int left, int right){ + + int base = target[left]; // 基准元素的值 + int base_index = left; // 基准元素最终应该在的位置 + + for (int i = left+1; i <= right; i++) { // 从基准元素的下一个元素开始 + if(target[i] < base){ // 若其小于基准元素 + base_index++; // 若其小于基准元素,则基准元素最终位置后移;否则不用移动 + if(base_index != i){ // 相等情况意味着i之前的元素都小于base,只需要换一次即可,不需要次次都换 + int temp = target[base_index]; + target[base_index] = target[i]; + target[i] = temp; + } + } + } + + // 将基准元素就位 + target[left] = target[base_index]; + target[base_index] = base; + + System.out.println(Arrays.toString(target)); + + return base_index; //返回划分后基准元素的位置 + } +} diff --git a/src/datastructure/sort/QuickSort_PartitionOnly.java b/src/datastructure/sort/QuickSort_PartitionOnly.java new file mode 100644 index 0000000..d654263 --- /dev/null +++ b/src/datastructure/sort/QuickSort_PartitionOnly.java @@ -0,0 +1,49 @@ +package datastructure.sort; + +import java.util.Arrays; + + +/** + * Title:ı + * Description: ϲϵþŵĻ㷨 + * ʱ临ӶȣO(n^2) + * @author rico + * @created 201762 9:10:43 + */ +public class QuickSort_PartitionOnly { + /** + * @description еÿԪΪ׼л֣ + * ֱԪض + * @author rico + * @param array + */ + public void quicksort(int[] array) { + if (array != null && array.length != 0) { + for (int i = 0; i < array.length; i++) { + // ǿŵĻ㷨 + int base_index = 0; + int base = array[i]; + base_index = i; + for (int j = i+1; j < array.length; j++) { + if (array[j] <= base ) { + base_index ++; + if (base_index != j) { + int temp = array[base_index]; + array[base_index] = array[j]; + array[j] = temp; + } + } + } + array[i] = array[base_index]; + array[base_index] = base; + System.out.println(Arrays.toString(array)); + } + } + } + + public static void main(String[] args) { +// int[] array = { 1, 2, 3, 2, 2, 2, 5, 4, 2 }; + int[] array = { 1, 2, 3, 5, 0, 4, 9, 2, 6 }; + new QuickSort_PartitionOnly().quicksort(array); + } +} diff --git a/src/datastructure/sort/RadixSort.java b/src/datastructure/sort/RadixSort.java new file mode 100644 index 0000000..6ef144d --- /dev/null +++ b/src/datastructure/sort/RadixSort.java @@ -0,0 +1,60 @@ +package datastructure.sort; + +/** + * Title: еĻ + * Description: ڶԪؽбȽϵĻϽ򣬶Dz " + ռ" İ취 + * + * ȣĿиԪط䵽ͰУ + * ΣͰеԪذȽȳ˳ٷŻȥ + * ... + * + * ʱ临ӶȣO(d*(r+n)) O(dn),d ĴСһܵ nӰ + * ռ临ӶȣO(rd + n) O(n) + * ԣȶ + * ڲ(Ԫȫڴ) + * @author rico + * @created 2017520 10:40:00 + */ +public class RadixSort { + + /** + * @description + ռ + * @author rico + * @created 2017521 9:25:52 + * @param target + * @param r + * @param d Ԫصλ + * @param n Ԫظ + * @return + */ + public static int[] radixSort(int[] target, int r, int d, int n){ + if (target != null && target.length != 1 ) { + + int[][] bucket = new int[r][n]; // һлrͰÿͰnԪ + int digit; // ȡԪضӦλϵ֣װǸͰ + int divisor = 1; // ÿһֵij1, 10, 100, ... + int[] count = new int[r]; // ͳÿͰʵʴԪصĸ + + for (int i = 0; i < d; i++) { // d λԪأҪ䡢ռdμ + + // + for (int ele : target) { + digit = (ele/divisor) % 10; // ȡԪضӦλϵ() + bucket[digit][count[digit]++] = ele; // ԪطӦͰͰԪĿ1 + } + + // ռ + int index = 0; // Ŀ± + for (int j = 0; j < r; j++) { + int k = 0; // ڰȽȳ˳ȡͰԪ + while(k < count[j]){ + target[index++] = bucket[j][k++]; // ȽȳȡͰеԪ + } + count[j] = 0; // + } + divisor *= 10; //ڻȡԪضӦλ + } + } + return target; + } +} diff --git a/src/datastructure/sort/ShellSort.java b/src/datastructure/sort/ShellSort.java new file mode 100644 index 0000000..7b43654 --- /dev/null +++ b/src/datastructure/sort/ShellSort.java @@ -0,0 +1,47 @@ +package datastructure.sort; + +/** + * Title: еϣ + * Description: ֱԼΪgapgapнֱӲ򣬲Сgap,ֱΪ 1 + * + * տʼʱgapϴÿԪؽ٣ٶȽϿ죻 + * ڣgapСÿԪؽ϶࣬󲿷ԪػٶԽϿ졣 + * + * ʱ临ӶȣO(n) ~ O(n^2) + * ռ临ӶȣO(1) + * ԣȶ + * ڲ(Ԫȫڴ) + * @author rico + * @created 2017520 10:40:00 + */ +public class ShellSort { + + + /** + * @description + * @author rico + * @created 2017521 7:56:03 + * @param target + * @return + */ + public static int[] shellSort(int[] target){ + if(target != null && target.length != 1){ + int gap = target.length; // gapСΪgap + do{ + gap = gap/3 + 1; // СgapֱΪ1 + for (int i = 0 + gap; i < target.length; i++) { // ÿнֱӲ + if(target[i] < target[i-gap]){ + int j = i - gap; + int temp = target[i]; // ֵ + do{ + target[j + gap] = target[j]; // Ԫ + j = j - gap; // ٱȽǰһԪ + }while(j >= 0 && target[j] > temp); // ǰȽϵֹ + target[j + gap] = temp; // ֵʵλ + } + } + }while(gap > 1); + } + return target; + } +} diff --git a/src/datastructure/sort/StraightInsertionSort.java b/src/datastructure/sort/StraightInsertionSort.java new file mode 100644 index 0000000..e85762c --- /dev/null +++ b/src/datastructure/sort/StraightInsertionSort.java @@ -0,0 +1,30 @@ +package datastructure.sort; + +/** + * Title: еֱӲ ڳʼ + * Description: вϲµļ¼ԴﵽĿ + * ʱ临ӶȣO(n)ƽO(n^2)O(n^2) + * ռ临ӶȣO(1) + * ԣȶ + * ڲ(Ԫȫڴ) + * @author rico + * @created 2017520 10:40:00 + */ +public class StraightInsertionSort { + + public static int[] insertSort(int[] target){ + + if(target != null && target.length != 1){ // 鲻Ϊҳȴ1 + for (int i = 1; i < target.length; i++) { // Уֱչ + for (int j = i; j > 0; j--) { // вµԪ + if(target[j] < target[j-1]){ // + int temp = target[j]; + target[j] = target[j-1]; + target[j-1] = temp; + } + } + } + } + return target; + } +} \ No newline at end of file diff --git a/src/datastructure/sort/StraightSelectSort.java b/src/datastructure/sort/StraightSelectSort.java new file mode 100644 index 0000000..4ea8842 --- /dev/null +++ b/src/datastructure/sort/StraightSelectSort.java @@ -0,0 +1,34 @@ +package datastructure.sort; + +/** + * Title: ѡеֱѡ + * Description: ÿҵеǰΧеСֵ÷ΧеĵһֽͬʱСΧ + * ʱ临ӶȣO(n^2)ƽO(n^2)O(n^2) + * ռ临ӶȣO(1) + * ԣȶ + * ڲ(Ԫȫڴ) + * @author rico + * @created 2017520 10:40:00 + */ +public class StraightSelectSort { + + public static int[] selectSort(int[] target){ + + if(target != null && target.length != 1){ + for (int i = 0; i < target.length; i++) { + int min_index = i; + for (int j = i + 1; j < target.length; j++) { + if(target[min_index] > target[j]){ + min_index = j; + } + } + if(target[min_index] != target[i]){ // ²ȶأ + int min = target[min_index]; + target[min_index] = target[i]; + target[i] = min; + } + } + } + return target; + } +} diff --git a/src/datastructure/stack/LinkedListStack.java b/src/datastructure/stack/LinkedListStack.java new file mode 100644 index 0000000..2e726db --- /dev/null +++ b/src/datastructure/stack/LinkedListStack.java @@ -0,0 +1,58 @@ +package datastructure.stack; + +import java.util.LinkedList; + + +/** + * Title: LinkedListʵջ + * Description: + * @author rico + * @created 201746 8:25:21 + */ +public class LinkedListStack { + + // ֧List + private LinkedList stack; + + // 캯 + public LinkedListStack(){ + stack = new LinkedList(); + } + + // ǷΪ + public boolean isEmpty(){ + return stack.isEmpty(); + } + + // ѹջ + public void push(E data){ + stack.addFirst(data); + } + + //ջ + public E pop() throws Exception{ + if(stack.isEmpty()){ + throw new Exception("ջ..."); + } + + return stack.pop(); + } + + @Override + public String toString() { + return stack.toString(); + } + + public static void main(String[] args) throws Exception { + LinkedListStack stack = new LinkedListStack(); + stack.push("Rico"); + stack.push("TJU"); + stack.push("Livia"); + stack.push("NEU"); + + System.out.println(stack); + + stack.pop(); + System.out.println(stack); + } +} diff --git a/src/datastructure/stack/LinkedStack.java b/src/datastructure/stack/LinkedStack.java new file mode 100644 index 0000000..19e5ee9 --- /dev/null +++ b/src/datastructure/stack/LinkedStack.java @@ -0,0 +1,167 @@ +package datastructure.stack; + +import java.util.Comparator; + +/** + * Title: ǿԶʽջ [ͨάһջ֤O(1)ʱ临ӶջеСԪ (ռ任ȡʱ)] + * Description: ʹöջṹ洢ջеСԪ + * ǰջԪرԭջеСֵС䱣浽СֵջУ򣬲κβ + * ǰջԪǵǰջеСֵôСֵջеĸСֵҲһ򣬲κβ + * @author rico + * @created 201746 7:52:34 + */ +public class LinkedStack { + + private Node top; // ջԪ + private int size; // ʽջĴС + + /** Сֵջ (@author: rico) */ + private LinkedStack min; + + // 캯 + public LinkedStack(){ + } + + /** + * @description жջǷΪ + * @author rico + * @created 2017514 10:54:44 + * @return + */ + public boolean isEmpty() { + return size == 0; + } + + /** + * @description ѹջ + * @author rico + * @param data + */ + public void push(E data) { + Node node = new Node(data); + // ¼ԪָջԪ + node.next = top; + top = node; + size++; + } + + /** + * @description ѹջ,ʹСֵջ + * @author rico + * @param data + */ + public void push(E data, Comparator c) { + push(data); + if(min == null){ + min = new LinkedStack(); + } + if(min.peek() == null){ + min.push(data); + }else if(c.compare(this.peek().data, min.peek().data) < 0){ + min.push(data); + } + } + + /** + * @description ɾջԪ + * @author rico + * @return + * @throws Exception + */ + public Node pop(){ + if (isEmpty()) { + return null; + } + + Node node = top; + top = top.next; + node.next = null; + size--; + return node; + } + + /** + * @description ɾջԪ,ʹСֵջ + * @author rico + * @return + * @throws Exception + */ + public Node pop(Comparator c){ + Node temp = this.pop(); + if(temp != null && min.peek() != null){ + if(c.compare(temp.data, min.peek().data) == 0){ + min.pop(); + } + } + return temp; + } + + /** + * @description ջԪ(ִɾ) + * @author rico + * @return + */ + public Node peek(){ + if (isEmpty()) { + return null; + } + return top; + } + + /** + * @description ȡǰջеСֵ + * @author rico + * @return + */ + public Node min() { + if(min.peek() == null){ + return null; + }else{ + return min.peek(); + } + } + + /** + * @description ӡջ + * @author rico + */ + public void print() { + Node index = top; + while (index != null) { + System.out.print(index.data + " "); + index = index.next; + } + System.out.println(); + } + + + + /** + * @description ջĴС + * @author rico + * @return + */ + public int size(){ + return size; + } + + public LinkedStack getMin() { + return min; + } + + public void setMin(LinkedStack min) { + this.min = min; + } + + @Override + public String toString() { + // TODO Auto-generated method stub + Node index = top; + StringBuilder sb = new StringBuilder(); + while (index != null) { + sb.append(index.data).append(" "); + index = index.next; + } + return sb.toString(); + } +} diff --git a/src/datastructure/stack/Node.java b/src/datastructure/stack/Node.java new file mode 100644 index 0000000..5064600 --- /dev/null +++ b/src/datastructure/stack/Node.java @@ -0,0 +1,43 @@ +package datastructure.stack; + +/** + * Title: + * Description: ĻԪ + * + * @author rico + * @created 201746 9:55:58 + */ + public class Node { + //ɼ + Node next; + T data; + + /** + * 캯 + * + * @description һ½ڵ + * @author rico + * @created 201746 9:56:56 + * @param data + * Ԫ + * @param next + * ԪϽڵ + */ + public Node(T data) { + this.data = data; + } + + public T getData() { + return data; + } + + @Override + public String toString() { + return data.toString(); + } + + public Node getNext() { + return next; + } + +} diff --git a/src/datastructure/stack/SeqStack.java b/src/datastructure/stack/SeqStack.java new file mode 100644 index 0000000..c3e3c11 --- /dev/null +++ b/src/datastructure/stack/SeqStack.java @@ -0,0 +1,72 @@ +package datastructure.stack; + +import java.util.Arrays; + +/** + * Title: ˳ջ + * Description: Ϊײʵ + * @author rico + * @created 201746 5:27:13 + */ +public class SeqStack { + + private Object[] stack; // ֧ + private int top; // ջָ + private int maxSize; // ջ + + // ĬϹ캯 + public SeqStack(){ + this(10); + } + + // ָĹ캯 + public SeqStack(int maxSize){ + this.stack = new Object[maxSize]; + this.top = -1; + this.maxSize = maxSize; + } + + // ǷΪ + public boolean isEmpty(){ + return top == -1; + } + + // ɾջԪ + @SuppressWarnings("unchecked") + public E pop() throws Exception{ + if (top == -1) { + throw new Exception("ջΪ..."); + } + E element = (E)stack[top]; + stack[top --] = null; // ɾԪ + return element; + } + + // Ԫ + public void push(E e) throws Exception{ + if (top == maxSize -1) { + throw new Exception("ջ..."); + } + stack[++top] = e; + } + + // ӡջ + @Override + public String toString() { + return Arrays.toString(stack); + } + + public static void main(String[] args) throws Exception { + SeqStack stack = new SeqStack(); + stack.push("Rico"); + stack.push("TJU"); + stack.push("Livia"); + stack.push("NEU"); + + System.out.println(stack); + + stack.pop(); + System.out.println(stack); + + } +} diff --git a/src/datastructure/test/BinarySearchTreeTest.java b/src/datastructure/test/BinarySearchTreeTest.java new file mode 100644 index 0000000..98320b9 --- /dev/null +++ b/src/datastructure/test/BinarySearchTreeTest.java @@ -0,0 +1,36 @@ +package datastructure.test; + + +import datastructure.BinarySearchTree.BinarySearchTree; + +public class BinarySearchTreeTest { + public static void main(String[] args) { + int[] input = {53,78,65,17,87,9,81,45,23}; + BinarySearchTree tree = new BinarySearchTree(input); + + System.out.println(""); + tree.inOrder(tree.getRoot()); + System.out.println(); + System.out.println("\n------------------------\n"); + System.out.println("ӡ"); + tree.printTree(tree.getRoot()); + System.out.println(); + System.out.println("\n------------------------\n"); + + System.out.println("Ŀֵ"); + System.out.println(tree.search(23, tree.getRoot())); + System.out.println("\n------------------------\n"); + + System.out.println("Ŀֵ"); + tree.insert(10, tree.getRoot()); + tree.printTree(tree.getRoot()); + System.out.println(); + System.out.println("\n------------------------\n"); + + System.out.println("ɾĿֵ"); + tree.remove(78, tree.getRoot()); + tree.printTree(tree.getRoot()); + System.out.println(); + System.out.println("\n------------------------\n"); + } +} diff --git a/src/datastructure/test/BinaryTreeTest.java b/src/datastructure/test/BinaryTreeTest.java new file mode 100644 index 0000000..8a1c19a --- /dev/null +++ b/src/datastructure/test/BinaryTreeTest.java @@ -0,0 +1,85 @@ +package datastructure.test; + + +import datastructure.tree.BinaryTree; + +public class BinaryTreeTest { + public static void main(String[] args) { + BinaryTree tree = new BinaryTree(); + tree.createBinaryTree("A(B(D,E(G(I,J),M)),C(F(,H(,K)),))"); + + System.out.println(); + System.out.println("treeIJ() " + tree.levelOrder()); + System.out.println("\n----------------------------------------------\n"); + System.out.println("treeǰ (ݹ) " + tree.preOrder(tree.getRoot())); + System.out.println("treeǰ () " + tree.preOrder()); + System.out.println("\n----------------------------------------------\n"); + + System.out.println("tree (ݹ) " + tree.inOrder(tree.getRoot())); + System.out.println("tree () " + tree.inOrder()); + System.out.println("\n----------------------------------------------\n"); + + System.out.println("treeĺ (ݹ) " + tree.postOrder(tree.getRoot())); + System.out.println("treeĺ () " + tree.postOrder()); + System.out.println("\n----------------------------------------------\n"); + + System.out.println("treeĸ " + tree.getRoot()); + System.out.println("\n----------------------------------------------\n"); + + System.out.println("treeĸ߶ " + tree.height(tree.getRoot())); + System.out.println("\n----------------------------------------------\n"); + + System.out.println("treeĽ " + tree.size(tree.getRoot())); + System.out.println("\n----------------------------------------------\n"); + + // Ʋ + BinaryTree tree2 = new BinaryTree(tree.getRoot()); + System.out.println("treeΪ " + tree.printBinaryTree(tree.getRoot())); + System.out.println("treeΪtree2 " + tree2.printBinaryTree(tree2.getRoot())); + // ж tree2 tree Ƿ + System.out.println("tree2 tree Ƿ: " + tree.equals(tree2)); + System.out.println("\n----------------------------------------------\n"); + + // tree1 + BinaryTree tree1 = new BinaryTree(); + tree1.createBinaryTree("A(B(D,E(G(I,J),)),C(F(,H(,K)),))"); + System.out.println("treeΪ " + tree.printBinaryTree(tree.getRoot())); + System.out.println("tree1Ϊ " + tree1.printBinaryTree(tree1.getRoot())); + // ж tree1 tree Ƿ + System.out.println("tree1 tree Ƿ: " + tree.equals(tree1)); + System.out.println("\n----------------------------------------------\n"); + + // ǰ + BinaryTree tree3 = new BinaryTree( + "ABC##DE#G##F###".toCharArray()); + System.out.println("ǰ: " + tree3.printBinaryTree(tree3.getRoot())); + System.out.println("ǰtree3 " + tree.preOrder(tree3.getRoot())); + System.out.println("tree3 " + tree.inOrder(tree3.getRoot())); + System.out.println("tree3 " + tree.postOrder(tree3.getRoot())); + System.out.println("\n----------------------------------------------\n"); + + // Թʽӡ + System.out.println("ԹʽӡtreeΪ " + tree.printBinaryTree(tree.getRoot())); + System.out.println("Թʽӡtree1Ϊ " + tree1.printBinaryTree(tree1.getRoot())); + System.out.println("Թʽӡtree2Ϊ " + tree2.printBinaryTree(tree2.getRoot())); + System.out.println("Թʽӡtree3Ϊ " + tree3.printBinaryTree(tree3.getRoot())); + System.out.println("\n----------------------------------------------\n"); + + // tree + String pre = tree.preOrder().replace(" ", ""); + String in = tree.inOrder().replace(" ", ""); + BinaryTree tree4 = new BinaryTree(pre, in, true); + System.out.println("treetree4Ϊ " + + tree.printBinaryTree(tree4.getRoot())); + System.out.println("treetree4Ƿȣ " + tree.equals(tree4)); + System.out.println("\n----------------------------------------------\n"); + + // treeͺ + String post = tree.postOrder().replace(" ", ""); + BinaryTree tree5 = new BinaryTree(in, post, false); + System.out.println("treeͺtree5Ϊ " + + tree.printBinaryTree(tree5.getRoot())); + System.out.println("treetree5Ƿȣ " + tree.equals(tree5)); + + } +} diff --git a/src/datastructure/test/LinkedListTest.java b/src/datastructure/test/LinkedListTest.java new file mode 100644 index 0000000..86430b8 --- /dev/null +++ b/src/datastructure/test/LinkedListTest.java @@ -0,0 +1,71 @@ +package datastructure.test; + + +import datastructure.list.LinkedList; + +public class LinkedListTest { + public static void main(String[] args) throws Exception { + + LinkedList list1 = new LinkedList(); + list1.add("Rico"); + list1.add("Rico"); + list1.add("Livia"); + list1.add("TJU"); + list1.add("Livia"); + list1.add("NEU"); + list1.add("NEU"); + list1.add("Rico"); + list1.add("NEU"); + + System.out.println("ԭ"); + list1.print(); + System.out.println(); + + list1.remove(); + System.out.println("ɾβڵ"); + list1.print(); + + System.out.println(); + + list1.removeDuplicateNodes(); + System.out.println("ɾظ"); + list1.print(); + System.out.println(); + + System.out.println("ӡKڵ㣺"); + System.out.println(list1.getEndK(2)); + System.out.println(); + + list1.reverseLinkedList(); + System.out.println("ת"); + list1.print(); + list1.reverseLinkedList(); + list1.print(); + System.out.println(); + + System.out.println("ӡ"); + list1.reversePrint(list1.getHead()); + System.out.println(); + System.out.println(); + + System.out.println("ӡмڵ㣺"); + list1.printMiddleNodes(); + System.out.println(); + + + LinkedList list2 = new LinkedList(); + list2.add(""); + list2.add(list1.getEndK(2)); + System.out.println("жǷཻ"); + list1.print(); + list2.print(); + System.out.println(list1.size() + " : " + list2.size()); + System.out.println("Ƿཻ : " + list1.isIntersect(list2)); + System.out.println(" " + list1.getIntersectionPoint(list2)); + System.out.println(); + + list1.deleteNodeWithoutHead(list1.getEndK(2)); + System.out.println("ӡɾضڵ"); + list1.print(); + } +} diff --git a/src/datastructure/test/LinkedQueueTest.java b/src/datastructure/test/LinkedQueueTest.java new file mode 100644 index 0000000..665059b --- /dev/null +++ b/src/datastructure/test/LinkedQueueTest.java @@ -0,0 +1,28 @@ +package datastructure.test; + + +import datastructure.queue.LinkedQueue; + +public class LinkedQueueTest { + public static void main(String[] args) { + LinkedQueue queue = new LinkedQueue(); + queue.put(1); + queue.put(2); + queue.put(4); + queue.put(3); + queue.put(0); + System.out.println(queue); + System.out.println("\n ------------------- \n"); + + + queue.pop(); + System.out.println(queue); + System.out.println("\n ------------------- \n"); + + + System.out.println(queue.peek()); + queue.put(121); + System.out.println(queue); + + } +} diff --git a/src/datastructure/test/LinkedStackTest.java b/src/datastructure/test/LinkedStackTest.java new file mode 100644 index 0000000..f117122 --- /dev/null +++ b/src/datastructure/test/LinkedStackTest.java @@ -0,0 +1,48 @@ +package datastructure.test; + + + +import datastructure.stack.LinkedStack; + +import java.util.Comparator; + +public class LinkedStackTest { + // + public static void main(String[] args) throws Exception { + + LinkedStack stack = new LinkedStack(); + Comparator c = new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + // TODO Auto-generated method stub + if(o1 > o2) + return 1; + else if(o1 < o2) + return -1; + else + return 0; + } + }; + + stack.push(7,c); + stack.push(6,c); + stack.push(8,c); + stack.push(5,c); + stack.push(3,c); + + System.out.println("ԭջ(ԪջԪ)"); + stack.print(); + System.out.println(); + + System.out.println("ջԪأ"); + System.out.println(stack.peek()); + System.out.println("ջеСֵ" + stack.min()); + System.out.println(); + + System.out.println("ɾջԪغ"); + stack.pop(c); + stack.print(); + System.out.println("ջеСֵ" + stack.min()); + System.out.println(); + } +} diff --git a/src/datastructure/test/MinHeapTest.java b/src/datastructure/test/MinHeapTest.java new file mode 100644 index 0000000..1ae825f --- /dev/null +++ b/src/datastructure/test/MinHeapTest.java @@ -0,0 +1,25 @@ +package datastructure.test; + + +import datastructure.heap.MinHeap; + +public class MinHeapTest { + public static void main(String[] args) { + + int[] arr = {53, 17, 78, 9, 45, 65, 87, 23}; + MinHeap heap = new MinHeap(arr,20); + System.out.println("ѣ"); + heap.printMinHeap(0); + System.out.println("\n---------------------------\n"); + System.out.println("вԪ7󣬶ѱΪ"); + heap.insert(7); + heap.printMinHeap(0); + System.out.println("\n---------------------------\n"); + System.out.println("ɾĩβԪأѱΪ"); + heap.remove(); + heap.printMinHeap(0); + System.out.println("\n---------------------------\n"); + System.out.println("Ϊ"); + heap.sort(); + } +} diff --git a/src/datastructure/test/OptimizationStackQueueTest.java b/src/datastructure/test/OptimizationStackQueueTest.java new file mode 100644 index 0000000..e87d9b4 --- /dev/null +++ b/src/datastructure/test/OptimizationStackQueueTest.java @@ -0,0 +1,27 @@ +package datastructure.test; + + +import datastructure.queue.OptimizationStackQueue; + +public class OptimizationStackQueueTest { + public static void main(String[] args) { + OptimizationStackQueue queue = new OptimizationStackQueue(); + queue.put(1); + queue.put(3); + queue.put(5); + queue.put(2); + queue.put(8); + queue.put(6); + System.out.println(queue); + System.out.println("\n------------------\n"); + + queue.pop(); + System.out.println(queue); + System.out.println("\n------------------\n"); + + queue.put(4); + queue.put(5); + System.out.println(queue); + System.out.println("\n------------------\n"); + } +} diff --git a/src/datastructure/test/SeqQueueTest.java b/src/datastructure/test/SeqQueueTest.java new file mode 100644 index 0000000..ac49d5a --- /dev/null +++ b/src/datastructure/test/SeqQueueTest.java @@ -0,0 +1,27 @@ +package datastructure.test; + + +import datastructure.queue.SeqQueue; + +public class SeqQueueTest { + + public static void main(String[] args) { + + SeqQueue queue = new SeqQueue(5); + queue.put(1); + queue.put(2); + queue.put(3); + queue.put(4); + queue.put(3); + queue.put(4); + System.out.println(queue); + System.out.println("\n----------------\n"); + + queue.pop(); + System.out.println(queue); + System.out.println("\n----------------\n"); + + System.out.println(queue.peek()); + System.out.println(queue); + } +} diff --git a/src/datastructure/test/SortTest.java b/src/datastructure/test/SortTest.java new file mode 100644 index 0000000..3539248 --- /dev/null +++ b/src/datastructure/test/SortTest.java @@ -0,0 +1,87 @@ +package datastructure.test; + + + +import datastructure.sort.*; + +import java.util.Arrays; + +public class SortTest { + + public static void main(String[] args) { + + int[] target1 = { 38, 65, 97, 76, 13, 27, 49 }; + System.out.println("ֱѡ "); + System.out.println("ԭ " + Arrays.toString(target1)); + StraightSelectSort.selectSort(target1); + System.out.println(Arrays.toString(target1)); + + System.out.println("\n----------------------\n"); + System.out.println("ֱӲ "); + int[] target2 = { 38, 65, 97, 76, 13, 27, 49 }; + System.out.println("ԭ " + Arrays.toString(target2)); + StraightInsertionSort.insertSort(target2); + System.out.println(Arrays.toString(target2)); + + System.out.println("\n----------------------\n"); + System.out.println("ð "); + int[] target3 = { 1, 2, 3, 4, 5, 8, 7, 6 }; + System.out.println("ԭ " + Arrays.toString(target3)); + BubbleSort.bubbleSort(target3); + + System.out.println("\n----------------------\n"); + System.out.println("Żð "); + int[] target4 = { 1, 2, 3, 4, 5, 8, 7, 6 }; + System.out.println("ԭ " + Arrays.toString(target4)); + BubbleSort.optimizeBubbleSort(target4); + + System.out.println("\n----------------------\n"); + System.out.println("鲢 "); + int[] target5 = { 21, 25, 49, 25, 16, 8, 31, 41 }; + System.out.println("ԭ " + Arrays.toString(target5)); + MergeSort.mergeSort(target5, 0, target5.length - 1); + System.out.println(Arrays.toString(target5)); + + System.out.println("\n----------------------\n"); + System.out.println(" "); + int[] target6 = { 21, 25, 49, 25, 16, 8, 31, 41 }; + System.out.println("ԭ " + Arrays.toString(target6)); + QuickSort.quickSort(target6, 0, target6.length - 1); + System.out.println(Arrays.toString(target6)); + + System.out.println("\n----------------------\n"); + System.out.println("ϣ "); + int[] target7 = { 21, 25, 49, 25, 16, 8, 31, 41,1,16 }; + System.out.println("ԭ " + Arrays.toString(target7)); + ShellSort.shellSort(target7); + System.out.println(Arrays.toString(target7)); + + System.out.println("\n----------------------\n"); + System.out.println(" "); + int[] target8 = { 332, 633, 598, 232, 664, 179, 457, 825, 405, 361}; + System.out.println("ԭ " + Arrays.toString(target8)); + RadixSort.radixSort(target8,10,3,target8.length); + System.out.println(Arrays.toString(target8)); + + System.out.println("\n----------------------\n"); + System.out.println(" "); + int[] target9 = { 21, 25, 49, 25, 16, 18, 31, 41 }; + System.out.println("ԭ " + Arrays.toString(target9)); + RadixSort.radixSort(target9,10,2,target9.length); + System.out.println(Arrays.toString(target9)); + + System.out.println("\n----------------------\n"); + System.out.println(" "); + int[] target10 = { 21, 25, 49, 25, 16, 18, 31, 41 }; + System.out.println("ԭ " + Arrays.toString(target10)); + HeapSort.heapSort(target10); + System.out.println(Arrays.toString(target10)); + + System.out.println("\n----------------------\n"); + System.out.println("۰ "); + int[] target11 = { 21, 25, 49, 25, 16, 18, 31, 41, 21, 9 }; + System.out.println("ԭ " + Arrays.toString(target11)); + BinaryInsertSort.binaryInsertSort(target11); + System.out.println(Arrays.toString(target11)); + } +} diff --git a/src/datastructure/test/StackQueueTest.java b/src/datastructure/test/StackQueueTest.java new file mode 100644 index 0000000..b73d5e7 --- /dev/null +++ b/src/datastructure/test/StackQueueTest.java @@ -0,0 +1,27 @@ +package datastructure.test; + + +import datastructure.queue.StackQueue; + +public class StackQueueTest { + public static void main(String[] args) { + StackQueue queue = new StackQueue(); + queue.put(1); + queue.put(3); + queue.put(5); + queue.put(2); + queue.put(8); + queue.put(6); + System.out.println(queue); + System.out.println("\n------------------\n"); + + queue.pop(); + System.out.println(queue); + System.out.println("\n------------------\n"); + + queue.put(4); + System.out.println(queue); + System.out.println("\n------------------\n"); + } +} + diff --git a/src/datastructure/test/TestString.java b/src/datastructure/test/TestString.java new file mode 100644 index 0000000..1c70832 --- /dev/null +++ b/src/datastructure/test/TestString.java @@ -0,0 +1,32 @@ +package datastructure.test; + +public class TestString { + + + public static void main(String[] args) { + String str1 = "HelloFlyapi"; + String str2 = "HelloFlyapi"; + String str3 = new String("HelloFlyapi"); + String str4 = "Hello"; + String str5 = "Flyapi"; + String str6 = "Hello" + "Flyapi"; + String str7 = str4 + str5; + + System.out.println("str1 == str2 result: " + (str1 == str2)); + + System.out.println("str1 == str3 result: " + (str1 == str3)); + + System.out.println("str1 == str6 result: " + (str1 == str6)); + + System.out.println("str1 == str7 result: " + (str1 == str7)); + + System.out.println("str1 == str7.intern() result: " + (str1 == str7.intern())); + + System.out.println("str3 == str3.intern() result: " + (str3 == str3.intern())); + + } + + + + +} diff --git a/src/datastructure/tree/BinaryTree.java b/src/datastructure/tree/BinaryTree.java new file mode 100644 index 0000000..949d380 --- /dev/null +++ b/src/datastructure/tree/BinaryTree.java @@ -0,0 +1,550 @@ +package datastructure.tree; + +import java.util.LinkedList; + +/** + * Title: (Խṹ)Ĺز + * Description: + * Թʽַ'()'ǰʾ㣬öŸŲʡ + * IJ/㷨 + * ǰ򡢺ĵݹͷǵݹ㷨(ÿڵԣֱʽҪýΣΨһڸýķʱ) + * ݶǰ򡢺 + * ĸ߶ + * Ľ + * ĸ㸴һŶ + * ȡĸ㣬ӽڵ + * ӡ + * жŶǷ + * + * @author rico + * @created 2017523 11:16:12 + */ +public class BinaryTree { + + + /** ĸ (@author: rico) */ + private Node root; + + + /** + * ޲ι캯 + * @description Ĭ޲ι캯 + * @author rico + * @created 2017524 3:36:35 + */ + public BinaryTree() { + super(); + } + + /** + * 캯 + * + * @description һĸ㸴ƹ + * @author rico + * @created 2017523 2:17:06 + * @param node + * ԭĸ + */ + public BinaryTree(Node node) { + // TODO Auto-generated constructor stub + this.root = copy(node); + } + + + /** + * 캯 + * @description һǰƹ + * @author rico + * @created 2017524 3:38:02 + * @param preOrderStr + */ + public BinaryTree(char[] preOrderStr) { + root = createTreeByPreOrederStr(preOrderStr, null); + } + + + /** + * 캯 + * @description һǰ+(+)ƹ + * @author rico + * @created 2017524 3:38:33 + * @param s1 + * @param s2 + * @param isPreIn + */ + public BinaryTree(String s1, String s2, boolean isPreIn) { + if (isPreIn) { + root = createBinaryTreeByPreAndIn(s1, s2); + }else{ + root = createBinaryTreeByInAndPost(s1, s2); + } + } + + /** + * @description ݹʽ + * @author rico + * @created 2017522 3:16:01 + * @param exp + */ + public void createBinaryTree(String exp) { + LinkedList> stack = new LinkedList>(); // ջ + Node node = null; // ½ + Node temp = null; // ջ + Node parent = null; // ׽ + boolean flag = false; // true ʾ뵽λãfalseʾ븸Һλ + + for (int i = 0; i < exp.length(); i++) { // ʽĸַ + char c = exp.charAt(i); + switch (c) { + case '(': // ǰڵкӽڵ㣬ջԱ亢 + stack.push(temp); + flag = true; + break; + case ')': // úջڵĺӣջ + stack.pop(); + break; + case ',': // ǰڵ޺ӣҪ亢ӽڵ㣬˲Ҫջ + flag = false; + break; + default: // ݴڵ + node = new Node(c); + break; + } + + // ڣ򴴽ĸ + if (root == null) { + root = node; + } + + // ΪջڵŮ + if (!stack.isEmpty()) { + if (node != null) { // '('')'','ַʱԹ + parent = stack.peek(); + if (flag) { + parent.left = node; + } else { + parent.right = node; + } + } + } + + temp = node; // ջ + node = null; // nodeÿ + } + } + + /** + * @description ݹʽ + * @author rico + * @created 2017522 3:16:01 + * @param exp + */ + public static Node createBinaryTree(String exp, Node root) { + LinkedList stack = new LinkedList(); // ջ + Node node = null; // ½ + Node temp = null; // ջ + Node parent = null; // ׽ + boolean flag = false; // true ʾ뵽λãfalseʾ븸Һλ + + for (int i = 0; i < exp.length(); i++) { // ʽĸַ + char c = exp.charAt(i); + switch (c) { + case '(': // ǰڵкӽڵ㣬ջԱ亢 + stack.push(temp); + flag = true; + break; + case ')': // úջڵĺӣջ + stack.pop(); + break; + case ',': // ǰڵ޺ӣҪ亢ӽڵ㣬˲Ҫջ + flag = false; + break; + default: // ݴڵ + node = new Node(c); + break; + } + + // ڣ򴴽ĸ + if (root == null) { + root = node; + } + + // ΪջڵŮ + if (!stack.isEmpty()) { + if (node != null) { // '('')'','ַʱԹ + parent = stack.peek(); + if (flag) { + parent.left = node; + } else { + parent.right = node; + } + } + } + + temp = node; // ջ + node = null; // nodeÿ + } + return root; + } + + /** + * @description /α + * @author rico + * @created 2017522 3:05:57 + * @return + */ + public String levelOrder() { + StringBuilder sb = new StringBuilder(); + LinkedList> queue = new LinkedList>(); // + if (root != null) { + queue.add(root); + while (!queue.isEmpty()) { + Node temp = queue.pop(); + sb.append(temp.data).append(" "); + + // ڱǰڵʱͬʱҺ + if (temp.left != null) + queue.add(temp.left); + if (temp.right != null) + queue.add(temp.right); + } + } + return sb.toString().trim(); + } + + /** + * @description ǰ(ݹ) + * @author rico + * @created 2017522 3:06:11 + * @param root + * @return + */ + public String preOrder(Node root) { + StringBuilder sb = new StringBuilder(); // 浽ݹջ + if (root != null) { // ݹֹ + sb.append(root.data + " "); // ǰǰ + sb.append(preOrder(root.left)); // ǰ + sb.append(preOrder(root.right)); // ǰ + } + return sb.toString(); + } + + /** + * @description ǰ():Խṹ()ջǰڵջ + * @author rico + * @created 2017524 8:48:09 + * @return + */ + public String preOrder() { + + StringBuilder sb = new StringBuilder(); + LinkedList> stack = new LinkedList>(); // ջ¼· + Node node = root; + + while (node != null || !stack.isEmpty()) { // + if (node != null) { // ǰڵ㲻Ϊ + sb.append(node.data + " "); // ʵǰڵ + stack.push(node); // ǰڵջ + node = node.left; // + } else { + node = stack.pop(); // 丸ڵ + node = node.right; // + } + } + return sb.toString(); + } + + /** + * @description (ݹ) + * @author rico + * @created 2017522 3:06:28 + * @param root + * @return + */ + public String inOrder(Node root) { + StringBuilder sb = new StringBuilder(); // 浽ݹջ + if (root != null) { // ݹֹ + sb.append(inOrder(root.left)); // + sb.append(root.data + " "); // ǰ + sb.append(inOrder(root.right)); // + } + return sb.toString(); + } + + /** + * @description ()Խṹ()ջǰڵջ + * + * @author rico + * @created 2017524 9:22:31 + * @return + */ + public String inOrder() { + StringBuilder sb = new StringBuilder(); + LinkedList> stack = new LinkedList>(); // ջ¼· + Node node = root; + + while (node != null || !stack.isEmpty()) { // + if (node != null) { // ǰڵ㲻Ϊ + stack.push(node); // ǰڵջ + node = node.left; // + } else { + node = stack.pop(); // ڵ㵯ջ + sb.append(node.data + " "); // ʸڵ + node = node.right; // + } + } + return sb.toString(); + } + + /** + * @description (ݹ) + * @author rico + * @created 2017522 3:06:44 + * @param root + * @return + */ + public String postOrder(Node root) { + StringBuilder sb = new StringBuilder(); // 浽ݹջ + if (root != null) { // ݹֹ + sb.append(postOrder(root.left)); // + sb.append(postOrder(root.right)); // + sb.append(root.data + " "); // ǰ + } + return sb.toString(); + } + + /** + * @description ():Խṹ()ջǰڵջ + * αһڵʱŷ,ҪڽڵNodeһboolֶΣڱǷҪڱηʸýڵ + * @author rico + * @created 2017524 9:34:48 + * @return + */ + public String postOrder() { + StringBuilder sb = new StringBuilder(); + LinkedList> stack = new LinkedList>(); // ¼·Ĺջ + Node node = root; + while (node != null || !stack.isEmpty()) { // + if (node != null) { // ǰڵ㲻Ϊ + node.isFirst = true; // ״ηʸýڵ㣬Ϊtrue + stack.push(node); // ѹջ + node = node.left; // + } else { // ǰڵΪյջΪ + node = stack.pop(); // ǰڵ㵯ջ + if (node.isFirst) { + node.isFirst = false; // ڶηʸýڵ,Ϊfalse + stack.push(node); // ֻڵβŷʣˣǰڵٴѹջ + node = node.right; // ʸýڵ + } else { // ηʸýڵ + sb.append(node.data + " "); // + node = null; // ǰڵѷ,ҪʹջеĽڵ + } + } + } + return sb.toString(); + } + + /** + * @description ǰؽ + * @author rico + * @created 2017524 12:24:41 + * @return + */ + public Node createBinaryTreeByPreAndIn(String pre, String in) { + if (pre.length() > 0) { + Node root = new Node(pre.charAt(0)); + int index = in.indexOf(pre.charAt(0)); + root.left = createBinaryTreeByPreAndIn(pre.substring(1, index + 1), + in.substring(0, index)); + root.right = createBinaryTreeByPreAndIn( + pre.substring(index + 1, pre.length()), + in.substring(index + 1, in.length())); + return root; + } + return null; + } + + /** + * @description 򡢺ؽ + * @author rico + * @created 2017524 12:24:43 + * @return + */ + public Node createBinaryTreeByInAndPost(String in, String post) { + if (post.length() > 0) { + Node root = new Node(post.charAt(post.length() - 1)); + int index = in.indexOf(post.charAt(post.length() - 1)); + + root.left = createBinaryTreeByInAndPost(in.substring(0, index), + post.substring(0, index)); + root.right = createBinaryTreeByInAndPost( + in.substring(index + 1, in.length()), + post.substring(index, post.length() - 1)); + return root; + } + return null; + } + + /** + * @description ԭĸ㸴Ƴһһģһ + * @author rico + * @created 2017523 2:21:08 + * @param root + * @return + */ + public Node copy(Node root) { + if (root == null) + return null; + Node node = new Node(null); + node.data = root.data; + node.left = copy(root.left); + node.right = copy(root.right); + return node; + } + + /** createTreeByPreOrederStrҪõָ (@author: rico) */ + private int index = 0; + + /** + * @description ǰؽеҶӽڵ㶼"#"ʾ + * @author rico + * @created 2017524 7:51:54 + * @param preOrderStr + * @param temp + * @return + */ + public Node createTreeByPreOrederStr(char[] preOrderStr, Node temp) { + if (index < preOrderStr.length) { + char c = preOrderStr[index++]; + if (c != '#') { // ݹֹ + Node node = new Node(c); + node.left = createTreeByPreOrederStr(preOrderStr, node); // ݹΪǰڵ㴴 + node.right = createTreeByPreOrederStr(preOrderStr, node); // ݹΪǰڵ㴴 + return node; + } + return null; + } + return null; + } + + /** + * @description ȡĸ + * @author rico + * @created 2017522 3:09:18 + * @return + */ + public Node getRoot() { + return root; + } + + /** + * @description õǰӽ + * @author rico + * @created 2017523 11:13:48 + * @param node + * @return + */ + public Node getLeftChild(Node node) { + return node.left; + } + + /** + * @description õǰҺӽ + * @author rico + * @created 2017523 11:13:50 + * @param node + * @return + */ + public Node getRightChild(Node node) { + return node.right; + } + + /** + * @description ˼룺нڵ + * @author rico + * @created 2017523 11:59:19 + * @param root + * @return + */ + public int size(Node root) { + if (root != null) { // ݹֹ + return size(root.left) + size(root.right) + 1; + } + return 0; + } + + /** + * @description ˼룺ĸ߶(Ϊ0) + * @author rico + * @created 2017523 12:00:08 + * @param root + * @return + */ + public int height(Node root) { + if (root != null) { // ݹֹ + int h1 = height(root.left); + int h2 = height(root.right); + return h1 > h2 ? h1 + 1 : h2 + 1; + } + return 0; + } + + /** + * @description ԹʽӡǰӦ + * @author rico + * @created 2017524 8:13:08 + * @param root + * @return + */ + public String printBinaryTree(Node root) { + StringBuilder sb = new StringBuilder(); + if (root != null) { + sb.append(root.data); + if (root.left != null || root.right != null) { + sb.append('('); + sb.append(printBinaryTree(root.left)); + sb.append(','); + sb.append(printBinaryTree(root.right)); + sb.append(')'); + } + } + return sb.toString(); + } + + @Override + public String toString() { + // TODO Auto-generated method stub + return levelOrder(); + } + + /** + * @description ĸжǷ + * @author rico + * @created 2017523 3:05:35 + * @param src + * ԭĸ + * @param des + * Ŀĸ + * @return + */ + private boolean equals0(Node src, Node des) { + if (src == null && des == null) { // + return true; + } else if (src == null || des == null) { // ǿ + return false; + } else { // ǿǿǷȣǰڵǷ && Ƿ && Ƿ + return src.equals(des) && equals0(src.left, des.left) + && equals0(src.right, des.right); + } + } + + @Override + public boolean equals(Object obj) { + // TODO Auto-generated method stub + if (obj instanceof BinaryTree) { // ԷǷҲһŶ + BinaryTree tree = (BinaryTree) obj; + return equals0(this.root, tree.root); + } + return false; + } +} \ No newline at end of file diff --git a/src/datastructure/tree/Node.java b/src/datastructure/tree/Node.java new file mode 100644 index 0000000..8673397 --- /dev/null +++ b/src/datastructure/tree/Node.java @@ -0,0 +1,49 @@ +package datastructure.tree; + +/** + * Title: + * Description: Ľ + * + * @author rico + * @created 201746 9:55:58 + */ + public class Node { + + T data; // + Node left; // ָӽ + Node right; // ָҺӽ + boolean isFirst; // ڷǵݹ + + + /** + * 캯 + * + * @description һ½ + * @author rico + * @created 201746 9:56:56 + * @param data + * Ԫ + * @param next + * ԪϽ + */ + public Node(T data) { + this.data = data; + } + + @Override + public String toString() { + return data == null ? null : data.toString(); + } + + @Override + public boolean equals(Object obj) { + // TODO Auto-generated method stub + if (obj instanceof Node) { + Node temp = (Node) obj; + if (data.equals(temp.data)) { + return true; + } + } + return false; + } +} diff --git a/src/designpattern/adapter/Adapter.java b/src/designpattern/adapter/Adapter.java index c562675..fec3acb 100644 --- a/src/designpattern/adapter/Adapter.java +++ b/src/designpattern/adapter/Adapter.java @@ -11,6 +11,6 @@ public class Adapter extends Target { @Override public void request() { - adaptee.specificRequest(); + adaptee.specificRequest(); } } diff --git a/src/designpattern/myadapter/Voltage220.java b/src/designpattern/myadapter/Voltage220.java new file mode 100644 index 0000000..dd6d80d --- /dev/null +++ b/src/designpattern/myadapter/Voltage220.java @@ -0,0 +1,9 @@ +package designpattern.myadapter; + +public class Voltage220 { + public int output220V(){ + int src = 220; + System.out.println("我是"+src+"V"); + return src ; + } +} diff --git a/src/designpattern/myadapter/Voltage5.java b/src/designpattern/myadapter/Voltage5.java new file mode 100644 index 0000000..7578ed4 --- /dev/null +++ b/src/designpattern/myadapter/Voltage5.java @@ -0,0 +1,5 @@ +package designpattern.myadapter; + +public interface Voltage5 { + int output5v(); +} diff --git a/src/designpattern/myadapter/VoltageAdapter.java b/src/designpattern/myadapter/VoltageAdapter.java new file mode 100644 index 0000000..2746e3f --- /dev/null +++ b/src/designpattern/myadapter/VoltageAdapter.java @@ -0,0 +1,16 @@ +package designpattern.myadapter; + +/** + * 适配器 + */ +public class VoltageAdapter extends Voltage220 implements Voltage5{ + + @Override + public int output5v() { + int src = output220V(); + System.out.println("适配器工作开始适配电压"); + int dst = src / 44; + System.out.println("适配完成后输出电压:" + dst); + return dst; + } +} From e2e353d481832f82ced8f7cd00d7c32614a15855 Mon Sep 17 00:00:00 2001 From: liupeng Date: Mon, 7 Dec 2020 18:35:18 +0800 Subject: [PATCH 2/6] T1 add datastructure code --- src/datastructure/backtrack/EightQueen.java | 7 +- src/datastructure/list/LinkedList.java | 69 ++++++++++++----- src/datastructure/list/Node.java | 1 + src/datastructure/queue/LinkedQueue.java | 10 ++- .../queue/OptimizationStackQueue.java | 12 ++- src/datastructure/queue/SeqQueue.java | 3 +- src/datastructure/queue/StackQueue.java | 7 +- src/datastructure/sort/BinaryInsertSort.java | 12 ++- .../sort/StraightInsertionSort.java | 13 ++-- .../sort/StraightSelectSort.java | 3 +- src/datastructure/stack/LinkedStack.java | 10 ++- src/datastructure/stack/Node.java | 5 +- src/datastructure/stack/SeqStack.java | 13 ++-- src/datastructure/test/LinkedListTest.java | 77 ++++++++++++------- src/datastructure/test/LinkedQueueTest.java | 2 +- 15 files changed, 161 insertions(+), 83 deletions(-) diff --git a/src/datastructure/backtrack/EightQueen.java b/src/datastructure/backtrack/EightQueen.java index d396774..fbe9303 100644 --- a/src/datastructure/backtrack/EightQueen.java +++ b/src/datastructure/backtrack/EightQueen.java @@ -10,9 +10,10 @@ * @created 2017531 4:54:17 */ public class EightQueen { - - private static final short N = 8; // ʹó壬֮Nʺ - private static int count = 0; // + // ʹó壬֮Nʺ + private static final short N = 8; + // + private static int count = 0; public static void main(String[] args) { Date begin = new Date(); diff --git a/src/datastructure/list/LinkedList.java b/src/datastructure/list/LinkedList.java index ee5da49..4ff2925 100644 --- a/src/datastructure/list/LinkedList.java +++ b/src/datastructure/list/LinkedList.java @@ -48,7 +48,8 @@ public Node add(E data, int index) throws Exception { for (int i = 0; i < index; i++) { cur = cur.next; } - Node node = new Node(data); // Ԫ + // Ԫ + Node node = new Node(data); cur.next = node; size++; return node; @@ -168,16 +169,23 @@ public Node getEndK(int k) { * @author rico */ public void reverseLinkedList() { - Node cur = head.next; // ԭ - Node pre = null; // ת - - while (cur != null) { // ԭеÿڵзת - Node next = cur.next; // ¼ǰڵһڵ - cur.next = pre; // ǰڵָת - pre = cur; // ·ת - cur = next; // µǰڵ + // ԭ + Node cur = head.next; + // ת + Node pre = null; + // ԭеÿڵзת + while (cur != null) { + // ¼ǰڵһڵ + Node next = cur.next; + // ǰڵָת + cur.next = pre; + // ·ת + pre = cur; + // µǰڵ + cur = next; } - head.next = pre; // ԭͷָת + // ԭͷָת + head.next = pre; } /** @@ -208,9 +216,14 @@ public void print() { * @param head */ public void reversePrint(Node head) { + if (head.next != null) { - reversePrint(head.next); // "ȥ" - System.out.print(head.next.data + " "); // ""ʼӡ + // "ȥ" + System.out.println("~~"+head.data); + reversePrint(head.next); + System.out.println(head.data+"~"); + // ""ʼӡ + System.out.print(head.next.data + " "); } } @@ -241,8 +254,10 @@ public void printMiddleNodes() { * @return */ public boolean hasLoop() { - Node index1 = head.next; // ָ - Node index2 = head.next; // ָ + // ָ + Node index1 = head.next; + // ָ + Node index2 = head.next; while (index2 != null && index2.next != null && index2.next.next != null) { index1 = index1.next; @@ -261,7 +276,8 @@ public boolean hasLoop() { * @return */ public boolean deleteNodeWithoutHead(Node node) { - if (node == null || node.next == null) { // ָڵΪջΪβڵʱ޷ɾ + // ָڵΪջΪβڵʱ޷ɾ + if (node == null || node.next == null) { return false; } @@ -284,8 +300,10 @@ public boolean deleteNodeWithoutHead(Node node) { * @return */ public boolean isIntersect(LinkedList list2) { - Node cur1 = head.next; // ǰ - Node cur2 = list2.getHead().next; // Ŀ + // ǰ + Node cur1 = head.next; + // Ŀ + Node cur2 = list2.getHead().next; // һΪգ򷵻 false if(cur1 == null || cur2 == null){ @@ -301,8 +319,8 @@ public boolean isIntersect(LinkedList list2) { while(cur2.next != null){ cur2 = cur2.next; } - - return cur1 == cur2; // ཻȡβڵǷͬ + // ཻȡβڵǷͬ + return cur1 == cur2; } /** @@ -348,4 +366,17 @@ public Node getIntersectionPoint(LinkedList list2) { public int size(){ return size; } + + public int factorial(int n){ + if(n == 1){ + //System.out.println( 1); + return 1; + } else { + System.out.println(n +"*" +factorial(n - 1)); + + return n * factorial(n - 1); + + } + + } } diff --git a/src/datastructure/list/Node.java b/src/datastructure/list/Node.java index 66e2a35..32eb866 100644 --- a/src/datastructure/list/Node.java +++ b/src/datastructure/list/Node.java @@ -13,6 +13,7 @@ public class Node { T data; /** + * * 캯 * * @description һ½ڵ diff --git a/src/datastructure/queue/LinkedQueue.java b/src/datastructure/queue/LinkedQueue.java index dbc0d84..c2ce065 100644 --- a/src/datastructure/queue/LinkedQueue.java +++ b/src/datastructure/queue/LinkedQueue.java @@ -9,10 +9,12 @@ */ public class LinkedQueue { - - private Node head; // ͷ - private Node rear; // βָ - private int size; // дС + // ͷ + private Node head; + // βָ + private Node rear; + // дС + private int size; public LinkedQueue(){ head = rear = new Node(null); diff --git a/src/datastructure/queue/OptimizationStackQueue.java b/src/datastructure/queue/OptimizationStackQueue.java index 0bddae5..53c0e29 100644 --- a/src/datastructure/queue/OptimizationStackQueue.java +++ b/src/datastructure/queue/OptimizationStackQueue.java @@ -11,9 +11,10 @@ * @created 2017519 10:45:11 */ public class OptimizationStackQueue { - - private LinkedStack stack1; // ջ - private LinkedStack stack2; // ջ + // ջ + private LinkedStack stack1; + // ջ + private LinkedStack stack2; public OptimizationStackQueue() { @@ -36,11 +37,16 @@ public void put(E e) { * ΪգȽstack1еԪȫstack2ٶstack2ִеջ * ֱӶstack2ִеջ * @author rico + * * @created 2017519 10:48:32 + * * @return */ public E pop() { + + if (stack2.isEmpty()) { + while (!stack1.isEmpty()) { stack2.push(stack1.pop().getData()); } diff --git a/src/datastructure/queue/SeqQueue.java b/src/datastructure/queue/SeqQueue.java index ad9f7d2..426383d 100644 --- a/src/datastructure/queue/SeqQueue.java +++ b/src/datastructure/queue/SeqQueue.java @@ -15,7 +15,8 @@ public class SeqQueue { /** еĴ洢ṹ (@author: rico) */ private Object[] queue; private int size; - private int maxSize; // + // + private int maxSize; public SeqQueue(int maxSize){ this.maxSize = maxSize; diff --git a/src/datastructure/queue/StackQueue.java b/src/datastructure/queue/StackQueue.java index 4fd33ab..38d9490 100644 --- a/src/datastructure/queue/StackQueue.java +++ b/src/datastructure/queue/StackQueue.java @@ -10,9 +10,10 @@ * @created 2017519 10:45:11 */ public class StackQueue { - - private LinkedStack stack1; // 洢ռ - private LinkedStack stack2; //ʱ + // 洢ռ + private LinkedStack stack1; + //ʱ + private LinkedStack stack2; public StackQueue() { stack1 = new LinkedStack(); diff --git a/src/datastructure/sort/BinaryInsertSort.java b/src/datastructure/sort/BinaryInsertSort.java index 11438dc..8e253fe 100644 --- a/src/datastructure/sort/BinaryInsertSort.java +++ b/src/datastructure/sort/BinaryInsertSort.java @@ -20,14 +20,18 @@ public static int[] binaryInsertSort(int[] target) { int right = i - 1; int mid; int temp = target[i]; - if(temp < target[right]){ // ǰֵСеֵʱʼҲλ + // ǰֵСеֵʱʼҲλ + if(temp < target[right]){ while(left <= right){ mid = (left + right)/2; if(target[mid] < temp){ - left = mid + 1; // С + // С + left = mid + 1; }else if(target[mid] > temp){ - right = mid - 1; // С - }else{ // ֵеtarget[mid]ȣ֤ȶԵĴ + // С + right = mid - 1; + // ֵеtarget[mid]ȣ֤ȶԵĴ + }else{ left = left + 1; } } diff --git a/src/datastructure/sort/StraightInsertionSort.java b/src/datastructure/sort/StraightInsertionSort.java index e85762c..8978ad6 100644 --- a/src/datastructure/sort/StraightInsertionSort.java +++ b/src/datastructure/sort/StraightInsertionSort.java @@ -13,11 +13,14 @@ public class StraightInsertionSort { public static int[] insertSort(int[] target){ - - if(target != null && target.length != 1){ // 鲻Ϊҳȴ1 - for (int i = 1; i < target.length; i++) { // Уֱչ - for (int j = i; j > 0; j--) { // вµԪ - if(target[j] < target[j-1]){ // + // 鲻Ϊҳȴ1 + if(target != null && target.length != 1){ + // Уֱչ + for (int i = 1; i < target.length; i++) { + // вµԪ + for (int j = i; j > 0; j--) { + // + if(target[j] < target[j-1]){ int temp = target[j]; target[j] = target[j-1]; target[j-1] = temp; diff --git a/src/datastructure/sort/StraightSelectSort.java b/src/datastructure/sort/StraightSelectSort.java index 4ea8842..0f51f04 100644 --- a/src/datastructure/sort/StraightSelectSort.java +++ b/src/datastructure/sort/StraightSelectSort.java @@ -22,7 +22,8 @@ public static int[] selectSort(int[] target){ min_index = j; } } - if(target[min_index] != target[i]){ // ²ȶأ + // ²ȶأ + if(target[min_index] != target[i]){ int min = target[min_index]; target[min_index] = target[i]; target[i] = min; diff --git a/src/datastructure/stack/LinkedStack.java b/src/datastructure/stack/LinkedStack.java index 19e5ee9..17e9939 100644 --- a/src/datastructure/stack/LinkedStack.java +++ b/src/datastructure/stack/LinkedStack.java @@ -11,9 +11,10 @@ * @created 201746 7:52:34 */ public class LinkedStack { - - private Node top; // ջԪ - private int size; // ʽջĴС + // ջԪ + private Node top; + // ʽջĴС + private int size; /** Сֵջ (@author: rico) */ private LinkedStack min; @@ -84,7 +85,8 @@ public Node pop(){ * @description ɾջԪ,ʹСֵջ * @author rico * @return - * @throws Exception + * @throws Exception + * */ public Node pop(Comparator c){ Node temp = this.pop(); diff --git a/src/datastructure/stack/Node.java b/src/datastructure/stack/Node.java index 5064600..0115d64 100644 --- a/src/datastructure/stack/Node.java +++ b/src/datastructure/stack/Node.java @@ -19,9 +19,8 @@ public class Node { * @author rico * @created 201746 9:56:56 * @param data - * Ԫ - * @param next - * ԪϽڵ + * Ԫ + * ԪϽڵ */ public Node(T data) { this.data = data; diff --git a/src/datastructure/stack/SeqStack.java b/src/datastructure/stack/SeqStack.java index c3e3c11..b9471e1 100644 --- a/src/datastructure/stack/SeqStack.java +++ b/src/datastructure/stack/SeqStack.java @@ -9,10 +9,12 @@ * @created 201746 5:27:13 */ public class SeqStack { - - private Object[] stack; // ֧ - private int top; // ջָ - private int maxSize; // ջ + // ֧ + private Object[] stack; + // ջָ + private int top; + // ջ + private int maxSize; // ĬϹ캯 public SeqStack(){ @@ -38,7 +40,8 @@ public E pop() throws Exception{ throw new Exception("ջΪ..."); } E element = (E)stack[top]; - stack[top --] = null; // ɾԪ + // ɾԪ + stack[top --] = null; return element; } diff --git a/src/datastructure/test/LinkedListTest.java b/src/datastructure/test/LinkedListTest.java index 86430b8..a4b8a02 100644 --- a/src/datastructure/test/LinkedListTest.java +++ b/src/datastructure/test/LinkedListTest.java @@ -1,59 +1,79 @@ package datastructure.test; -import datastructure.list.LinkedList; +import java.util.List; public class LinkedListTest { public static void main(String[] args) throws Exception { - - LinkedList list1 = new LinkedList(); - list1.add("Rico"); - list1.add("Rico"); - list1.add("Livia"); - list1.add("TJU"); - list1.add("Livia"); - list1.add("NEU"); - list1.add("NEU"); - list1.add("Rico"); - list1.add("NEU"); - + + List ll = new java.util.LinkedList<>(); + ll.add(1+""); + int a = 1; + int b= 101; + + System.out.println("~~"+1%6); + System.out.println("~~"+2%6); + System.out.println("~~"+3%6); + System.out.println("~~"+4%6); + System.out.println("~~"+5%6); + System.out.println("~~"+6%6); + System.out.println("~~"+7%6); + System.out.println("~~"+8%6); + + + /*LinkedList list1 = new LinkedList(); + list1.add("A"); + list1.add("B"); + list1.add("C"); + list1.add("B"); + list1.add("E"); + list1.add("C"); + list1.add(list1.getEndK(2)); + + + System.out.println("ԭ"); list1.print(); System.out.println(); - - list1.remove(); + + + System.out.println("жǷл"); + System.out.println(list1.hasLoop()); + list1.print(); + + //list1.remove(); System.out.println("ɾβڵ"); list1.print(); - + System.out.println(); - - list1.removeDuplicateNodes(); + + // list1.removeDuplicateNodes(); System.out.println("ɾظ"); list1.print(); System.out.println(); - + System.out.println("ӡKڵ㣺"); - System.out.println(list1.getEndK(2)); + // System.out.println(list1.getEndK(3)); System.out.println(); - + list1.reverseLinkedList(); System.out.println("ת"); list1.print(); list1.reverseLinkedList(); list1.print(); System.out.println(); - + System.out.println("ӡ"); list1.reversePrint(list1.getHead()); System.out.println(); System.out.println(); - + System.out.println("ӡмڵ㣺"); list1.printMiddleNodes(); System.out.println(); - - - LinkedList list2 = new LinkedList(); + + + LinkedList list2 = new LinkedList<>(); list2.add(""); list2.add(list1.getEndK(2)); System.out.println("жǷཻ"); @@ -63,9 +83,12 @@ public static void main(String[] args) throws Exception { System.out.println("Ƿཻ : " + list1.isIntersect(list2)); System.out.println(" " + list1.getIntersectionPoint(list2)); System.out.println(); - + list1.deleteNodeWithoutHead(list1.getEndK(2)); System.out.println("ӡɾضڵ"); list1.print(); +*/ + + } } diff --git a/src/datastructure/test/LinkedQueueTest.java b/src/datastructure/test/LinkedQueueTest.java index 665059b..dd3cfc1 100644 --- a/src/datastructure/test/LinkedQueueTest.java +++ b/src/datastructure/test/LinkedQueueTest.java @@ -5,7 +5,7 @@ public class LinkedQueueTest { public static void main(String[] args) { - LinkedQueue queue = new LinkedQueue(); + LinkedQueue queue = new LinkedQueue<>(); queue.put(1); queue.put(2); queue.put(4); From 5d9f8f588a1ce9753280be4f3faa922d0bf3a86a Mon Sep 17 00:00:00 2001 From: liupeng Date: Thu, 8 Apr 2021 10:09:08 +0800 Subject: [PATCH 3/6] T1 add package dome --- src/datastructure/exercise/MaxPQ.java | 15 ++ src/datastructure/sort/MergeSort.java | 28 +++- src/datastructure/sort/QuickSort.java | 21 ++- .../sort/StraightInsertionSort.java | 9 +- .../sort/StraightSelectSort.java | 6 +- src/datastructure/test/LinkedListTest.java | 6 +- src/datastructure/test/SortTest.java | 6 +- src/dome/HttpDownloaderPDF.java | 139 ++++++++++++++++++ 8 files changed, 210 insertions(+), 20 deletions(-) create mode 100644 src/datastructure/exercise/MaxPQ.java create mode 100644 src/dome/HttpDownloaderPDF.java diff --git a/src/datastructure/exercise/MaxPQ.java b/src/datastructure/exercise/MaxPQ.java new file mode 100644 index 0000000..20ab752 --- /dev/null +++ b/src/datastructure/exercise/MaxPQ.java @@ -0,0 +1,15 @@ +package datastructure.exercise; + +/** + * @author liupe + * Priority Queue + */ +public class MaxPQ { + + private int [] qp; + private int N =0; + + public MaxPQ(int[] qp) { + this.qp = qp; + } +} diff --git a/src/datastructure/sort/MergeSort.java b/src/datastructure/sort/MergeSort.java index 6ae0dea..5a95511 100644 --- a/src/datastructure/sort/MergeSort.java +++ b/src/datastructure/sort/MergeSort.java @@ -22,7 +22,8 @@ public class MergeSort { /** - * @description 鲢㷨(ݹ㷨)ȥֽ⣬ϲ + * @description 鲢㷨(ݹ㷨)ȥֽ⣬ϲ in + * t[] target5 = { 21, 25, 49, 25, 16, 8, 31, 41 }; * @author rico * @created 2017520 4:04:52 * @param target @@ -31,14 +32,18 @@ public class MergeSort { * @return */ public static int[] mergeSort(int[] target, int left, int right) { - - if(right > left){ // ݹֹ + // ݹֹ + if(right > left){ int mid = (left + right)/2; - mergeSort(target, left, mid); // 鲢һ - mergeSort(target, mid+1, right); // 鲢ڶ - return merge(target,left,mid,right); // ϲгԭ + // 鲢һ + mergeSort(target, left, mid); + // 鲢ڶ + mergeSort(target, mid+1, right); + // ϲгԭ + return merge(target,left,mid,right); } return target; + } @@ -64,11 +69,18 @@ public static int[] merge(int[] target, int left, int mid, int right){ // δ꣬Ƚ while(s1 <= mid && s2 <= right){ - if(temp[s1] <= temp[s2]){ // ȶ + // ȶ + + + + if(temp[s1] <= temp[s2]){ + target[index++] = temp[s1++]; }else{ target[index++] = temp[s2++]; + } + } //һδ꣬ @@ -83,3 +95,5 @@ public static int[] merge(int[] target, int left, int mid, int right){ return target; } } + + diff --git a/src/datastructure/sort/QuickSort.java b/src/datastructure/sort/QuickSort.java index 052b734..2b9abdf 100644 --- a/src/datastructure/sort/QuickSort.java +++ b/src/datastructure/sort/QuickSort.java @@ -39,10 +39,14 @@ public class QuickSort { * @return */ public static int[] quickSort(int[] target, int left, int right) { - if(right > left){ // 递归终止条件 - int base_index = partition(target,left, right); // 原序列划分后基准元素的位置 - quickSort(target, left, base_index-1); // 对第一个子序列快速排序,不包含基准元素! - quickSort(target, base_index+1, right); // 对第二个子序列快速排序,不包含基准元素! + // 递归终止条件 + if(right > left){ + // 原序列划分后基准元素的位置 + int base_index = partition(target,left, right); + // 对第一个子序列快速排序,不包含基准元素! + quickSort(target, left, base_index-1); + // 对第二个子序列快速排序,不包含基准元素! + quickSort(target, base_index+1, right); } return target; } @@ -53,17 +57,22 @@ public static int[] quickSort(int[] target, int left, int right) { * @author rico * @created 2017年5月20日 下午5:10:54 * @param target 序列 + * * @param left 序列左端 * @param right 序列右端 * @return */ public static int partition(int[] target, int left, int right){ + int base = target[left]; // 基准元素的值 int base_index = left; // 基准元素最终应该在的位置 + - for (int i = left+1; i <= right; i++) { // 从基准元素的下一个元素开始 + for (int i = left+1; i <= right; i++) { // 从基 + // 准元素的下一个元素开始 if(target[i] < base){ // 若其小于基准元素 + base_index++; // 若其小于基准元素,则基准元素最终位置后移;否则不用移动 if(base_index != i){ // 相等情况意味着i之前的元素都小于base,只需要换一次即可,不需要次次都换 int temp = target[base_index]; @@ -74,6 +83,8 @@ public static int partition(int[] target, int left, int right){ } // 将基准元素就位 + + target[left] = target[base_index]; target[base_index] = base; diff --git a/src/datastructure/sort/StraightInsertionSort.java b/src/datastructure/sort/StraightInsertionSort.java index 8978ad6..1d2cd1c 100644 --- a/src/datastructure/sort/StraightInsertionSort.java +++ b/src/datastructure/sort/StraightInsertionSort.java @@ -1,6 +1,8 @@ package datastructure.sort; - -/** + +import java.util.Arrays; + +/** * Title: еֱӲ ڳʼ * Description: вϲµļ¼ԴﵽĿ * ʱ临ӶȣO(n)ƽO(n^2)O(n^2) @@ -24,8 +26,11 @@ public static int[] insertSort(int[] target){ int temp = target[j]; target[j] = target[j-1]; target[j-1] = temp; + System.out.println(Arrays.toString(target)); } + } + } } return target; diff --git a/src/datastructure/sort/StraightSelectSort.java b/src/datastructure/sort/StraightSelectSort.java index 0f51f04..14da64d 100644 --- a/src/datastructure/sort/StraightSelectSort.java +++ b/src/datastructure/sort/StraightSelectSort.java @@ -1,6 +1,8 @@ package datastructure.sort; -/** +import java.util.Arrays; + +/** * Title: ѡеֱѡ * Description: ÿҵеǰΧеСֵ÷ΧеĵһֽͬʱСΧ * ʱ临ӶȣO(n^2)ƽO(n^2)O(n^2) @@ -28,8 +30,10 @@ public static int[] selectSort(int[] target){ target[min_index] = target[i]; target[i] = min; } + System.out.println(Arrays.toString(target)); } } + return target; } } diff --git a/src/datastructure/test/LinkedListTest.java b/src/datastructure/test/LinkedListTest.java index a4b8a02..4029a53 100644 --- a/src/datastructure/test/LinkedListTest.java +++ b/src/datastructure/test/LinkedListTest.java @@ -1,6 +1,8 @@ package datastructure.test; +import datastructure.list.LinkedList; + import java.util.List; public class LinkedListTest { @@ -21,7 +23,7 @@ public static void main(String[] args) throws Exception { System.out.println("~~"+8%6); - /*LinkedList list1 = new LinkedList(); + LinkedList list1 = new LinkedList(); list1.add("A"); list1.add("B"); list1.add("C"); @@ -87,7 +89,7 @@ public static void main(String[] args) throws Exception { list1.deleteNodeWithoutHead(list1.getEndK(2)); System.out.println("ӡɾضڵ"); list1.print(); -*/ + } diff --git a/src/datastructure/test/SortTest.java b/src/datastructure/test/SortTest.java index 3539248..f642dc9 100644 --- a/src/datastructure/test/SortTest.java +++ b/src/datastructure/test/SortTest.java @@ -13,7 +13,7 @@ public static void main(String[] args) { int[] target1 = { 38, 65, 97, 76, 13, 27, 49 }; System.out.println("ֱѡ "); System.out.println("ԭ " + Arrays.toString(target1)); - StraightSelectSort.selectSort(target1); + // StraightSelectSort.selectSort(target1); System.out.println(Arrays.toString(target1)); System.out.println("\n----------------------\n"); @@ -27,13 +27,13 @@ public static void main(String[] args) { System.out.println("ð "); int[] target3 = { 1, 2, 3, 4, 5, 8, 7, 6 }; System.out.println("ԭ " + Arrays.toString(target3)); - BubbleSort.bubbleSort(target3); + //BubbleSort.bubbleSort(target3); System.out.println("\n----------------------\n"); System.out.println("Żð "); int[] target4 = { 1, 2, 3, 4, 5, 8, 7, 6 }; System.out.println("ԭ " + Arrays.toString(target4)); - BubbleSort.optimizeBubbleSort(target4); + //BubbleSort.optimizeBubbleSort(target4); System.out.println("\n----------------------\n"); System.out.println("鲢 "); diff --git a/src/dome/HttpDownloaderPDF.java b/src/dome/HttpDownloaderPDF.java new file mode 100644 index 0000000..75348df --- /dev/null +++ b/src/dome/HttpDownloaderPDF.java @@ -0,0 +1,139 @@ +package dome; + +import javax.net.ssl.HttpsURLConnection; +import java.io.DataInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +public class HttpDownloaderPDF { + //pdf文件请求地址 + String remoteFileUrl = ""; + //本地存放pdf文件路径 + String localFilePath = ""; + + public static void httpDownloader(String remoteFileUrl, String localFilePath) { + try { + URL url = new URL(remoteFileUrl); + // 5000 自定义连接超时毫秒数 + HttpURLConnection httpURLConnection = (HttpsURLConnection) url.openConnection(); + httpURLConnection.setConnectTimeout(5 * 1000); + httpURLConnection.connect(); // 连接 + System.out.println("connect URL success!"); + + int fileLenght = httpURLConnection.getContentLength(); + System.out.println("file size:" + (fileLenght / 1024.0) + " KB"); + + System.out.println("start the download..."); + try (DataInputStream dis = new DataInputStream( + httpURLConnection.getInputStream()); + FileOutputStream fos = new FileOutputStream(localFilePath)) { + // 根据实际情况自定义 buf 大小 + byte[] buf = new byte[10240]; + for (int readSize; (readSize = dis.read(buf)) > 0;) { + fos.write(buf, 0, readSize); + } + System.out.println("download is complete!"); + } catch (IOException ex) { + System.out.println("download is error"); + } + + httpURLConnection.disconnect(); + } catch (IOException ex) { + ex.printStackTrace(); + System.out.println("URL Does not exist or connection timeout!"); + } + } + + + + public static void main(String[] args) { + + Map ll = new HashMap<>(); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17106329&app-key=okash-pay" , "17106329"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17580816&app-key=okash-pay" ,"17580816"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17606130&app-key=okash-pay" ,"17606130"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17607006&app-key=okash-pay" ,"17607006"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17607605&app-key=okash-pay" ,"17607605"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17608433&app-key=okash-pay" ,"17608433"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17608521&app-key=okash-pay" ,"17608521"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17608636&app-key=okash-pay" ,"17608636"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17609528&app-key=okash-pay" ,"17609528"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17609780&app-key=okash-pay" ,"17609780"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17612348&app-key=okash-pay" ,"17612348"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17612426&app-key=okash-pay" ,"17612426"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17612432&app-key=okash-pay" ,"17612432"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17614042&app-key=okash-pay" ,"17614042"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17614113&app-key=okash-pay" ,"17614113"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17615071&app-key=okash-pay" ,"17615071"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17616291&app-key=okash-pay" ,"17616291"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17616854&app-key=okash-pay" ,"17616854"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17617528&app-key=okash-pay" ,"17617528"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17618468&app-key=okash-pay" ,"17618468"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17618577&app-key=okash-pay" ,"17618577"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17620145&app-key=okash-pay" ,"17620145"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17620337&app-key=okash-pay" ,"17620337"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17622678&app-key=okash-pay" ,"17622678"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17622828&app-key=okash-pay" ,"17622828"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17622840&app-key=okash-pay" ,"17622840"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17623721&app-key=okash-pay" ,"17623721"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17624146&app-key=okash-pay" ,"17624146"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17624286&app-key=okash-pay" ,"17624286"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17624593&app-key=okash-pay" ,"17624593"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17625916&app-key=okash-pay" ,"17625916"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17626705&app-key=okash-pay" ,"17626705"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17628372&app-key=okash-pay" ,"17628372"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17628571&app-key=okash-pay" ,"17628571"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17629042&app-key=okash-pay" ,"17629042"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17629083&app-key=okash-pay" ,"17629083"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17629700&app-key=okash-pay" ,"17629700"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17630016&app-key=okash-pay" ,"17630016"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17630727&app-key=okash-pay" ,"17630727"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17634015&app-key=okash-pay" ,"17634015"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17634238&app-key=okash-pay" ,"17634238"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17634439&app-key=okash-pay" ,"17634439"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17634809&app-key=okash-pay" ,"17634809"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17636028&app-key=okash-pay" ,"17636028"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17637780&app-key=okash-pay" ,"17637780"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17638147&app-key=okash-pay" ,"17638147"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17638389&app-key=okash-pay" ,"17638389"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17638394&app-key=okash-pay" ,"17638394"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17638826&app-key=okash-pay" ,"17638826"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17643948&app-key=okash-pay" ,"17643948"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17644474&app-key=okash-pay" ,"17644474"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17644779&app-key=okash-pay" ,"17644779"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17646685&app-key=okash-pay" ,"17646685"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17646753&app-key=okash-pay" ,"17646753"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17648157&app-key=okash-pay" ,"17648157"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17648384&app-key=okash-pay" ,"17648384"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17649634&app-key=okash-pay" ,"17649634"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17650766&app-key=okash-pay" ,"17650766"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17651091&app-key=okash-pay" ,"17651091"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17652999&app-key=okash-pay" ,"17652999"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17654017&app-key=okash-pay" ,"17654017"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17654182&app-key=okash-pay" ,"17654182"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17654795&app-key=okash-pay" ,"17654795"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17654884&app-key=okash-pay" ,"17654884"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17655039&app-key=okash-pay" ,"17655039"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17655611&app-key=okash-pay" ,"17655611"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17656431&app-key=okash-pay" ,"17656431"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17656475&app-key=okash-pay" ,"17656475"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17658839&app-key=okash-pay" ,"17658839"); + + for (Map.Entry entry : ll.entrySet()) { + + httpDownloader(entry.getKey(), "F:/doc/okash_contract/"+entry.getValue()+".pdf"); + + } + + + } + + + + +} + From fbfdb7f93c3ec3c07418d680e3f73ec662770a3f Mon Sep 17 00:00:00 2001 From: liupeng Date: Mon, 24 May 2021 14:45:12 +0800 Subject: [PATCH 4/6] T1 add package dome --- src/basic/clone/Person.java | 68 ++++++++++++++++++++++++++ src/basic/clone/PersonApp.java | 24 ++++++++++ src/basic/io/SocketServer.java | 2 + src/dome/HttpDownloaderPDF.java | 84 ++++++--------------------------- 4 files changed, 108 insertions(+), 70 deletions(-) create mode 100644 src/basic/clone/Person.java create mode 100644 src/basic/clone/PersonApp.java diff --git a/src/basic/clone/Person.java b/src/basic/clone/Person.java new file mode 100644 index 0000000..3e58aa6 --- /dev/null +++ b/src/basic/clone/Person.java @@ -0,0 +1,68 @@ +package basic.clone; + +public class Person implements Cloneable{ + + public Person(String name, int age, String email, String desc) { + this.name = name; + this.age = age; + this.email = email; + this.desc = desc; + } + + // 姓名 + private String name; + // 年龄 + private int age; + // 邮件 + private String email; + // 描述 + private String desc; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } + + @Override + public String toString() { + return "Person{" + + "name='" + name + '\'' + + ", age=" + age + + ", email='" + email + '\'' + + ", desc='" + desc + '\'' + + '}'; + } + + @Override + protected Object clone() throws CloneNotSupportedException { + return super.clone(); + } +} + diff --git a/src/basic/clone/PersonApp.java b/src/basic/clone/PersonApp.java new file mode 100644 index 0000000..4d89efb --- /dev/null +++ b/src/basic/clone/PersonApp.java @@ -0,0 +1,24 @@ +package basic.clone; + +import java.util.concurrent.ConcurrentHashMap; + +public class PersonApp { + + public static void main(String[] args) throws CloneNotSupportedException { + // 初始化一个对象 + Person person = new Person("liup",20,"123456@qq.com","I'm liupeng"); + // 复制对象 + Person person1 = person; + // 改变 person1 的属性值 + person1.setName("lp"); + System.out.println("person:"+person); + System.out.println("person1:"+person1); + + Person person2 = (Person)person.clone(); + person2.setName("clone object"); + System.out.println("person:"+person); + System.out.println("person2:"+person2); + ConcurrentHashMap map = new ConcurrentHashMap(); + + } +} diff --git a/src/basic/io/SocketServer.java b/src/basic/io/SocketServer.java index c3a3449..278df09 100644 --- a/src/basic/io/SocketServer.java +++ b/src/basic/io/SocketServer.java @@ -23,3 +23,5 @@ public static void main(String[] args) throws IOException { } } + + diff --git a/src/dome/HttpDownloaderPDF.java b/src/dome/HttpDownloaderPDF.java index 75348df..78aab2c 100644 --- a/src/dome/HttpDownloaderPDF.java +++ b/src/dome/HttpDownloaderPDF.java @@ -53,76 +53,20 @@ public static void httpDownloader(String remoteFileUrl, String localFilePath) { public static void main(String[] args) { Map ll = new HashMap<>(); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17106329&app-key=okash-pay" , "17106329"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17580816&app-key=okash-pay" ,"17580816"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17606130&app-key=okash-pay" ,"17606130"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17607006&app-key=okash-pay" ,"17607006"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17607605&app-key=okash-pay" ,"17607605"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17608433&app-key=okash-pay" ,"17608433"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17608521&app-key=okash-pay" ,"17608521"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17608636&app-key=okash-pay" ,"17608636"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17609528&app-key=okash-pay" ,"17609528"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17609780&app-key=okash-pay" ,"17609780"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17612348&app-key=okash-pay" ,"17612348"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17612426&app-key=okash-pay" ,"17612426"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17612432&app-key=okash-pay" ,"17612432"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17614042&app-key=okash-pay" ,"17614042"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17614113&app-key=okash-pay" ,"17614113"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17615071&app-key=okash-pay" ,"17615071"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17616291&app-key=okash-pay" ,"17616291"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17616854&app-key=okash-pay" ,"17616854"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17617528&app-key=okash-pay" ,"17617528"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17618468&app-key=okash-pay" ,"17618468"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17618577&app-key=okash-pay" ,"17618577"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17620145&app-key=okash-pay" ,"17620145"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17620337&app-key=okash-pay" ,"17620337"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17622678&app-key=okash-pay" ,"17622678"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17622828&app-key=okash-pay" ,"17622828"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17622840&app-key=okash-pay" ,"17622840"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17623721&app-key=okash-pay" ,"17623721"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17624146&app-key=okash-pay" ,"17624146"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17624286&app-key=okash-pay" ,"17624286"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17624593&app-key=okash-pay" ,"17624593"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17625916&app-key=okash-pay" ,"17625916"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17626705&app-key=okash-pay" ,"17626705"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17628372&app-key=okash-pay" ,"17628372"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17628571&app-key=okash-pay" ,"17628571"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17629042&app-key=okash-pay" ,"17629042"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17629083&app-key=okash-pay" ,"17629083"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17629700&app-key=okash-pay" ,"17629700"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17630016&app-key=okash-pay" ,"17630016"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17630727&app-key=okash-pay" ,"17630727"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17634015&app-key=okash-pay" ,"17634015"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17634238&app-key=okash-pay" ,"17634238"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17634439&app-key=okash-pay" ,"17634439"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17634809&app-key=okash-pay" ,"17634809"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17636028&app-key=okash-pay" ,"17636028"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17637780&app-key=okash-pay" ,"17637780"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17638147&app-key=okash-pay" ,"17638147"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17638389&app-key=okash-pay" ,"17638389"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17638394&app-key=okash-pay" ,"17638394"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17638826&app-key=okash-pay" ,"17638826"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17643948&app-key=okash-pay" ,"17643948"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17644474&app-key=okash-pay" ,"17644474"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17644779&app-key=okash-pay" ,"17644779"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17646685&app-key=okash-pay" ,"17646685"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17646753&app-key=okash-pay" ,"17646753"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17648157&app-key=okash-pay" ,"17648157"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17648384&app-key=okash-pay" ,"17648384"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17649634&app-key=okash-pay" ,"17649634"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17650766&app-key=okash-pay" ,"17650766"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17651091&app-key=okash-pay" ,"17651091"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17652999&app-key=okash-pay" ,"17652999"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17654017&app-key=okash-pay" ,"17654017"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17654182&app-key=okash-pay" ,"17654182"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17654795&app-key=okash-pay" ,"17654795"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17654884&app-key=okash-pay" ,"17654884"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17655039&app-key=okash-pay" ,"17655039"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17655611&app-key=okash-pay" ,"17655611"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17656431&app-key=okash-pay" ,"17656431"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17656475&app-key=okash-pay" ,"17656475"); - ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17658839&app-key=okash-pay" ,"17658839"); - + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=16944303&app-key=okash-pay" , "16944303"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=16990549&app-key=okash-pay" , "16990549"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=16695915&app-key=okash-pay" , "16695915"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=16834328&app-key=okash-pay" , "16834328"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=16786733&app-key=okash-pay" , "16786733"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=16856982&app-key=okash-pay" , "16856982"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17035274&app-key=okash-pay" , "17035274"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=16677024&app-key=okash-pay" , "16677024"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17158830&app-key=okash-pay" , "17158830"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=16843124&app-key=okash-pay" , "16843124"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=16747954&app-key=okash-pay" , "16747954"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=17145040&app-key=okash-pay" , "17145040"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=16752224&app-key=okash-pay" , "16752224"); + ll.put("https://service.ke.o-kash.com/api/user/contract/v1/info?applyId=16792747&app-key=okash-pay" , "16792747"); for (Map.Entry entry : ll.entrySet()) { httpDownloader(entry.getKey(), "F:/doc/okash_contract/"+entry.getValue()+".pdf"); From c3ed1bd30765f5193471a6bd04285b246860dee9 Mon Sep 17 00:00:00 2001 From: liupeng Date: Mon, 17 Apr 2023 11:03:30 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/designpattern/responsibilitychain/Handler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/designpattern/responsibilitychain/Handler.java b/src/designpattern/responsibilitychain/Handler.java index 4fd360f..ddd17b9 100644 --- a/src/designpattern/responsibilitychain/Handler.java +++ b/src/designpattern/responsibilitychain/Handler.java @@ -17,7 +17,7 @@ public void setSuccessor(Handler successor) { public abstract void handleRequest(int request); } -// 具体处理者类,处理它所负责的请求,可访问它的后继者,如果可处理该请求,则处理,否则转给它的后继者处理 +// 具体处理者类,处理它所负责的请求,可访问它的后继者,如果可处理该请求,则处理,否则转给它的后继者处理 test class ConcreteHandlerA extends Handler { @Override From b23694d79cc1cdb049965bfedb2e031c6fcea254 Mon Sep 17 00:00:00 2001 From: liupeng Date: Tue, 25 Apr 2023 19:38:44 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/designpattern/responsibilitychain/Client.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/designpattern/responsibilitychain/Client.java b/src/designpattern/responsibilitychain/Client.java index 5118c2c..bb2853a 100644 --- a/src/designpattern/responsibilitychain/Client.java +++ b/src/designpattern/responsibilitychain/Client.java @@ -19,7 +19,7 @@ public static void main(String[] args) { int[] requests = { 2, 14, 5, 6, 8, 23, 12, 21 }; for (int i : requests) { - handlerA.handleRequest(i); + handlerA.handleRequest(i); } }