算法提交

main
xiaotiancai893661742 3 years ago
parent 4305177015
commit fca8239636

@ -21,6 +21,16 @@
</build> </build>
<dependencies> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies> </dependencies>

@ -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 class Hello {
public static void main(String[] args) { 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); System.out.println(12);
} }
} }

@ -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 {
}

@ -0,0 +1,8 @@
package com;
import org.springframework.context.annotation.ImportResource;
//@ImportResource(locations = {"bean.xml"})
public class B {
}

@ -1,5 +1,8 @@
package zuolaos.jichuban; package zuolaos.jichuban;
import java.util.ArrayList;
import java.util.List;
public class Code03_ { 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 { private static class DoubleNode {
int value; int value;
DoubleNode next; DoubleNode next;
DoubleNode last; DoubleNode last;
public DoubleNode(int value) { public DoubleNode(int value) {
this.value = value; this.value = value;
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
NewNode newNode = new NewNode(1);
Node node = new Node(1); newNode.left = new NewNode(2);
node.next = new Node(2); newNode.right = new NewNode(3);
node.next.next = new Node(3); newNode.left.left = new NewNode(2);
Node newNode = reverNode(node); newNode.left.right = new NewNode(2);
System.out.println(newNode.value); 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<List<Integer>> 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); public static List<List<Integer>> cengji(NewNode node) {
DoubleNode doubleNode2 = new DoubleNode(2); List<List<Integer>> ans = new ArrayList<>();
DoubleNode doubleNode3 = new DoubleNode(3); List<NewNode> nodes = new ArrayList<>();
doubleNode.next = doubleNode2; nodes.add(node);
doubleNode.next.next = doubleNode3; process(nodes, ans);
doubleNode3.last=doubleNode2; return ans;
doubleNode3.last.last=doubleNode; }
DoubleNode newNode2 = reverDoubleNode(doubleNode);
System.out.println(newNode2.value);
private static void process(List<NewNode> nodes, List<List<Integer>> ans) {
if (nodes == null || nodes.size() == 0) {
return;
}
List<Integer> values = new ArrayList<>();
List<NewNode> 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) { private static DoubleNode reverDoubleNode(DoubleNode head) {
DoubleNode pre=null; DoubleNode pre = null;
DoubleNode next=null; DoubleNode next = null;
while (head!=null){ while (head != null) {
next=head.next; next = head.next;
head.next=pre; head.next = pre;
head.last=next; head.last = next;
pre=head; pre = head;
head=next; head = next;
} }
return pre; 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) { private static Node reverNode(Node head) {
Node pre=null; Node pre = null;
Node next=null; Node next = null;
while (head!=null){ while (head != null) {
next=head.next; next = head.next;
head.next=pre; head.next = pre;
pre=head; pre = head;
head=next; head = next;
} }
return pre; 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;
}
} }

@ -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);
}
}
}
}

@ -0,0 +1,20 @@
package zuolaos.jichuban;
import java.util.Stack;
import java.util.TreeMap;
public class Code11_ {
}
class TowStacktToQueue{
Stack<Integer> push=new Stack<>();
Stack<Integer> poll=new Stack<>();
public void offer(){
TreeMap<Object, Object> objectObjectTreeMap = new TreeMap<>();
}
}

@ -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));
}
}

@ -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<Integer> 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];
}
}

@ -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<Integer, Node> nextNodes;
public Node(){
pass=0;
end=0;
nextNodes=new HashMap<Integer, Node>();
}
}
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;
}
}

@ -0,0 +1,4 @@
package zuolaos.jichuban;
public class Code15_ {
}
Loading…
Cancel
Save