1.二查找分法
找出最左的位置或最右的位置
package 左乘云.新手班.第3节;
public class 二分查找 {
public static void main(String [] args){
int arr[] = new int[]{1, 3, 3, 3, 4, 4, 5};
// int index = a(arr, 4);
// System.out.println(index);
int index = b(arr, 3, 0, arr.length);
System.out.println(index);
}
// 找出最左的数
public static int a(int arr[], int value){
if (arr.length == 0 || arr == null) return -1;
int l = 0;
int r = arr.length;
int index = -1;
while(l<=r){
int mid = (l+r)>>1;
if(value <= arr[mid]){
if (value == arr[mid]) index = mid;//注意
r = mid-1;
}else {
l = mid + 1;
}
}
return index;
}
// 方法2
public static int b(int[] arr, int value, int l, int r){
if (l > r) return -1;
int index = -1;
int temp = 0;
int mid = (l+r)>>1;
if (value > arr[mid]){
return b(arr, value, mid + 1, r);
}else if (value < arr[mid]){
return b(arr, value, l, mid - 1);
}else{
index = mid;
temp = index+1;
//换成大于号就是找大的,换成小于号就是找小的
while (temp > 0 && arr[temp] == value){
index = temp;
temp--;
}
return index;
}
}
}
2.取局部最小值
package 左乘云.新手班.第3节;
public class 局部最小值 {
public static void main(String[] args) {
int [] arr = new int[]{9, 7, 5, 7, 8, 9};
int index = min(arr, 0, arr.length-1);
System.out.println(index);
}
public static int min(int [] arr, int l, int r){
if (arr.length == 0 || arr == null) return -1;
if (arr.length == 1) return arr[0];
int N = arr.length;
// 需要考虑一开始就是凹的情况
// if (arr.length == 2){
// return arr[0] < arr[1] ? arr[0] : arr[1];
// }
if (arr[0] < arr[1]) return arr[0];
if (arr[N-1] < arr[N-2]) return arr[N-1];
int mid = (l+r)>>1;
if (arr[mid]<arr[mid+1] && arr[mid] < arr[mid-1]){
return mid;
}
else if (arr[mid] > arr[mid-1]){
r = mid-1;
return min(arr, l, r);
}
else if (arr[mid] > arr[mid+1]){
l = mid + 1;
return min(arr, l, r);
}
else return -1;
}
}
3.哈希表
public class 哈希表 {
public static class Node {
public int value;
public Node(int v) {
value = v;
}
}
// (K V)表
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("zuochengyun", "我是左程云");
System.out.println(map.containsKey("zuochengyun"));
System.out.println(map.containsKey("zuo"));
System.out.println(map.get("zuochengyun"));
map.put("zuochengyun", "他是左程云");
System.out.println(map.get("zuochengyun"));
// map.remove("zuochengyun");
// System.out.println(map.containsKey("zuochengyun"));
// System.out.println(map.get("zuochengyun"));
String test1 = "zuochengyun";
String test2 = "zuochengyun";
System.out.println(map.containsKey(test1));
System.out.println(map.containsKey(test2));
HashMap<Integer, String> map2 = new HashMap<>();
map2.put(1234567, "我是1234567");
Integer a = 1234567;
Integer b = 1234567;
System.out.println(a == b);
System.out.println(map2.containsKey(a));
System.out.println(map2.containsKey(b));
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));
System.out.println(map3.containsKey(node2));
System.out.println("===================");
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));
System.out.println(treeMap1.containsKey(6));
System.out.println(treeMap1.get(3));
treeMap1.put(3, "他是3");
System.out.println(treeMap1.get(3));
treeMap1.remove(3);
System.out.println(treeMap1.get(3));
System.out.println(treeMap1.firstKey());
System.out.println(treeMap1.lastKey());
// <=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<Node, String> treeMap2 = new TreeMap<>();
// treeMap2.put(node3, "我是node3");
// treeMap2.put(node4, "我是node4");
}
}