leetcode刷题分类笔记
Posted Panda_Java
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode刷题分类笔记相关的知识,希望对你有一定的参考价值。
leetcode刷题分类笔记20220413
0. 常规操作
1. 部分数组复制 Arrays.copyOfRange(nums,start,end) // 包含开始元素 不包含结尾元素
2. 字符串数组复制:String[] strs = "flower","flow","flight";
3. 将整型转化为字符串: String str = String.valueOf(n);
4. 将char转化为int: int c = str.charAt(i)-'0';
5. 字符串转化为字符数组: char[] nums = str.toCharArray();
6. 截取字符串: String a = s.substring(index,index+i); 不含末尾元素
7. 将字符串转为整形: Integer.valueOf(s) > 255
8. 获取字符串某一个字符 char ch = str.charAt(j)
1. 链表
83. 删除排序链表中的重复元素
class Solution
public ListNode deleteDuplicates(ListNode head)
if(head == null)
return head;
ListNode dump = new ListNode(-1);
ListNode q = head; // 慢指针
dump.next = q;
ListNode p = head.next; // 快指针
int a = head.val;
while(p!=null)
if(a != p.val)
q.next = p;
a = p.val; // 存值方便下一次再比较
q = p; // 慢指针也要往下一个位置
p = p.next;
else
p = p.next;
q.next = null;
return dump.next;
203. 移除链表元素
/**
* Definition for singly-linked list.
* public class ListNode
* int val;
* ListNode next;
* ListNode()
* ListNode(int val) this.val = val;
* ListNode(int val, ListNode next) this.val = val; this.next = next;
*
*/
class Solution
快慢指针//
public ListNode removeElements(ListNode head, int val)
if(head == null)
return head;
ListNode dump = new ListNode(-1);
dump.next = null;
ListNode q = dump; // q慢指针
ListNode p = head; // p快指针
while(p != null)
if(p.val != val)
q.next = p;
q = p;
p = p.next;
else
p = p.next;
q.next = null;
return dump.next;
2. 二叉树
leetcode108. 将有序数组转换为二叉搜索树
class Solution
public TreeNode sortedArrayToBST(int[] nums)
if(nums.length == 0)
return null;
int i = 0;
int j = nums.length;
int k = (i+j)/2;
int a = nums[k];
TreeNode root = new TreeNode(a);
root.left = sortedArrayToBST(Arrays.copyOfRange(nums,i,k));
root .right = sortedArrayToBST(Arrays.copyOfRange(nums,k+1,j));
return root;
111. 二叉树的最小深度
class Solution
public int minDepth(TreeNode root)
if(root == null)
return 0;
if(root.left== null && root.right == null)
return 1;
int res = Integer.MAX_VALUE;
if(root.left != null)
res = Math.min(minDepth(root.left),res);
if(root.right != null)
res = Math.min(minDepth(root.right),res);
return res+1;
113. 路径总和 II
class Solution
public List<List<Integer>> pathSum(TreeNode root, int targetSum)
List<List<Integer>> res = new LinkedList<>();
List<Integer> list = new LinkedList<>();
if(root == null)
return res;
dfs(res,root,targetSum,list);
return res;
public void dfs(List<List<Integer>> res,TreeNode root, int targetSum, List<Integer> list)
if(root == null)
return;
list.add(root.val);
if(root.left==null && root.right== null&& targetSum == root.val)
List<Integer> list1 = new LinkedList<>(list);
res.add(list1);
return;
if(root.left != null)
dfs(res,root.left,targetSum-root.val,list);
list.remove(list.size()-1);
if(root.right != null)
dfs(res,root.right,targetSum-root.val,list);
list.remove(list.size()-1);
543. 二叉树的直径
/**
* Definition for a binary tree node.
* public class TreeNode
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode()
* TreeNode(int val) this.val = val;
* TreeNode(int val, TreeNode left, TreeNode right)
* this.val = val;
* this.left = left;
* this.right = right;
*
*
*/
class Solution
int res;
public int diameterOfBinaryTree(TreeNode root)
if(root == null)
return 0;
dfs(root);
return res;
public int dfs(TreeNode root)
if(root == null)
return 0;
int l = dfs(root.left);
int r = dfs(root.right);
if(l + r > res)
res = l + r;
return Math.max(l,r) + 1;
3. 动态规划
45. 跳跃游戏 II
class Solution
public int jump(int[] nums)
// int[] dp = new int[nums.length];
// 此题并不是用动态规划而是贪心算法思想
int res = 0;
int maxPosition = 0;
int end = 0;
for(int i = 0; i < nums.length; i++)
maxPosition = Math.max(maxPosition,i+nums[i]);
if(end >= nums.length-1) //越界不用再跳了
break;
if(i == end)
res++;
end = maxPosition;
return res;
96. 不同的二叉搜索树
class Solution
public int numTrees(int n)
int[] res = new int[n+1];
res[0] = 1;
res[1] = 1;
for(int i = 2; i <= n; i++)
int sum = 0;
for(int j = 1; j <= i; j++)
sum += res[j-1]*res[i-j];
res[i] = sum;
return res[n];
4. 深度遍历与回溯
93. 复原 IP 地址
// Dfs
class Solution
public List<String> restoreIpAddresses(String s)
List<String> res = new LinkedList<>();
dfs(0,0,res,"",s);
return res;
public void dfs(int index, int count, List<String> res, String str, String s)
// if(count > 4) return;
if(count == 4 && index == s.length())
res.add(str);
for(int i = 1; i < 4; i++)
if(index + i > s.length())
return;
String a = s.substring(index,index+i);
if(a.startsWith("0") && a.length() > 1 || Integer.valueOf(a) > 255)
return;
dfs(index+i, count+1, res, str+a+(count==3?"":"."), s);
131. 分割回文串
class Solution
public List<List<String>> partition(String s)
List<List<String>> res = new LinkedList<>();
List<String> list = new LinkedList<>();
backtracking(res,list,s,0);
return res;
public void backtracking( List<List<String>> res, List<String> list,String s,int index)
if(index == s.length())
List<String> list1 = new LinkedList<>(list);
res.add(list1);
for(int i = index; i <s.length();i++)
String ss = s.substring(index,i+1);
if(!check(ss))
continue;
list.add(ss);
backtracking(res,list,s,i+1);
list.remove(list.size()-1);
public boolean check(String str)
if (str == null || str.length() <= 1)
return true;
int i = 0;
int j = str.length()-1;
while(i <= j)
if(str.charAt(i) == str.charAt(j))
i++;
j--;
else
return false;
return true;
方法二:
47. 全排列 II (去掉重复元素)
class Solution
public List<List<Integer>> permuteUnique(int[] nums)
List<List<Integer>> res = new ArrayList<>();
List<Integer> list = new ArrayList<>();
boolean[] p = new boolean[nums.length];
Arrays.sort(nums);
for(int i = 0; i < nums.length; i++)
p[i] = false;
backtracking(nums,res,list,p);
return res;
public void backtracking(int[] nums, List<List<Integer>> res,List<Integer> list1, boolean[] p)
if(list1.size()==nums.length)
List<Integer> temp = new ArrayList<>(list1);
res.add(temp);
return;
for(int i = 0; i < nums.length; i++)
if(i != 0 && nums[i-1] == nums[i] && p[i-1] == true)
continue;
if(!p[i])
list1.add(nums[i]);
p[i] = true;
backtracking(nums,res,list1,p);
list1.remove(list1.size()-1);
p[i] = false;
491. 递增子序列
class Solution
public List<List<Integer>> findSubsequences(int[] nums)
List<List<Integer>> res = new LinkedList<>();
List<Integer> list = LeetCode刷题系列之-多数之和类型