练习,整理

pull/3/head
Leo 5 years ago
parent 26e758f3f4
commit a1fa24b4a7

@ -1,9 +1,6 @@
package leo.class01;
package leo.class01_01;
import com.sun.org.apache.xpath.internal.functions.FuncFalse;
import leo.util.ArrayUtil;
import sun.jvm.hotspot.debugger.Page;
import sun.security.util.Length;
/**
* @author Leo

@ -1,11 +1,8 @@
package leo.class01;
package leo.class01_01;
import leo.util.ArrayUtil;
import javax.swing.plaf.TreeUI;
import java.util.Arrays;
/**
* @author Leo
* @ClassName BSExist

@ -1,4 +1,4 @@
package leo.class01;
package leo.class01_01;
import leo.util.ArrayUtil;

@ -1,4 +1,4 @@
package leo.class01;
package leo.class01_01;
import leo.util.ArrayUtil;

@ -1,4 +1,4 @@
package leo.class01;
package leo.class01_01;
import leo.util.ArrayUtil;

@ -1,4 +1,4 @@
package leo.class01;
package leo.class01_01;
import leo.util.ArrayUtil;

@ -1,8 +1,4 @@
package leo.class01;
import sun.applet.Main;
import java.nio.ByteOrder;
package leo.class01_02;
/**
* @author Leo

@ -1,4 +1,4 @@
package leo.class01;
package leo.class01_02;
import java.util.HashMap;
import java.util.HashSet;

@ -1,4 +1,4 @@
package leo.class02;
package leo.class02_03;
/**
* @author Leo

@ -1,4 +1,4 @@
package leo.class02;
package leo.class02_03;
import leo.util.ArrayUtil;

@ -1,6 +1,4 @@
package leo.class02;
import jdk.nashorn.internal.ir.IfNode;
package leo.class02_03;
import java.util.ArrayList;
import java.util.Arrays;

@ -1,6 +1,5 @@
package leo.class02;
package leo.class02_03;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@ -1,4 +1,4 @@
package leo.class02;
package leo.class02_03;
import java.util.Queue;

@ -1,4 +1,4 @@
package leo.class02;
package leo.class02_03;
import java.util.LinkedList;
import java.util.Queue;

@ -1,4 +1,4 @@
package leo.class02;
package leo.class02_03;
import java.util.LinkedList;
import java.util.Queue;

@ -1,4 +1,4 @@
package leo.class02;
package leo.class02_03;
import java.util.LinkedList;
import java.util.Queue;

@ -1,6 +1,4 @@
package leo.class02;
import java.util.function.IntPredicate;
package leo.class03_04;
/**
* @author Leo

@ -1,4 +1,4 @@
package leo.class02;
package leo.class03_04;
import leo.util.ArrayUtil;

@ -1,4 +1,4 @@
package leo.class02;
package leo.class03_04;
import leo.util.ArrayUtil;

@ -1,4 +1,4 @@
package leo.class02;
package leo.class03_04;
import leo.util.ArrayUtil;

@ -1,4 +1,4 @@
package leo.class03;
package leo.class03_05;
import leo.util.ArrayUtil;
@ -263,6 +263,7 @@ class CountOfRangeSum3{
}
class CountOfRangeSum4 {
public static int countRangeSum(int[] nums, int lower, int upper) {
@ -328,6 +329,70 @@ class CountOfRangeSum4 {
}
class CountOfRangeSum5 {
public static int countRangeSum(int[] num, int lower, int upper) {
if (num.length == 0 || num == null) {
return 0;
}
long[] sum = new long[num.length];
sum[0] = num[0];
for (int i = 1; i < num.length; i++) {
sum[i] = sum[i - 1] + num[i];
}
return process(sum, 0, sum.length - 1,lower,upper);
}
public static int process(long[] sum, int l, int r, int lower, int upper) {
if (l > r) {
return 0;
}
if (l == r) {
return sum[l] >= lower && sum[l] <= upper ? 1 : 0;
}
int m = l + ((r - l) >> 1);
return process(sum, l, m, lower, upper) + process(sum, m + 1, r, lower, upper) + merge(sum, l, m, r, lower, upper);
}
private static int merge(long[] sum, int l, int m, int r, int lower, int upper) {
int windowR = l;
int windowL = l;
int index = m + 1;
int res = 0;
while (index <= r) {
long max = sum[index] - lower;
long min = sum[index] - upper;
while (windowR <= r && sum[windowR] <= max) {
windowR++;
}
while (windowL < r && sum[windowL] < min) {
windowL++;
}
index++;
res += windowR - windowL;
}
int p1 = l;
int p2 = m+1;
long[] help = new long[r - l + 1];
int i = 0;
while (p1 <= m && p2 <= r) {
help[i++] = sum[p1] <= sum[p2] ? sum[p1++] : sum[p2++];
}
while (p1 <= m) {
help[i++] = sum[p1++];
}
while (p2 <= r) {
help[i++] = sum[p2++];
}
for (i = 0; i < help.length; i++) {
sum[l + i] = help[i];
}
return res;
}
}
class MainTest{
public static int countRangeSum(int[] nums, int lower, int upper) {

@ -1,12 +1,9 @@
package leo.class03;
package leo.class03_05;
import com.sun.org.apache.bcel.internal.generic.POP2;
import com.sun.xml.internal.bind.v2.model.core.ID;
import leo.util.ArrayUtil;
import java.util.Arrays;
import java.util.Stack;
import java.util.function.IntPredicate;
/**
* @author Leo
@ -363,6 +360,55 @@ class QuickSort3_3{
}
}
class QuickSort3_4{
public static void quickSort(int[] arr) {
if (arr.length < 2 || arr == null) {
return;
}
process(arr, 0, arr.length - 1);
}
public static void process(int[] arr, int l, int r) {
if (l >= r) {
return;
}
int[] equalArea = partition(arr, l, r);
process(arr, l, equalArea[0] - 1);
process(arr, equalArea[0] + 1, r);
}
private static int[] partition(int[] arr, int l, int r) {
int leftIndex = l - 1;
int rightIndex = r;
int index = l;
while (index < rightIndex) {
if (arr[index] == arr[r]) {
index++;
} else if (arr[index] > arr[r]) {
swap(arr, index, --rightIndex);
} else if (arr[index] < arr[r]) {
swap(arr, index++, ++leftIndex);
}
}
swap(arr, r, rightIndex);
return new int[]{leftIndex + 1, rightIndex};
}
public static void swap(int[] arr, int i, int j) {
if (i == j || arr[i] == arr[j]) {
return;
}
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
}
class QuickSortUnRecursive{
private static class Op {
@ -452,7 +498,7 @@ class TestMain {
for (int i = 0; i < testTimes; i++) {
int[] arr = ArrayUtil.randomArray(sizeMax, range);
int[] copyArray = ArrayUtil.copyArray(arr);
QuickSort3_3.quickSort(arr);
QuickSort3_4.quickSort(arr);
Arrays.sort(copyArray);
if (!ArrayUtil.isEqual(arr, copyArray)) {
ArrayUtil.printArr(arr);

@ -1,4 +1,4 @@
package leo.class03;
package leo.class04_06;
/**
* @author Leo

@ -1,8 +1,7 @@
package leo.class03;
package leo.class04_06;
import leo.util.ArrayUtil;
import java.lang.reflect.Array;
import java.util.Arrays;
/**

@ -1,4 +1,4 @@
package leo.class03;
package leo.class04_06;
import java.util.PriorityQueue;

@ -1,4 +1,4 @@
package leo.class03;
package leo.class04_06;
import leo.util.ArrayUtil;
@ -34,6 +34,7 @@ public class SortArrayDistanceLessK {
}
}
}
class SortArrayDistanceLessK1 {
@ -60,6 +61,29 @@ class SortArrayDistanceLessK1 {
}
class SortArrayDistanceLessK2{
public static void sortArrayDistanceLessK(int[] arr, int k) {
if (k == 0) {
return;
}
int index = 0;
PriorityQueue<Integer> queue = new PriorityQueue<>();
for (; index < Math.min(arr.length - 1, k - 1); index++) {
queue.add(arr[index]);
}
int i = 0;
for (; index < arr.length; index++, i++) {
queue.add(arr[index]);
arr[i] = queue.poll();
}
while (!queue.isEmpty()) {
arr[i++] = queue.poll();
}
}
}
class MainK {
@ -77,7 +101,7 @@ class MainK {
UpsetArray(arr, k);
int[] copyArray = ArrayUtil.copyArray(arr);
Arrays.sort(copyArray);
SortArrayDistanceLessK1.sortArrayDistanceLessK(arr,k);
SortArrayDistanceLessK2.sortArrayDistanceLessK(arr,k);
if (!ArrayUtil.isEqual(arr, copyArray)) {
ArrayUtil.printArr(arr);
ArrayUtil.printArr(copyArray);

@ -1,4 +1,4 @@
package leo.class04;
package leo.class04_07;
import java.util.Arrays;
import java.util.Comparator;
@ -66,6 +66,93 @@ public class CoverMax {
}
class CoverMax1 {
public static class Line {
private int start;
private int end;
public Line(int s, int e) {
this.start = s;
this.end = e;
}
}
public static class StartAscComparator implements Comparator<Line> {
@Override
public int compare(Line o1, Line o2) {
return o1.start - o2.start;
}
}
public static int maxCover(int[][] arr) {
if (arr.length == 0 || arr == null) {
return 0;
}
Line[] lines = new Line[arr.length];
for (int i = 0; i < arr.length; i++) {
lines[i] = new Line(arr[i][0], arr[i][1]);
}
Arrays.sort(lines, new StartAscComparator());
PriorityQueue<Integer> queue = new PriorityQueue<>();
int max = 0;
for (int i = 0; i <lines.length ; i++) {
if (!queue.isEmpty() && queue.peek() <= lines[i].start) {
queue.poll();
}
queue.add(lines[i].end);
max = Math.max(max, queue.size());
}
return max;
}
}
class CoverMax2 {
public static class Line{
private int start;
private int end;
public Line(int s, int end) {
this.start = s;
this.end = end;
}
}
public static class StartComparator implements Comparator<Line>{
@Override
public int compare (Line o1,Line o2){
return o1.start - o2.start;
}
}
public static int maxCover(int[][] arr) {
if (arr.length == 0 || arr == null) {
return 0;
}
Line[] lines = new Line[arr.length];
for (int i = 0; i < arr.length; i++) {
lines[i] = new Line(arr[i][0], arr[i][1]);
}
Arrays.sort(lines, new StartComparator());
PriorityQueue<Integer> queue = new PriorityQueue<>();
int max = 0;
for (int i = 0; i < lines.length; i++) {
if (!queue.isEmpty() && queue.peek() <= lines[i].start) {
queue.poll();
}
queue.add(lines[i].end);
max = Math.max(max, queue.size());
}
return max;
}
}
class CoverMaxForTest{
public static int maxCover(int[][] arr) {
@ -102,7 +189,7 @@ class CoverMaxMain {
for (int i = 0; i < testTime; i++) {
int[][] lines = generateLines(sizeMax, L, R);
int max = CoverMax.maxCover(lines);
int max = CoverMax2.maxCover(lines);
int maxForTest = CoverMaxForTest.maxCover(lines);
if (max != maxForTest) {
System.out.println("max : " + max + " maxForTest : " + maxForTest);

@ -1,4 +1,4 @@
package leo.class04;
package leo.class04_07;
/**
* @author Leo

@ -1,4 +1,4 @@
package leo.class04;
package leo.class04_07;
import java.util.ArrayList;
import java.util.Comparator;

@ -0,0 +1,299 @@
package leo.class05_08;
import java.util.HashMap;
// 该程序的对数器跑不过你能发现bug在哪吗
public class Code01_TrieTree {
public static class Node1 {
public int pass;
public int end;
public Node1[] nexts;
public Node1() {
pass = 0;
end = 0;
nexts = new Node1[26];
}
}
public static class Trie1 {
private Node1 root;
public Trie1() {
root = new Node1();
}
public void insert(String word) {
if (word == null) {
return;
}
char[] chs = word.toCharArray();
Node1 node = root;
node.pass++;
int index = 0;
for (int i = 0; i < chs.length; i++) { // 从左往右遍历字符
index = chs[i] - 'a'; // 由字符,对应成走向哪条路
if (node.nexts[index] == null) {
node.nexts[index] = new Node1();
}
node = node.nexts[index];
node.pass++;
}
node.end++;
}
public void delete(String word) {
if (search(word) != 0) {
char[] chs = word.toCharArray();
Node1 node = root;
node.pass--;
int index = 0;
for (int i = 0; i < chs.length; i++) {
index = chs[i] - 'a';
if (--node.nexts[index].pass == 0) {
node.nexts[index] = null;
return;
}
node = node.nexts[index];
}
node.end--;
}
}
// word这个单词之前加入过几次
public int search(String word) {
if (word == null) {
return 0;
}
char[] chs = word.toCharArray();
Node1 node = root;
int index = 0;
for (int i = 0; i < chs.length; i++) {
index = chs[i] - 'a';
if (node.nexts[index] == null) {
return 0;
}
node = node.nexts[index];
}
return node.end;
}
// 所有加入的字符串中有几个是以pre这个字符串作为前缀的
public int prefixNumber(String pre) {
if (pre == null) {
return 0;
}
char[] chs = pre.toCharArray();
Node1 node = root;
int index = 0;
for (int i = 0; i < chs.length; i++) {
index = chs[i] - 'a';
if (node.nexts[index] == null) {
return 0;
}
node = node.nexts[index];
}
return node.pass;
}
}
public static class Node2 {
public int pass;
public int end;
public HashMap<Integer, Node2> nexts;
public Node2() {
pass = 0;
end = 0;
nexts = new HashMap<>();
}
}
public static class Trie2 {
private Node2 root;
public Trie2() {
root = new Node2();
}
public void insert(String word) {
if (word == null) {
return;
}
char[] chs = word.toCharArray();
Node2 node = root;
node.pass++;
int index = 0;
for (int i = 0; i < chs.length; i++) {
index = (int) chs[i];
if (!node.nexts.containsKey(index)) {
node.nexts.put(index, new Node2());
}
node = node.nexts.get(index);
node.pass++;
}
node.end++;
}
public void delete(String word) {
if (search(word) != 0) {
char[] chs = word.toCharArray();
Node2 node = root;
node.pass--;
int index = 0;
for (int i = 0; i < chs.length; i++) {
index = (int) chs[i];
if (--node.nexts.get(index).pass == 0) {
node.nexts.remove(index);
return;
}
node = node.nexts.get(index);
}
node.end--;
}
}
// word这个单词之前加入过几次
public int search(String word) {
if (word == null) {
return 0;
}
char[] chs = word.toCharArray();
Node2 node = root;
int index = 0;
for (int i = 0; i < chs.length; i++) {
index = (int) chs[i];
if (!node.nexts.containsKey(index)) {
return 0;
}
node = node.nexts.get(index);
}
return node.end;
}
// 所有加入的字符串中有几个是以pre这个字符串作为前缀的
public int prefixNumber(String pre) {
if (pre == null) {
return 0;
}
char[] chs = pre.toCharArray();
Node2 node = root;
int index = 0;
for (int i = 0; i < chs.length; i++) {
index = (int) chs[i];
if (!node.nexts.containsKey(index)) {
return 0;
}
node = node.nexts.get(index);
}
return node.pass;
}
}
public static class Right {
private HashMap<String, Integer> box;
public Right() {
box = new HashMap<>();
}
public void insert(String word) {
if (!box.containsKey(word)) {
box.put(word, 1);
} else {
box.put(word, box.get(word) + 1);
}
}
public void delete(String word) {
if (box.containsKey(word)) {
if (box.get(word) == 1) {
box.remove(word);
} else {
box.put(word, box.get(word) - 1);
}
}
}
public int search(String word) {
if (!box.containsKey(word)) {
return 0;
} else {
return box.get(word);
}
}
public int prefixNumber(String pre) {
int count = 0;
for (String cur : box.keySet()) {
if (cur.startsWith(pre)) {
count+= box.get(cur);
// count++;
}
}
return count;
}
}
// for test
public static String generateRandomString(int strLen) {
char[] ans = new char[(int) (Math.random() * strLen) + 1];
for (int i = 0; i < ans.length; i++) {
int value = (int) (Math.random() * 6);
ans[i] = (char) (97 + value);
}
return String.valueOf(ans);
}
// for test
public static String[] generateRandomStringArray(int arrLen, int strLen) {
String[] ans = new String[(int) (Math.random() * arrLen) + 1];
for (int i = 0; i < ans.length; i++) {
ans[i] = generateRandomString(strLen);
}
return ans;
}
public static void main(String[] args) {
int arrLen = 10;
int strLen = 4;
int testTimes = 100000;
for (int i = 0; i < testTimes; i++) {
String[] arr = generateRandomStringArray(arrLen, strLen);
Trie1 trie1 = new Trie1();
Trie2 trie2 = new Trie2();
Right right = new Right();
for (int j = 0; j < arr.length; j++) {
double decide = Math.random();
if (decide < 0.25) {
trie1.insert(arr[j]);
trie2.insert(arr[j]);
right.insert(arr[j]);
} else if (decide < 0.5) {
trie1.delete(arr[j]);
trie2.delete(arr[j]);
right.delete(arr[j]);
} else if (decide < 0.75) {
int ans1 = trie1.search(arr[j]);
int ans2 = trie2.search(arr[j]);
int ans3 = right.search(arr[j]);
if (ans1 != ans2 || ans2 != ans3) {
System.out.println("Oops!");
}
} else {
int ans1 = trie1.prefixNumber(arr[j]);
int ans2 = trie2.prefixNumber(arr[j]);
int ans3 = right.prefixNumber(arr[j]);
if (ans1 != ans2 || ans2 != ans3) {
System.out.println("Oops!");
}
}
}
}
System.out.println("finish!");
}
}

@ -0,0 +1,105 @@
package leo.class05_08;
/**
* @author Leo
* @ClassName TrieTree
* @DATE 2020/12/1 5:06
* @Description
*/
class TrieTree {
public class Node{
int pass;
int end;
Node[] next;
public Node() {
this.pass = 0;
this.end = 0;
this.next = new Node[26];
}
}
private Node root;
public TrieTree() {
this.root = new Node();
}
public void insert(String word) {
if (word == null) {
return;
}
char[] str = word.toCharArray();
int path;
Node node = this.root;
node.pass++;
for (int i = 0; i < str.length; i++) {
path = str[i] - 'a';
if (node.next[path] == null) {
node.next[path] = new Node();
}
node.pass++;
node = node.next[path];
}
node.end++;
}
public void delete(String word) {
if (search(word) == 0) {
return;
}
char[] chars = word.toCharArray();
Node node = this.root;
node.pass--;
int path;
for (int i = 0; i < chars.length; i++) {
path = chars[i] - 'a';
if (--node.next[path].pass == 0) {
node.next[path] = null;
return;
}
node = node.next[path];
}
node.end--;
}
public int search(String word) {
if (word == null || "".equals(word.trim())) {
return 0;
}
char[] chars = word.toCharArray();
Node node = this.root;
int path;
for (int i = 0; i < chars.length; i++) {
path = chars[i] - 'a';
if (node.next[path] == null) {
return 0;
}
node = node.next[path];
}
return node.end;
}
public int prefixNumber(String word) {
if (word == null || "".equals(word.trim())) {
return 0;
}
Node node = this.root;
char[] chars = word.toCharArray();
int path;
for (int i = 0; i < chars.length; i++) {
path = chars[i] - 'a';
if (node.next[path] == null) {
return 0;
}
node = node.next[path];
}
return node.pass;
}
}
Loading…
Cancel
Save