日常

tech2025-04-02  13

排序系列 直接插入排序

public static void insertSort(int[] arr){ int j = 0; int temp = 0;//临时变量 for(int i = 1;i < arr.length;i++){//从第二个数开始比较 temp = arr[i]; //将当前数插入到已经有序的数组中 for(j = i - 1;j >= 0;j--){ if(arr[j] > temp){//如果前面的数大于当前数,将他后移 arr[j+1] = arr[j]; }else{ break; } } arr[j+1] = temp;//将当前轮数的数放到应该在的位置 } System.out.println(Arrays.toString(arr)); } public static void main(String[] args) { int[] arr={9,3,4,6,2}; insertSort(arr); }

快速排序

public class quicksort { public static void main(String[] args) { int[] arr={6,3,7,2,5,9}; quick(arr,0,arr.length-1); System.out.println(Arrays.toString(arr)); } private static void quick(int[] arr,int low,int high) { int i, j, temp, t; if (low > high) { return; } i = low; j = high; //temp就是基准位 temp = arr[low]; while (i < j) { //先看右边,依次往左递减 while (temp <= arr[j] && i < j) { j--; } //再看左边,依次往右递增 while (temp >= arr[i] && i < j) { i++; } //如果满足条件则交换 if (i < j) { t = arr[j]; arr[j] = arr[i]; arr[i] = t; } } //最后将基准为与i和j相等位置的数字交换 arr[low] = arr[i]; arr[i] = temp; //递归调用左半数组 quick(arr, low, j - 1); //递归调用右半数组 quick(arr, j + 1, high); } }

…… if(low>high){return;}

二叉树系列 层次遍历

class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res=new ArrayList<>(); if(root==null){return res;} LinkedList<TreeNode> queue=new LinkedList<>(); queue.add(root); while(queue.size()>0){ int size=queue.size(); ArrayList<Integer> tmp=new ArrayList<>(); for(int i=0;i<size;i++){ TreeNode t=queue.remove(); tmp.add(t.val); if(t.left!=null){queue.add(t.left);} if(t.right!=null){queue.add(t.right);} } res.add(tmp); } return res; } }

前序遍历

public class preOrder { class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> res=new ArrayList<>(); Stack<TreeNode> stack=new Stack<>(); if(root==null){return res;} stack.push(root); while(!stack.empty()){ TreeNode t=stack.pop(); res.add(t.val); if(t.right!=null){stack.push(t.right);} if(t.left!=null){stack.push(t.left);} } return res; } } }

中序遍历

public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res=new ArrayList<>(); if(root==null){return res;} Stack<TreeNode> stack=new Stack<>(); TreeNode cur=root; while(cur!=null||!stack.empty()){ while(cur!=null){ stack.push(cur); cur=cur.left; } cur=stack.pop(); res.add(cur.val); cur=cur.right; } return res; }

查找

public class BinarySearch { public static void main(String[] args) { int[] arr = {6, 12, 33, 87, 90, 97, 108, 561}; System.out.println(binarySearch(arr,90)); } private static int binarySearch(int[] arr, int key) { int low=0; int high=arr.length-1; while(low<=high){ int mid=(low+high)/2; if(arr[mid]>key){high=mid-1;} if(arr[mid]<key){low=mid+1;} if(arr[mid]==key){return mid;} } return -1; } }

判定是小于等于不然会找不到

其他 最小公倍数

public static void main(String args[]){ int m = min(a,b); int n = a * b / m; System.out.print(n); } public static int min(int a, int b){ if(a < b){ int t = a; a = b; b = t; } while(b != 0){ if(a == b){ return a; }else{ int k = a % b; a = b; b = k; } } return a; }
最新回复(0)