algorithm
for62
一个热衷于源码分析、技术研究的Java后端练习生!
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
从二叉树遍历深入理解BFS和DFS
BFS(Breadth-First Search,广度优先搜索)和 DFS(Depth-First Search,深度优先搜索)是两种常见的图和树的遍历算法。原创 2025-02-10 21:45:41 · 643 阅读 · 0 评论 -
递归-回溯-k进制串
回溯原创 2022-06-15 16:24:03 · 122 阅读 · 0 评论 -
递归-是否有序数组
判断数组是否有序原创 2022-06-15 11:19:11 · 132 阅读 · 0 评论 -
递归-汉诺塔
汉诺塔如何理解?原创 2022-06-15 00:40:26 · 102 阅读 · 0 评论 -
递归解决全排列问题
private static void sortQueue(String input) { char[] chars = input.toCharArray(); TreeSet<String> treeSet = new TreeSet<>(); sortQueue(treeSet, chars, 0, chars.length - 1); for (String s : treeSet) { System.out.print(s +原创 2020-09-15 17:45:18 · 121 阅读 · 0 评论 -
dfs解决区域连续问题
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int m = scanner.nextInt(); int n = scanner.nextInt(); int nums[][] = new int[m][n]; for (int i = 0; i < m;原创 2020-09-15 09:46:24 · 161 阅读 · 0 评论 -
滑动窗口解决“最小覆盖子串”问题
/** * 滑动窗口解决“最小覆盖子串”问题 */class Solution { Map<Character, Integer> ori = new HashMap<Character, Integer>(); // 字符出现的次数 Map<Character, Integer> cnt = new HashMap<Character, Integer>(); /** * ori.put(tChars[i], ori原创 2020-09-14 11:11:46 · 187 阅读 · 0 评论 -
单调栈的应用(用于解决NextGreaterElement问题)
题一:返回下一个更大元素/** * 单调栈的使用 * 下一个更大元素 * * @param nums1 * @param nums2 * @return */public int[] nextGreaterElement(int nums1[], int nums2[]) { int result[] = new int[nums1.length]; Deque<Integer> stack = new LinkedList<>(); //原创 2020-09-14 09:21:26 · 225 阅读 · 0 评论 -
n维数组中找i个最值
题意:给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积。get:通过本题的接法我们可以发现如何在n维数组中找出最大(小)的i个数,if中将遍历值放在最值位置,后续else if逐个更新!class Solution { /** * 求出数组中最大的三个数以及最小的两个数 * * @param nums * @return */ public int maximumProduct(int[] nums) {原创 2020-09-13 20:46:46 · 210 阅读 · 0 评论 -
找出不在数组中的最小自然数
/** * 找出不在数组中的最小自然数 * 规则:tree[i] = i+1{1,2,...,length} * @param tree * @return */public int findMin(int[] tree) { int length = tree.length; for (int i = 0; i < length; i++) { // 用于维护规则 while (tree[i] > 0 && tree[i] <原创 2020-09-12 12:38:18 · 582 阅读 · 0 评论 -
切割子串,子串头尾相同,并使子串数量最小
两种策略:1、 双指针移动右指针,更新左指针2、 双指针移动左指针,更新右指针返回两种策略的最小值即可。 /** * 切割子串,子串头尾相同,并使子串数量最小 * 双指针 * * @param s * @return */ public static int solution(String s) { char[] chars = s.toCharArray(); // 双指针 int i = 0,原创 2020-09-12 12:21:55 · 229 阅读 · 0 评论 -
现有一个整形数组,可以重新排列数组中的元素,也可以任意删除元素,使得新数组a按照以下公式:sum = ∑a [i]*(i+1) 计算的和最大。
import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String strTemp = scanner.next(原创 2020-09-11 21:41:50 · 404 阅读 · 0 评论 -
快排思想找第K大的数
public int findKth(int[] a, int n, int K) { return quickFind(K, a, 0, n - 1);}public int quick(int nums[], int left, int right) { int temp = nums[left]; // 枢纽值 while (left < right) { while (temp >= nums[right] && left &原创 2020-09-10 13:51:01 · 122 阅读 · 0 评论 -
双指针法的应用
/** * 盛最多水的容器-双指针法-LeetCode11 * (时间复杂度O(n),空间复杂度O(1)) */class Solution { public int maxArea(int[] height) { int maxArea = 0, left = 0, right = height.length - 1; while (left < right) { maxArea = Math.max(maxArea, (rig原创 2020-08-22 11:56:19 · 134 阅读 · 0 评论 -
根据前序遍历和中序遍历重建二叉树以及树的遍历
import datastructure.TreeNode;import java.util.LinkedList;public class Mian { public static void main(String[] args) { int pre[] = {1, 2, 4, 7, 3, 5, 6, 8}; int in[] = {4, 7, 2, 1, 5, 3, 8, 6}; TreeNode tree = buildTree(pre,原创 2020-08-18 00:05:52 · 281 阅读 · 0 评论 -
最小绝对差-leetcode1200
class Solution { public List<List<Integer>> minimumAbsDifference(int[] arr) { int length = arr.length; List<List<Integer>> lists = new ArrayList<>(); Arrays.sort(arr); /** * 数组长度大于2原创 2020-08-14 23:48:43 · 144 阅读 · 0 评论 -
制造字母异位词的最小步骤数-LeetCode1347
本算法的核心在于学习【】内标注的思想class Solution { public int minSteps(String s, String t) { int length = s.length(); // s与t长度相同 int[] counts = new int[26]; // 26个字符的个数 for (int i = 0; i < length; i++) { char c1 = s.charAt(i),原创 2020-08-14 22:47:41 · 119 阅读 · 0 评论 -
令牌放置-leetcode948
1、 本题主要依据一个原则:能量换积分时取小能量,积分换能量时取大能量。所以需要保证数组有序。2、 先对数组进行排序,然后双指针分别指向数组左右端点。3、 定义积分:count表示不兑换最后一次,countChange表示兑换最后一次(能量不足时有没有必要兑换)4、 双指针left>right时循环break,特别说明一下当left=right时可能left或者right数值改变后未进行操作。5、 当前能量小于最小能量时直接break,返回结果;否则小能量换积分(要保存当前积分值与后续积分换大原创 2020-08-13 23:49:14 · 269 阅读 · 0 评论 -
乱序数组中找几个数(不相邻)要求加和最大
/** * 乱序数组中找几个数(不相邻)要求加和最大 * * @param n * @param a */public void printMax(int n, int a[]) { int dp[][] = new int[n + 1][2]; // 第一列存前i-1项求和,第二列求前i项求和 // 边界初始化可以不用写代码 for (int i = 0; i < n; i++) { dp[i + 1][0] = Math.max(dp[i][0]原创 2020-08-07 16:56:14 · 694 阅读 · 0 评论 -
回文(去掉一个字符)素数
import java.util.Scanner;/** * 回文(去掉一个字符)素数 * input:输入两个数n<m * output:输出两个数之间回文(去掉一个字符)素数的个数 * 回文素数:对一个数去掉一位之后是回文素数(既是素数又是回文数) */public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);原创 2020-08-06 22:11:49 · 415 阅读 · 0 评论 -
将无序序列调整为无重复元素的有序序列
思路分析:首先对序列进行排序,接着定义一个指向序列头的指针,逐次向后搜索,直至指针指向序列尾部。指针比较当前元素与下一元素是否相等(重复),相等时移除第一个元素,第二个元素倍乘,再对更新的序列重排序;不相等时,指针右移。 /** * 功能:将无序序列调整为无重复元素的有序序列 * 去重规则:重复元素的第一个删除,第二个倍乘 * 说明:可以使用Arrays.asList(...)将数组转为List; * @param lists 无序序列 * @retu原创 2020-07-29 16:59:05 · 1011 阅读 · 0 评论 -
快排思想求中位数
import java.util.Arrays;public class QuickSortMidNumber { public static void main(String[] args) { int arr[] = {5, 6, 9, 2, 3, 1, 4, 12, 7, 11, 10}; int val = quickMidNumber(arr, 0, arr.length - 1); System.out.println(val);原创 2020-07-29 00:00:31 · 250 阅读 · 0 评论
分享