![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Data Structure And Algorithm
Boston_Kimisong
这个作者很懒,什么都没留下…
展开
-
二分查找边界问题
二分查找某个元素是否存在left <= right找到目标后立即返回public static int binarySearch(int[] nums, int target) { int left = 0, right = nums.length-1; while(left < right) { int mid = left + (right-left) / 2; if(nums[mid] == target) {原创 2021-03-03 06:49:59 · 146 阅读 · 0 评论 -
n位非负整数移除k位,使剩余的数最小/大
n位非负整数移除k位,使剩余的数最小输入: num = "1432219", k = 3输出: "1219"解释: 移除掉三个数字 4, 3, 和 2 形成一个新的最小的数字 1219。// 0 <= k <= npublic static String removeKDigitsToSmallest(String num, int k) { Deque<Character> deque = new LinkedList<>(); for(ch原创 2021-03-01 20:00:08 · 233 阅读 · 0 评论 -
Java BitMap
参考:https://www.jianshu.com/p/e530baada558import java.util.*;/** * https://www.jianshu.com/p/e530baada558 */public class BitMap { private byte[] bits; public BitMap(int n) { bits = new byte[getIndex(n)+1]; } /** * 找到byte转载 2020-09-03 07:53:23 · 206 阅读 · 0 评论 -
桶排序
https://blog.csdn.net/qq_27124771/article/details/87651495转载 2020-08-19 13:58:49 · 77 阅读 · 0 评论 -
汉诺塔 Java
public class Hanoi { public static void move(int n, char from, char to) { System.out.println("Disk " + n + " from " + from + " to " + to); } public static void hanoi(int n, char from, char inter, char to) { if(n == 1) {原创 2020-08-19 04:15:45 · 73 阅读 · 0 评论 -
和为某个特定值的数组元素组合(是否存在/个数/所有组合)
1. 数组中是否存在某个元素组合,使得组合中所有元素的和为某个特定值public static boolean findSum(int[] nums, int s) { // dp[i][j] whether we can get j after scanning the first i elements boolean[][] dp = new boolean[nums.length + 1][s + 1]; dp[0][0] = true; for (int i = 1原创 2020-08-01 08:41:13 · 1667 阅读 · 0 评论 -
最长公共子序列/字符串(LCSubsequence/Substring)
1.1 计算最长公共子序列的长度public static int longestCommonSubsequence1(String text1, String text2) { int[][] dp = new int[text1.length()+1][text2.length()+1]; for(int i = 1; i < text1.length()+1; i++) { for(int j = 1; j < text2.length()+1; j++原创 2020-07-31 15:52:13 · 159 阅读 · 0 评论 -
字符串加减法
字符串相加num1 > 0 && num2 > 0public static String addString(String num1, String num2) { StringBuilder builder = new StringBuilder(); int carry = 0; for(int i = num1.length()-1, j = num2.length()-1; i >= 0 || j >= 0 || carry ==原创 2020-07-07 20:18:24 · 2245 阅读 · 0 评论 -
快速选择算法(查找数组中第k小的元素)
思路在数组中选定一个轴(可随机选,但一般固定选数组的最后一个元素),将数组中比轴小的元素全部移动到轴的左边,然后把轴归位,设此时轴的索引为index,如果index+1 == k,则表明选定的轴即为数组中第k小的元素,直接返回。如果index+1 > k, 则表明第k小的元素在轴的左侧,继续在左侧递归查找该元素。如果index+1 < k, 则表明第k小的元素在轴的右侧,继续在右侧递归查找该元素。代码public int findKthSmallest(int[] nums, i原创 2020-06-27 14:09:31 · 2365 阅读 · 0 评论 -
希尔排序
思想分组并在每个组内进行插入排序代码public static void shellSort(int[] arr) { for(int gap = arr.length / 2; gap > 0; gap /= 2) { for(int i = gap; i < arr.length; i++) { int temp = arr[i]; int j; for(j = i - gap; j >原创 2020-06-23 11:56:18 · 66 阅读 · 0 评论 -
排序
插入排序冒泡排序选择排序快速排序归并排序堆排序原创 2020-06-21 22:31:49 · 77 阅读 · 0 评论 -
快速排序(原始、随机)
思路在数组中选择一个哨兵,将比哨兵小的元素移动到哨兵左侧,比哨兵大的元素移动到哨兵右侧,然后把哨兵放到正确的位置,然后用递归对哨兵两侧的序列进行排序。代码public static void quickSort(int[] arr, int leftBorder, int rightBorder) { if(leftBorder < rightBorder) { int left = leftBorder; int right = rightBord原创 2020-06-20 17:09:41 · 177 阅读 · 0 评论 -
插入排序
思路将数组分为已排序序列和未排序序列,已排序序列在前,未排序序列在后。将未排序序列中的第一个元素,从后向前与已排序序列中的元素逐个比较、交换,直到到达合适的位置。代码public static void insertionSort(int[] arr) { for(int i = 1; i < arr.length; i++) { int temp = arr[i]; for(int j = i; j > 0; j--){ i原创 2020-06-20 14:06:37 · 91 阅读 · 0 评论 -
冒泡排序
思路每次遍历将未排序序列中最大的元素移到序列的最后一个位置代码public static void bubbleSort(int[] arr) { for(int i = 0; i < arr.length; i++) { for(int j = 0; j < arr.length-i-1; j++) { if(arr[j] > arr[j+1]){ swap(arr, j, j+1);原创 2020-06-20 13:16:02 · 79 阅读 · 0 评论 -
选择排序
思路从待排序序列中选出一个最小元素,与已排序序列之后的第一个元素进行交换。代码public static void selectionSort(int[] arr) { for(int i = 0; i < arr.length-1; i++) { int minIndex = i; for (int j = i; j < arr.length; j++) { if (arr[j] < arr[minIndex]) {原创 2020-06-19 10:14:28 · 77 阅读 · 0 评论 -
归并排序
思路拆 + 合代码public static void mergeSort(int[] arr) { int[] temp = new int[arr.length]; sort(arr, 0, arr.length-1, temp);}public static void sort(int[] arr, int left, int right, int[] temp) { if(left == right) { return; }else{原创 2020-06-18 17:01:18 · 75 阅读 · 0 评论 -
堆排序
资料参考:https://www.cnblogs.com/chengxiao/p/6129630.html升序排序总体思路:对无序数组构建大顶堆,此时数组的最大元素是数组的第一个元素。将数组的第一个元素与数组的最后一个元素进行交换,此时数组的最大元素是数组的最后一个元素。排除数组的最后一个元素,对数组的剩余元素进行调整,使得数组的剩余元素构成一个新的大顶堆,此时这些剩余元素中的最大元素位于数组的倒数第二个位置。反复执行2、3所描述的交换+调整策略,直到整个数组有序。代码public sta原创 2020-06-18 12:34:58 · 77 阅读 · 0 评论 -
链表反转(全链表反转、链表区间反转)
全链表反转/** * Reverse a linkedList, for example * 1 -> 3 -> 5 -> 7 -> 9 * 9 -> 7 -> 5 -> 3 -> 1 * @param head * @return */public static ListNode reverse(ListNode head) { ListNode cur = head; ListNode prev = null; .原创 2020-06-13 10:05:29 · 701 阅读 · 1 评论 -
树的深度优先遍历(递归、非递归)
1. 先序遍历递归/** * pre-order traversal (recursive) * @param node */public static void preOrderTraverse1(TreeNode node) { if(node != null) { System.out.print(node.val + " "); preOrderTraverse1(node.left); preOrderTraverse1(node原创 2020-05-17 21:48:56 · 658 阅读 · 0 评论 -
Java 双栈模拟队列 (LC 232)
核心思路:创建两个栈,主栈和辅助栈,主栈用来存储数据,辅助栈用来调整顺序,保证元素先进先出。当向主栈中添加元素时,首先把主栈中的元素全部加入辅助栈,然后加入该元素,再把辅助栈中的所有元素加入主栈。class MyQueue { private Stack<Integer> stack1; private Stack<Integer> stack2;...原创 2020-03-21 14:43:06 · 171 阅读 · 0 评论 -
Java 数组模拟队列
import java.util.ArrayDeque;public class ArrayQueue<T> { private Object[] arr; private int first; private int last; private int size; public ArrayQueue(int queueSize) thro...原创 2020-03-21 08:42:25 · 84 阅读 · 0 评论 -
数组局部反转
public void reverse(int[] nums, int left, int right) { for(int i = left; i < left + (right-left+1)/2; i++) { int temp = nums[i]; nums[i] = nums[right-(i-left)]; nums[rig...原创 2020-02-25 08:59:11 · 526 阅读 · 0 评论 -
二分搜索 (Binary Search)
给定一个已经按照从小到大的顺序排好序的整型数组a, 已经一个整型变量target。1. 在数组中查找与target相等的元素/*** Search an element which is equal to the target in the given array.* @param a* @param target* @return If success, return the ind...原创 2019-12-30 11:48:42 · 78 阅读 · 0 评论 -
二叉搜索树
二叉搜索树:对于二叉树中的每个节点,它的值小于它的左子树中所有节点的值,并且大于它的右子树中的所有的指。插图所示即为一颗二叉树:在二叉搜索树中插入结点:递归法与非递归法public class BinarySearchTree { private TreeNode root; public void insert(int val) { root =...原创 2019-11-13 13:47:07 · 78 阅读 · 0 评论 -
字母结点型DSU
字母节点型DSU:Solution:Use a hashmap to store a mapping between the nodes and their parent nodes. So to each item of this hashmap, the key is one node and the value is its parent node.Code:import java...原创 2019-11-11 13:08:29 · 170 阅读 · 0 评论 -
并查集(DSU)中两种提高算法效率的方法:path compression && union-by-rank.
1.path compressionIf a node we are visiting is not a root node, then we modify the structure of the tree to make sure this node and all of its ancestor nodes(except root node) share the root node as ...原创 2019-11-03 09:45:46 · 1281 阅读 · 1 评论