随笔分类 - 算法
随手写的
摘要:https://leetcode.cn/problems/maximum-count-of-positive-integer-and-negative-integer class Solution { public int maximumCount(int[] nums) { // 思路:由于数组非
阅读全文
摘要:https://kamacoder.com/problempage.php?pid=1176 import java.util.*; public class Main { static int[] dx={0,1,0,-1}; static int[] dy={1,0,-1,0}; static
阅读全文
摘要:45. 跳跃游戏 II class Solution { public int jump(int[] nums) { // 最短路做法: // 起始点为0,终点为n-1,边权重为0~nums[i] Deque<int[]> q=new ArrayDeque<>(); boolean[] vis=ne
阅读全文
摘要:322. 零钱兑换 class Solution { public int coinChange(int[] coins, int amount) { // 使用图的方式解决 // 最短路问题,总金额从0到amount需要走多少步 // 每一步能迈向的点都是面额里的点+出发点 // 每步的边权都是1
阅读全文
摘要:1559. 二维网格图中探测环 class Solution { int[] dx={0,1,0,-1}; int[] dy={1,0,-1,0}; int n; int m; char[][] grid; boolean[][] vis; int startx; int starty; boole
阅读全文
摘要:1020. 飞地的数量 class Solution { int[] dx={0,1,0,-1}; int[] dy={1,0,-1,0}; int n; int m; int[][] grid; boolean[][] vis; int res; public int numEnclaves(in
阅读全文
摘要:733. 图像渲染 class Solution { int[] dx={0,1,0,-1}; int[] dy={1,0,-1,0}; int n; int m; int[][] image; boolean[][] vis; int color; int sourceColor; public
阅读全文
摘要:https://leetcode.cn/problems/pacific-atlantic-water-flow/description/ class Solution { List<List<Integer>> res = new ArrayList<>(); int[] dx={0,1,0,-1
阅读全文
摘要:https://leetcode.cn/problems/island-perimeter/description/ 思路:遍历岛屿,一旦遍历到边界或者水则周长++ class Solution { int res; boolean[][] vis; int[][] grid; int[] dx=n
阅读全文
摘要:https://leetcode.cn/problems/surrounded-regions/ class Solution { int n; int m; boolean[][] vis; char[][] board; int[] dx=new int[]{0,1,0,-1}; int[] d
阅读全文
摘要:https://leetcode.cn/problems/number-of-operations-to-make-network-connected/ 并查集模版题 class Solution { public int makeConnected(int n, int[][] connectio
阅读全文
摘要:https://leetcode.cn/problems/redundant-connection/ class Solution { public int[] findRedundantConnection(int[][] edges) { // 思路:遍历边,不断的加入相连的点到集合中,直到遍历
阅读全文
摘要:https://leetcode.cn/problems/number-of-provinces/ 并查集模版题 class Solution { int[] p; void init(int n) { p=new int[n]; for(int i=0;i<n;i++)p[i]=i; } int
阅读全文
摘要:https://leetcode.cn/problems/get-equal-substrings-within-budget/description/ 滑动窗口,固定的套路是,固定右端点枚举,缩小左端点维护合法的窗口状态,在维护使得合法后(状态变化具有单调性,即扩大区间,状态是单调变化的),左端点
阅读全文
摘要:https://leetcode.cn/problems/permutation-in-string/description/ class Solution { public boolean checkInclusion(String S1, String S2) { // 看数据范围以及感觉,是双
阅读全文
摘要:https://leetcode.cn/problems/minimum-window-substring/description/ 难点在于解决字符出现次数的计算以及如何理解[涵盖],如何判断是否涵盖 class Solution { public String minWindow(String
阅读全文
摘要:https://leetcode.cn/problems/subarray-product-less-than-k/description/ 同向双指针,没什么难点 class Solution { int res; public int numSubarrayProductLessThanK(in
阅读全文
摘要:https://leetcode.cn/problems/longest-substring-without-repeating-characters/description/ 同向双指针问题,利用hashmap判重 class Solution { int res; public int leng
阅读全文
摘要:https://leetcode.cn/problems/koko-eating-bananas/description/ 二段性:速度有得完和不吃完两个段关键点是编写check函数,比较繁杂 class Solution { public int minEatingSpeed(int[] pile
阅读全文
摘要:https://leetcode.cn/problems/find-peak-element/description/ class Solution { public int findPeakElement(int[] nums) { // 需要依据题意来证明一个定理 // i~n-1中切nums[
阅读全文

浙公网安备 33010602011771号