签到

​​​https://blog.51cto.com/user/sign​

​​​​https://leetcode.cn/profile/info/​


学习参考

​https://labuladong.github.io/algo/​

​​​​https://programmercarl.com/​

​​​​https://visualgo.net/zh​

​​​​https://leetcode.cn/circle/discuss/G0n5iY/​

​https://www.chengxuchu.com/​


​IDEA设置LeeCode插件​



算法

​【LeeCode】排序算法​


​【LeeCode】颜色分类​


​【LeeCode】三数之和​




回溯法

​【LeeCode】46. 全排列​

排列问题

​【LeeCode】47. 全排列 II​

排列问题

​【LeeCode】77. 组合​

组合问题

​​​【LeeCode】39. 组合总和​

组合问题

​【LeeCode】40. 组合总和 II​

组合问题

​【LeeCode】377. 组合总和 Ⅳ​

组合问题

​【LeeCode】17. 电话号码的字母组合​

组合问题

​【LeeCode】78. 子集​

子集问题

​【LeeCode】90. 子集 II​

子集问题

131.分割回文串

分割问题

93.复原IP地址

分割问题

51.N皇后

棋盘问题

37.解数独

棋盘问题

491.递增子序列


332.重新安排行程



字符串


​【LeeCode】3.无重复字符的最长子串​


​【LeeCode】求两个字符串的公共字符​


​【LeeCode】最长公共子串(连续)​


​【LeeCode】443. 压缩字符串 -- 待完善​


​【LeeCode】76. 最小覆盖子串​


​【LeeCode】28. 找出字符串中第一个匹配项的下标​



双指针

​【LeeCode】392. 判断子序列​










二分法

​【LeeCode】704. 二分查找​

​【LeeCode】1011. 在 D 天内送达包裹的能力​

​【LeeCode】1482. 制作 m 束花所需的最少天数​




位运算

只出现一次的数字 II
只出现一次的数字 III
数组中两个数的最大异或值
重复的DNA序列
最大单词长度乘积


单调栈

  1. ​739.每日温度​
  2. ​496.下一个更大元素I​
  3. ​503.下一个更大元素II​
  4. ​42.接雨水​
  5. ​84.柱状图中最大的矩形​


​739. 每日温度​​​







数组

​【LeeCode】977.有序数组的平方​

​【LeeCode】448. 找到所有数组中消失的数字​

​【LeeCode】1748. 唯一元素的和​


排列

数组

​78. 子集​​​


​46. 全排列​​​


​​​77. 组合 ​



字符串





​字符串全排列​





括号

​【LeeCode】有效括号深度 (自测)​​​


​【LeeCode】22. 括号生成​​​


​​​【LeeCode】678. 有效的括号字符串​



​二叉树的简单学习​


​【LeeCode】94. 二叉树的中序遍历​


​【LeeCode】104. 二叉树的最大深度​



链表

​https://leetcode.cn/problems/reverse-linked-list/solutions/36710/dong-hua-yan-shi-206-fan-zhuan-lian-biao-by-user74/​


​【LeeCode】链表的学习​







贪心算法

​​​【LeeCode】455. 分发饼干​


​【LeeCode】122.买卖GP的最佳时机II​


​【LeeCode】123. 买卖股P的最佳时机 III​



​【LeeCode】714. 买卖股P的最佳时机含手续费​


​【LeeCode】309. 最佳买卖股P时机含冷冻期​



​【LeeCode】376. 摆动序列​


​【LeeCode】53. 最大子数组和​














动态规划

​背包问题大全​

动态规划的的四个解题步骤是:

  • 定义子问题
  • 写出子问题的递推关系
  • 确定 DP 数组的计算顺序
  • 空间优化(可选)

 【LeeCode】汇总_排序算法

​​​【LeeCode】198. 打家劫舍​




​【LeeCode】300. 最长递增子序列​

子序列(不连续)



​【LeeCode】152. 乘积最大子数组​


​【LeeCode】53. 最大子数组和​

子序列(连续)

​【LeeCode】718. 最长重复子数组​

子序列(连续)

​【LeeCode】674. 最长连续递增序列​

子序列(连续)






 【LeeCode】汇总_排序算法_02


​前缀和思想​


​​​【LeeCode】1. 两数之和​


​【LeeCode】560. 和为 K 的子数组​


​​​【LeeCode】724. 寻找数组的中心索引​


 【LeeCode】汇总_排序算法_03


模板


滑动窗口

模板1​​(推荐)​

 【LeeCode】汇总_排序算法_04

