diff --git a/pom.xml b/pom.xml index 5f0c536..6b6dad7 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,16 @@ + + org.springframework.boot + spring-boot-starter-web + 1.5.10.RELEASE + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 1.3.0 + diff --git a/src/main/java/Hello.java b/src/main/java/Hello.java index 40598ca..57f9fea 100644 --- a/src/main/java/Hello.java +++ b/src/main/java/Hello.java @@ -1,5 +1,28 @@ +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.context.support.ClassPathXmlApplicationContext; + public class Hello { + public static void main(String[] args) { + ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); + String username = System.getenv("USERNAME"); + System.out.println(username); + Hello hello = ac.getBean(Hello.class); + System.out.println(hello); + String a = ac.getEnvironment().getProperty("a"); + System.out.println(a); + } +} + + +class MyBeanFactoryPostProcess implements BeanFactoryPostProcessor{ + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + BeanDefinition hello = beanFactory.getBeanDefinition("Hello"); +// hello.set System.out.println(12); } } diff --git a/src/main/java/com/A.java b/src/main/java/com/A.java new file mode 100644 index 0000000..aae8e0d --- /dev/null +++ b/src/main/java/com/A.java @@ -0,0 +1,14 @@ +package com; + + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.ImportResource; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +@Component +@PropertySource(value = {"a.properties"}) +public class A { + +} diff --git a/src/main/java/com/B.java b/src/main/java/com/B.java new file mode 100644 index 0000000..2397fb6 --- /dev/null +++ b/src/main/java/com/B.java @@ -0,0 +1,8 @@ +package com; + + +import org.springframework.context.annotation.ImportResource; + +//@ImportResource(locations = {"bean.xml"}) +public class B { +} diff --git a/src/main/resources/a.properties b/src/main/resources/a.properties new file mode 100644 index 0000000..e6a001e --- /dev/null +++ b/src/main/resources/a.properties @@ -0,0 +1 @@ +a=12 \ No newline at end of file diff --git a/src/test/java/zuolaos/jichuban/Code03_链表反转.java b/src/test/java/zuolaos/jichuban/Code03_链表反转.java index dc8c3bd..ed1853a 100644 --- a/src/test/java/zuolaos/jichuban/Code03_链表反转.java +++ b/src/test/java/zuolaos/jichuban/Code03_链表反转.java @@ -1,5 +1,8 @@ package zuolaos.jichuban; +import java.util.ArrayList; +import java.util.List; + public class Code03_链表反转 { @@ -12,58 +15,149 @@ public class Code03_链表反转 { } } + private static class NewNode { + int value; + NewNode left; + NewNode right; + + public NewNode(int value) { + this.value = value; + } + } + private static class DoubleNode { int value; DoubleNode next; DoubleNode last; + public DoubleNode(int value) { this.value = value; } } public static void main(String[] args) { - - Node node = new Node(1); - node.next = new Node(2); - node.next.next = new Node(3); - Node newNode = reverNode(node); - System.out.println(newNode.value); + NewNode newNode = new NewNode(1); + newNode.left = new NewNode(2); + newNode.right = new NewNode(3); + newNode.left.left = new NewNode(2); + newNode.left.right = new NewNode(2); + newNode.right.left = new NewNode(2); + newNode.right.right = new NewNode(2); + newNode.right.right = new NewNode(2); + newNode.right.right.right = new NewNode(2); + List> cengji = cengji(newNode); + System.out.println(cengji); + Integer integer = new Integer(1); + Integer integer2 = new Integer(1); + System.out.println(integer.hashCode()==integer2.hashCode()); + } - DoubleNode doubleNode = new DoubleNode(1); - DoubleNode doubleNode2 = new DoubleNode(2); - DoubleNode doubleNode3 = new DoubleNode(3); - doubleNode.next = doubleNode2; - doubleNode.next.next = doubleNode3; - doubleNode3.last=doubleNode2; - doubleNode3.last.last=doubleNode; - DoubleNode newNode2 = reverDoubleNode(doubleNode); - System.out.println(newNode2.value); + public static List> cengji(NewNode node) { + List> ans = new ArrayList<>(); + List nodes = new ArrayList<>(); + nodes.add(node); + process(nodes, ans); + return ans; + } + private static void process(List nodes, List> ans) { + if (nodes == null || nodes.size() == 0) { + return; + } + List values = new ArrayList<>(); + List newNodes = new ArrayList<>(); + for (NewNode node : nodes) { + values.add(node.value); + if (node.left != null) { + newNodes.add(node.left); + } + if (node.right != null) { + newNodes.add(node.right); + } + } + ans.add(values); + process(newNodes, ans); } + + //双向链表迭代 private static DoubleNode reverDoubleNode(DoubleNode head) { - DoubleNode pre=null; - DoubleNode next=null; - while (head!=null){ - next=head.next; - head.next=pre; - head.last=next; - pre=head; - head=next; + DoubleNode pre = null; + DoubleNode next = null; + while (head != null) { + next = head.next; + head.next = pre; + head.last = next; + pre = head; + head = next; } return pre; } + //双向链表递归 + private static DoubleNode reverDoubleNode2(DoubleNode head) { + if (head == null || head.next == null) { + head.last = null; + return head; + } + DoubleNode newhead = reverDoubleNode2(head.next); + head.next.next = head; + head.last = head.next; + return newhead; + + } + + //单向链表迭代 private static Node reverNode(Node head) { - Node pre=null; - Node next=null; - while (head!=null){ - next=head.next; - head.next=pre; - pre=head; - head=next; + Node pre = null; + Node next = null; + while (head != null) { + next = head.next; + head.next = pre; + pre = head; + head = next; } return pre; } + + //单向链表递归 + private static Node reverNode2(Node node) { + if (node == null || node.next == null) { + return node; + } + Node head = reverNode2(node.next); + node.next.next = node; + node.next = null; + return head; + } + + //删除对应值的链表 + private static Node removeNodeOfValue(Node node, int num) { + //先确定头 + while (node != null) { + if (node.value != num) { + break; + } + node = node.next; + } + + Node pre = node; + Node cur = node; + + while (cur != null) { + if (cur.value == num) { + pre.next = cur.next; + } else { + pre = cur; + } + cur = cur.next; + } + + return node; + + + } + + } diff --git a/src/test/java/zuolaos/jichuban/Code04_链表实现队列.java b/src/test/java/zuolaos/jichuban/Code04_链表实现队列.java index 8b8123c..2a2edf7 100644 --- a/src/test/java/zuolaos/jichuban/Code04_链表实现队列.java +++ b/src/test/java/zuolaos/jichuban/Code04_链表实现队列.java @@ -1,6 +1,6 @@ package zuolaos.jichuban; -public class Code04_链表实现队列 { +public class Code04_链表实现队列 { private static class Node { V value; diff --git a/src/test/java/zuolaos/jichuban/Code11_异或运算.java b/src/test/java/zuolaos/jichuban/Code11_异或运算.java new file mode 100644 index 0000000..44494dd --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Code11_异或运算.java @@ -0,0 +1,47 @@ +package zuolaos.jichuban; + +public class Code11_异或运算 { + + + //某个数出现奇数次 其他出现偶数次 + public static void m1(int[] arr) { + int ans = 0; + for (int i = 0; i < arr.length; i++) { + ans ^= arr[i]; + } + System.out.println(ans); + } + + //某两个数出现奇数次 其他出现偶数次 + public static void m3(int[] arr) { + int ans = 0; + for (int num : arr) { + ans ^= num; + } + int rightOne = ans & (-ans); + int ans2 = 0; + for (int num : arr) { + if ((num & rightOne) != 0) { + ans2 ^= num; + } + } + System.out.println(ans2); + System.out.println(ans ^ ans2); + } + + //某个数出现了K次 其他数出现了M次 + public static void m2(int[] arr, int k, int m) { + int[] bits = new int[32]; + for (int num : arr) { + for (int i = 0; i < 32; i++) { + bits[i] += (num >> i) & 1; + } + } + int ans = 0; + for (int i = 0; i < bits.length; i++) { + if (bits[i] % m != 0) { + ans |= (1 << i); + } + } + } +} diff --git a/src/test/java/zuolaos/jichuban/Code11_栈和队列.java b/src/test/java/zuolaos/jichuban/Code11_栈和队列.java new file mode 100644 index 0000000..3e7409f --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Code11_栈和队列.java @@ -0,0 +1,20 @@ +package zuolaos.jichuban; + +import java.util.Stack; +import java.util.TreeMap; + +public class Code11_栈和队列 { + + +} + +class TowStacktToQueue{ + + + Stack push=new Stack<>(); + Stack poll=new Stack<>(); + + public void offer(){ + TreeMap objectObjectTreeMap = new TreeMap<>(); + } +} diff --git a/src/test/java/zuolaos/jichuban/Code12_归并排序进阶.java b/src/test/java/zuolaos/jichuban/Code12_归并排序进阶.java new file mode 100644 index 0000000..6d66eb8 --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Code12_归并排序进阶.java @@ -0,0 +1,78 @@ +package zuolaos.jichuban; + +public class Code12_归并排序进阶 { + + +} + + +//找到子数组累加和 在lower upper范围内 +class FindZiShuZuSum { + public static int start(int[] arr, int lower, int upper) { + int[] help = new int[arr.length]; + help[0] = arr[0]; + for (int i = 1; i < arr.length; i++) { + help[i] = help[i - 1] + arr[i]; + } + return process(help, 0, help.length - 1, lower, upper); + } + + private static int process(int[] help, int L, int R, int lower, int upper) { + if (L == R) { + if (help[L] >= lower && help[L] <= upper) { + return 1; + } else { + return 0; + } + } + int M = L + ((R - L) >> 1); + int left = process(help, L, M, lower, upper); + int right = process(help, M + 1, R, lower, upper); + int merge = merge(help, L, M, R, lower, upper); + return left + right + merge; + } + + private static int merge(int[] help, int L, int M, int R, int lower, int upper) { + int windowL = L; + int windowR = L; + int ans = 0; + for (int i = M + 1; i <= R; i++) { + int max = help[i] - lower; + int min = help[i] - upper; + //找到第一个满足条件的左窗口 + while (windowL <= M && help[windowL] < min) { + windowL++; + } + //找到第一个不满足条件的右窗口 + while (windowR <= M && help[windowR] <= max) { + windowR++; + } + ans += windowR - windowL; + } + int[] temp = new int[R - L + 1]; + int indexL = L; + int indexR = M+1; + int indexCur = 0; + while (indexL <= M && indexR <= R) { + temp[indexCur++] = help[indexL] <= help[indexR] ? help[indexL++] : help[indexR++]; + } + while (indexL <= M) { + temp[indexCur++] = help[indexL++]; + } + while (indexR <= R) { + temp[indexCur++] = help[indexR++]; + } + for (int i = 0; i < temp.length; i++) { + help[i + L] = temp[i]; + } + return ans; + } + + public static void main(String[] args) { + int[] arr={-2,2,1}; + int lower=0; + int upper=2; + System.out.println(start(arr,lower,upper)); + } +} + diff --git a/src/test/java/zuolaos/jichuban/Code13_堆排序.java b/src/test/java/zuolaos/jichuban/Code13_堆排序.java new file mode 100644 index 0000000..1494a02 --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Code13_堆排序.java @@ -0,0 +1,207 @@ +package zuolaos.jichuban; + +import java.util.PriorityQueue; + +public class Code13_堆排序 { + public static void main(String[] args) { + MyMinHeap myMaxHeap = new MyMinHeap(10); + myMaxHeap.push(5); + myMaxHeap.push(1); + myMaxHeap.push(5); + myMaxHeap.push(9); + myMaxHeap.push(105); + System.out.println(myMaxHeap.pop()); + System.out.println(myMaxHeap.pop()); + System.out.println(myMaxHeap.pop()); + System.out.println(myMaxHeap.pop()); + System.out.println(myMaxHeap.pop()); + + int arr[] = {8, 1, 2, 3, 4, 5, 6, 70}; + MyMinHeap myMinHeap = new MyMinHeap(arr); + System.out.println(myMinHeap.pop()); + System.out.println(myMinHeap.pop()); + System.out.println(myMinHeap.pop()); + System.out.println(myMinHeap.pop()); + System.out.println(myMinHeap.pop()); + System.out.println(myMinHeap.pop()); + System.out.println(myMinHeap.pop()); + System.out.println(myMinHeap.pop()); + } +} + + +class MaxMoveK { + public void process(int[] arr, int k) { + //小顶堆 + PriorityQueue heap = new PriorityQueue<>(); + int index = 0; + for (; index < Math.min(k, arr.length - 1); index++) { + heap.add(arr[index]); + } + int arrIndex=0; + for (; index < arr.length; index++) { + heap.add(arr[index]); + arr[arrIndex++]=heap.poll(); + } + + while (!heap.isEmpty()){ + arr[arrIndex++]=heap.poll(); + } + } + + +} + +class MyMinHeap { + private int[] heap; + private final int limit; + private int heapSize; + + public MyMinHeap(int limit) { + this.limit = limit; + heap = new int[limit]; + heapSize = 0; + } + + public MyMinHeap(int[] heap) { + this.heap = heap; + this.limit = heap.length; + this.heapSize = heap.length; + for (int i = heapSize - 1; i >= 0; i--) { + helpify(heap, i); + } + } + + public boolean isEmpty() { + return this.heapSize == 0; + } + + public boolean isFull() { + return this.heapSize == this.limit; + } + + public void push(int value) { + if (isFull()) { + throw new RuntimeException("heap is full"); + } + heap[heapSize] = value; + helpinsert(heap, heapSize++); + } + + public int pop() { + int ans = heap[0]; + swap(heap, 0, --heapSize); + helpify(heap, 0); + return ans; + } + + //新加起来的元素在index的位置上 + public void helpinsert(int[] arr, int index) { + while (arr[(index - 1) / 2] > arr[index]) { + //交换 + swap(arr, index, (index - 1) / 2); + index = (index - 1) / 2; + } + } + + //在index位置不断的下沉 + public void helpify(int[] arr, int index) { + int left = 2 * index + 1; + while (left < heapSize) { + int least = left + 1 < heapSize && arr[left + 1] < arr[left] ? left + 1 : left; + least = arr[index] > arr[least] ? least : index; + if (least == index) { + break; + } + swap(arr, index, least); + index = least; + left = 2 * index + 1; + } + } + + public void swap(int[] arr, int i, int j) { + if (i == j) { + return; + } + arr[i] = arr[i] ^ arr[j]; + arr[j] = arr[i] ^ arr[j]; + arr[i] = arr[i] ^ arr[j]; + } +} + + +class MyMaxHeap { + private int[] heap; + private final int limit; + private int heapSize; + + public MyMaxHeap(int limit) { + this.limit = limit; + heap = new int[limit]; + heapSize = 0; + } + + public boolean isEmpty() { + return this.heapSize == 0; + } + + public boolean isFull() { + return this.heapSize == this.limit; + } + + public void push(int value) { + if (isFull()) { + throw new RuntimeException("heap is full"); + } + heap[heapSize] = value; + helpinsert(heap, heapSize++); + } + + public int pop() { + int ans = heap[0]; + swap(heap, 0, --heapSize); + helpify(heap, 0); + return ans; + } + + //新加起来的元素在index的位置上 + public void helpinsert(int[] arr, int index) { + while (arr[(index - 1) / 2] < arr[index]) { + //交换 + swap(arr, index, (index - 1) / 2); + index = (index - 1) / 2; + } + } + + //在index位置不断的下沉 + public void helpify(int[] arr, int index) { + int left = 2 * index + 1; + while (left < heapSize) { + int large = left + 1 < heapSize && arr[left + 1] > arr[left] ? left + 1 : left; + large = arr[index] < arr[large] ? large : index; + if (large == index) { + break; + } + swap(arr, index, large); + index = large; + left = 2 * index + 1; + } + } + + public void swap(int[] arr, int i, int j) { + if (i == j) { + return; + } + arr[i] = arr[i] ^ arr[j]; + arr[j] = arr[i] ^ arr[j]; + arr[i] = arr[i] ^ arr[j]; + } +} + + + + + + + + diff --git a/src/test/java/zuolaos/jichuban/Code14_前缀树.java b/src/test/java/zuolaos/jichuban/Code14_前缀树.java new file mode 100644 index 0000000..db19618 --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Code14_前缀树.java @@ -0,0 +1,104 @@ +package zuolaos.jichuban; + +import java.util.HashMap; +import java.util.Map; + +public class Code14_前缀树 { + + public static void main(String[] args) { + Trie trie = new Trie(); + + trie.insert("abc"); + trie.insert("abcd"); + trie.insert("ab"); + trie.insert("ac"); + trie.insert("ae"); + trie.insert("ae"); + trie.insert("ae"); + System.out.println(trie.search("ae")); + System.out.println(trie.search("abc")); + trie.delete("ae"); + System.out.println(trie.search("ae")); + } +} + + +class Trie { + private class Node { + int pass; + int end; + private Map nextNodes; + + public Node(){ + pass=0; + end=0; + nextNodes=new HashMap(); + } + } + + Node root; + + public Trie() { + root = new Node(); + + } + + public void insert(String word) { + if (word == null) { + return; + } + char[] chars = word.toCharArray(); + Node temp = root; + temp.pass++; + int index = 0; + for (int i = 0; i < chars.length; i++) { + index = chars[i]; + if (!temp.nextNodes.containsKey(index)) { + temp.nextNodes.put(index, new Node()); + } + temp = temp.nextNodes.get(index); + temp.pass++; + } + temp.end++; + } + + + public void delete(String word) { + if (search(word) == 0) { + return; + } + char[] chars = word.toCharArray(); + Node temp = root; + temp.pass--; + int index = 0; + for (int i = 0; i < chars.length; i++) { + index = chars[i]; + if(--temp.nextNodes.get(index).pass==0){ + temp.nextNodes.remove(index); + return; + } + temp=temp.nextNodes.get(index); + } + temp.end--; + } + + + public int search(String word) { + if (word == null) { + return 0; + } + char[] chars = word.toCharArray(); + Node temp = root; + int index = 0; + for (int i = 0; i < chars.length; i++) { + index = chars[i]; + if (!temp.nextNodes.containsKey(index)) { + return 0; + } + temp = temp.nextNodes.get(index); + } + return temp.end; + } + + +} diff --git a/src/test/java/zuolaos/jichuban/Code15_基数排序.java b/src/test/java/zuolaos/jichuban/Code15_基数排序.java new file mode 100644 index 0000000..6b2cd78 --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Code15_基数排序.java @@ -0,0 +1,4 @@ +package zuolaos.jichuban; + +public class Code15_基数排序 { +} diff --git a/target/classes/a.properties b/target/classes/a.properties new file mode 100644 index 0000000..e6a001e --- /dev/null +++ b/target/classes/a.properties @@ -0,0 +1 @@ +a=12 \ No newline at end of file