import java.util.Arrays;
public class BSExist {
// arr保证有序
public static boolean find(int[] arr, int num) {
if (arr == null || arr.length == 0) {
return false;
}
int L = 0;
int R = arr.length - 1;
while (L <= R) {
int mid = (L + R) / 2; // 可能会溢出
if (arr[mid] == num) {
return true;
} else if (arr[mid] < num) {
L = mid + 1;
} else {
R = mid - 1;
}
}
return false;
}
// for test
public static boolean test(int[] sortedArr, int num) {
for (int cur : sortedArr) {
if (cur == num) {
return true;
}
}
return false;
}
// for test
public static int[] generateRandomArray(int maxSize, int maxValue) {
int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
}
return arr;
}
public static void main(String[] args) {
int testTime = 500000;
int maxSize = 10;
int maxValue = 100;
boolean succeed = true;
for (int i = 0; i < testTime; i++) {
int[] arr = generateRandomArray(maxSize, maxValue);
Arrays.sort(arr);
int value = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
if (test(arr, value) != find(arr, value)) {
System.out.println("出错了!");
succeed = false;
break;
}
}
System.out.println(succeed ? "Nice!" : "Fucking fucked!");
}
}
import java.util.Arrays;
public class BSNearLeft {
// arr有序的,>=num 最左
public static int mostLeftNoLessNumIndex(int[] arr, int num) {
if (arr == null || arr.length == 0) {
return -1;
}
int L = 0;
int R = arr.length - 1;
int ans = -1;
while (L <= R) {
int mid = (L + R) / 2; // 可能会溢出
if (arr[mid] >= num) {
ans = mid;
R = mid - 1;
} else {
L = mid + 1;
}
}
return ans;
}
// for test
public static int test(int[] arr, int value) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= value) {
return i;
}
}
return -1;
}
// for test
public static int[] generateRandomArray(int maxSize, int maxValue) {
int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
}
return arr;
}
// for test
public static void printArray(int[] arr) {
if (arr == null) {
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int testTime = 500000;
int maxSize = 10;
int maxValue = 100;
boolean succeed = true;
for (int i = 0; i < testTime; i++) {
int[] arr = generateRandomArray(maxSize, maxValue);
Arrays.sort(arr);
int value = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
if (test(arr, value) != mostLeftNoLessNumIndex(arr, value)) {
printArray(arr);
System.out.println(value);
System.out.println(test(arr, value));
System.out.println(mostLeftNoLessNumIndex(arr, value));
succeed = false;
break;
}
}
System.out.println(succeed ? "Nice!" : "Fucking fucked!");
}
}
import java.util.Arrays;
public class BSNearRight {
// 在arr上,找满足<=value的最右位置
public static int nearestIndex(int[] arr, int value) {
int L = 0;
int R = arr.length - 1;
int index = -1; // 记录最右的对号
while (L <= R) {
int mid = L + ((R - L) >> 1);
if (arr[mid] <= value) {
index = mid;
L = mid + 1;
} else {
R = mid - 1;
}
}
return index;
}
// for test
public static int test(int[] arr, int value) {
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] <= value) {
return i;
}
}
return -1;
}
// for test
public static int[] generateRandomArray(int maxSize, int maxValue) {
int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
}
return arr;
}
// for test
public static void printArray(int[] arr) {
if (arr == null) {
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int testTime = 500000;
int maxSize = 10;
int maxValue = 100;
boolean succeed = true;
for (int i = 0; i < testTime; i++) {
int[] arr = generateRandomArray(maxSize, maxValue);
Arrays.sort(arr);
int value = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
if (test(arr, value) != nearestIndex(arr, value)) {
printArray(arr);
System.out.println(value);
System.out.println(test(arr, value));
System.out.println(nearestIndex(arr, value));
succeed = false;
break;
}
}
System.out.println(succeed ? "Nice!" : "Fucking fucked!");
}
}
无序数组,返回局部最小索引
arr[0] : 0 为局部最小
arr[N-1] : N-1 为局部最小
arr[i-1]>arr[i]
public class BSAwesome {
// arr 整体无序
// arr 相邻的数不相等!
public static int oneMinIndex(int[] arr) {
if (arr == null || arr.length == 0) {
return -1;
}
int N = arr.length;
if (N == 1) {
return 0;
}
if (arr[0] < arr[1]) {
return 0;
}
if (arr[N - 1] < arr[N - 2]) {
return N - 1;
}
int L = 0;
int R = N - 1;
// L...R 肯定有局部最小
while (L < R - 1) {
int mid = (L + R) / 2;
if (arr[mid] < arr[mid - 1] && arr[mid] < arr[mid + 1]) {
return mid;
} else {
if (arr[mid] > arr[mid - 1]) {
R = mid - 1;
} else {
L = mid + 1;
}
}
}
return arr[L] < arr[R] ? L : R;
}
// 生成随机数组,且相邻数不相等
public static int[] randomArray(int maxLen, int maxValue) {
int len = (int) (Math.random() * maxLen);
int[] arr = new int[len];
if (len > 0) {
arr[0] = (int) (Math.random() * maxValue);
for (int i = 1; i < len; i++) {
do {
arr[i] = (int) (Math.random() * maxValue);
} while (arr[i] == arr[i - 1]);
}
}
return arr;
}
// 也用于测试
public static boolean check(int[] arr, int minIndex) {
if (arr.length == 0) {
return minIndex == -1;
}
int left = minIndex - 1;
int right = minIndex + 1;
boolean leftBigger = left >= 0 ? arr[left] > arr[minIndex] : true;
boolean rightBigger = right < arr.length ? arr[right] > arr[minIndex] : true;
return leftBigger && rightBigger;
}
public static void printArray(int[] arr) {
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
public static void main(String[] args) {
int maxLen = 100;
int maxValue = 200;
int testTime = 1000000;
System.out.println("测试开始");
for (int i = 0; i < testTime; i++) {
int[] arr = randomArray(maxLen, maxValue);
int ans = oneMinIndex(arr);
if (!check(arr, ans)) {
printArray(arr);
System.out.println(ans);
break;
}
}
System.out.println("测试结束");
}
}
时间复杂度(Time complexity)是一个函数,它定性描述该算法的运行频率。这是一个代表算法输入值的字符串的长度的函数. 时间复杂度常用大O表述,不包括这个函数的低阶项和首项系数。
O ( 1 ) , O ( l o g N ) , O ( N ) , O ( N ∗ l o g N ) , O ( N 2 ) , O ( N 3 ) , O ( k N ) , O ( n ! ) O(1),O(logN),O(N),O(N*logN),O(N^2),O(N^3),O(k^N),O(n!) O(1),O(logN),O(N),O(N∗logN),O(N2),O(N3),O(kN),O(n!)
ArrayList
动态扩容
数组查找 index
位置的时间复杂度是 O ( n ) O(n) O(n) ,我们思考扩容对时间复杂度的影响。计算知道扩容代价是 O ( n ) O(n) O(n) ,均摊到每一次操作就是 O ( 1 ) O(1) O(1) ,证明 ArrayList
和普通数组的时间复杂度一样都是常数级别的,但是比普通数组略慢一点。
哈希表无论多少数据,增删改查的时间复杂度都是 O ( 1 ) O(1) O(1) ,但是这个 O ( 1 ) O(1) O(1) 的系数有些大。
哈希表的按值查询
有序表无论多少数据,增删改查的时间复杂度都是 O ( l o g N ) O(logN) O(logN) 。
key
要可以比较,不然不能用,要自定义比较器
import java.util.HashMap;
import java.util.TreeMap;
public class TreeMap {
// (K V)表
public static void main(String[] args) {
TreeMap<Integer, String> treeMap1 = new TreeMap<>();
treeMap1.put(3, "我是3");
treeMap1.put(0, "我是3");
treeMap1.put(7, "我是3");
treeMap1.put(2, "我是3");
treeMap1.put(5, "我是3");
treeMap1.put(9, "我是3");
System.out.println(treeMap1.containsKey(7)); // true
System.out.println(treeMap1.containsKey(6)); // false
System.out.println(treeMap1.get(3)); // 我是3
treeMap1.put(3, "他是3");
System.out.println(treeMap1.get(3)); // 他是3
treeMap1.remove(3);
System.out.println(treeMap1.get(3)); // false
System.out.println(treeMap1.firstKey()); // 0
System.out.println(treeMap1.lastKey()); // 9
// <=5 离5最近的key告诉我
System.out.println(treeMap1.floorKey(5));
// <=6 离6最近的key告诉我
System.out.println(treeMap1.floorKey(6));
// >=5 离5最近的key告诉我
System.out.println(treeMap1.ceilingKey(5));
// >=6 离6最近的key告诉我
System.out.println(treeMap1.ceilingKey(6));
// Node node3 = new Node(3);
// Node node4 = new Node(4);
// TreeMap treeMap2 = new TreeMap<>();
// treeMap2.put(node3, "我是node3");
// treeMap2.put(node4, "我是node4");
}
}
哈希表的按值传递(原生数据结构 Integer Double Float String Character)
内存消耗为真实长度
public class HashMap {
// (K V)表
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put(1234567, "我是1234567");
Integer a = 1234567;
Integer b = 1234567;
System.out.println(a == b); // false
System.out.println(map.containsKey(a)); // true
System.out.println(map.containsKey(b)); // true
}
}
按引用传递(非原生数据结构 自定义 Node
)
内存消耗为地址内存和
import java.util.HashMap;
import java.util.TreeMap;
public class HashMap {
public static class Node {
public int value;
public Node(int v) {
value = v;
}
}
// (K V)表
public static void main(String[] args) {
Node node1 = new Node(1);
Node node2 = new Node(1);
HashMap<Node, String> map3 = new HashMap<>();
map3.put(node1, "我进来了!");
System.out.println(map3.containsKey(node1)); // true
System.out.println(map3.containsKey(node2)); // false
}
}
欢迎关注,会经常记录一些算法学习中遇到的问题。
欢迎随时留言讨论,与君共勉,知无不答!