public int longestOnes(int[] nums, int k) {
int len = nums.length;
int res = 0;
int left = 0;
int right = 0;
int zero = 0;
while (right < len){
if (nums[right] == 0) zero++;
while (zero > k) {
if (nums[left++] == 0)
zero--;
}
res = Math.max(res, right - left + 1);
right++;
}
return res;
}

模板2: 

/* 滑动窗口算法框架 */
void slidingWindow(string s, string t) {
Map<Character, Integer> need = new HashMap<>();
Map<Character, Integer> window = new HashMap<>();
for (char c : t.toCharArray())
need.put(c,need.getOrDefault(c,0)+1);
int left = 0, right = 0;
int valid = 0;
while (right < s.size()) {
// c 是将移入窗口的字符
char c = s.charAt(right);
// 右移窗口
right++;
// 进行窗口内数据的一系列更新
...

/*** debug 输出的位置 ***/
System.out.println("window: ["+left+","+ right+")");
/********************/

// 判断左侧窗口是否要收缩
while (window needs shrink) {
// d 是将移出窗口的字符
char d = s[left];
// 左移窗口
left++;
// 进行窗口内数据的一系列更新
...
}
}
}



回溯法

 【LeeCode】汇总_排序算法_05


回文字符串

方案1: 递归处理

public boolean Palindrome(String str){
StringBuilder sb = new StringBuilder(str);
if (str.equals(sb.reverse().toString())){
return true;
}else{
return false;
}
}

方案2: 首字符等于尾字符

private boolean isPalindrome(int left, int right) {
while (left < right)
if (s.charAt(left++) != s.charAt(right--))
return false;
return true;
}

方案3: 字符翻转后判断是否跟原字符相同

public boolean Palindrom(String str){
if (str.length() == 0 || str.length() == 1){
return true;
}
// 首尾字符相同 且 后面依次递归
return str.charAt(0) == str.charAt(str.length() - 1 ) && Palindrome(str.substring(1, str.length() - 1));
}

二分法

import java.util.*;
// 2023-1-15

class Solution {
public int search(int[] nums, int target) {
Arrays.sort(nums);
int start = 0;
// 这里是下标, 所以要减1
int end = nums.length - 1;
int mid = check(nums, target, start, end);
System.out.println(mid);
return mid;
}

private int check(int[] nums, int target, int start, int end) {
if (end < start) return -1;
int mid = (start + end) / 2;
if (nums[mid] == target) return mid;
if (nums[mid] >= target){
return check(nums, target, start, mid - 1);
}
if (nums[mid] < target){
return check(nums, target, mid + 1, end);
}
return -1;
}
}
public class Main {
public static void main(String[] args) {
new Solution().search(new int[]{-1,0,3,5,9,12}, 9); // 输出: 4
new Solution().search(new int[]{-1,0,3,5,9,12}, 2); // 输出: -1
new Solution().search(new int[]{-1,0,3,5,9,12}, 13); // 输出: -1
}
}


/**
* 范围查询规律
* 初始化:
* int left = 0;
* int right = nums.length - 1;
* 循环条件
* left <= right
* 右边取值
* right = mid - 1
* 左边取值
* left = mid + 1
* 查询条件
* >= target值, 则 nums[mid] >= target时, 都减right = mid - 1
* > target值, 则 nums[mid] > target时, 都减right = mid - 1
* <= target值, 则 nums[mid] <= target时, 都加left = mid + 1
* < target值, 则 nums[mid] < target时, 都加left = mid + 1
* 结果
* 求大于(含等于), 返回left
* 求小于(含等于), 返回right
* 核心思想: 要找某个值, 则查找时遇到该值时, 当前指针(例如right指针)要错过它, 让另外一个指针(left指针)跨过他(体现在left <= right中的=号), 则找到了
*/

作者:chendragon
链接:https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/solution/er-fen-cha-zhao-tong-yong-gui-lu-gu-ding-g93u/

质数

package com.company;

// 2022-12-26

import java.util.ArrayList;
import java.util.List;

class Solution {
/**
求1-n之间的质数
质数又称素数。一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数;否则称为合数。
*/
public void sushu(int n){
// 最小的素数: 2
List<Integer> list = new ArrayList<>();
for (int i = 2; i <= n; i++){
boolean flag = true;
for (int j = 2; j < i; j++){
if ( i % j == 0){
flag = false;
break;
}
}
if (flag) list.add(i);
}
System.out.println(list);
}
}

public class Test{
public static void main(String[] args) {
new Solution().sushu(100);
}
}


前缀和

// 开辟数组空间
int[] presum = new int[nums.length + 1];

for (int i = 0; i < nums.length; i++) {
presum[i+1] = nums[i] + presum[i];
}

 【LeeCode】汇总_排序算法_06