随笔分类 - 算法
摘要:647. 回文子串 //动态规划 // class Solution { // public int countSubstrings(String s) { // char[] a = s.toCharArray(); // int len = a.length; // //dp[i][j]代表区间
阅读全文
摘要:115. 不同的子序列 class Solution { public int numDistinct(String s, String t) { char[] a1 = s.toCharArray(), a2 = t.toCharArray(); if (a1.length == 0 || a2.
阅读全文
摘要:718. 最长重复子数组 class Solution { public int findLength(int[] nums1, int[] nums2) { int len1 = nums1.length, len2 = nums2.length; int[][] dp = new int[len
阅读全文
摘要:714. 买卖股票的最佳时机含手续费 class Solution { public int maxProfit(int[] prices, int fee) { int len = prices.length; int[][] dp = new int[len][2]; //两种状态:持有和不持有
阅读全文
摘要:122. 买卖股票的最佳时机 II // class Solution { // public int maxProfit(int[] prices) { // int n = prices.length; // int[][] dp = new int[n][2]; // dp[0][0] = -
阅读全文
摘要:139. 单词拆分 class Solution { public boolean wordBreak(String s, List<String> wordDict) { int len = s.length(); boolean[] dp = new boolean[len + 1]; Set<
阅读全文
摘要:377. 组合总和 Ⅳ class Solution { public int combinationSum4(int[] nums, int target) { int[] dp = new int[target + 1]; //求排列 每一层求得是所有物品刚好凑满i的排列数 每个物品无限使用 d
阅读全文
摘要:474. 一和零 class Solution { public int findMaxForm(String[] strs, int m, int n) { //两个维度的0/1背包 int[][] dp = new int[m + 1][n + 1]; for (String str : str
阅读全文
摘要:0/1背包滚动数组 题目: 代码: package LeetCode; import java.util.Arrays; public class zero_one_bag { public static void main(String[] args) { int[] weight = {1, 3
阅读全文
摘要:343. 整数拆分 class Solution { public int integerBreak(int n) { if (n < 2) return -1; int dp[] = new int[n + 1]; dp[2] = 1; for (int i = 3; i <= n; i++) {
阅读全文
摘要:509. 斐波那契数 class Solution { public int fib(int n) { if (n <= 1) return n; int dp[] = new int[n+1]; dp[0] = 0; dp[1] = 1; for (int i = 2; i <= n; i++)
阅读全文
摘要:56. 合并区间 class Solution { public int[][] merge(int[][] intervals) { //按左边界排 相等则右边界 Arrays.sort(intervals, (o1, o2) -> { if (o1[0] == o2[0]) return Int
阅读全文
摘要:435. 无重叠区间 class Solution { public int eraseOverlapIntervals(int[][] intervals) { //转化为找无重叠区间个数的问题 //按右边界排序 Arrays.sort(intervals, (o1, o2) -> { if (o
阅读全文
摘要:406. 根据身高重建队列 class Solution { public int[][] reconstructQueue(int[][] people) { //问题设计两个维度 h和k 需先确定一个维度 再来考虑解决另一个维度 //k不能先解决,因为k的合理性有h确定 所以先解决h //身高由
阅读全文
摘要:135. 分发糖果 class Solution { public int candy(int[] ratings) { int len = ratings.length; if (len == 0) return 0; //分配数组 int[] alot = new int[len]; //先从前
阅读全文
摘要:1005. K 次取反后最大化的数组和 class Solution { public int largestSumAfterKNegations(int[] nums, int k) { Arrays.sort(nums); int res = 0; //反转负数 计算总和 int i = 0,
阅读全文
摘要:53. 最大子数组和 class Solution { public int maxSubArray(int[] nums) { int count = 0, res = Integer.MIN_VALUE; int len = nums.length; for (int i = 0; i < le
阅读全文
摘要:37. 解数独 class Solution { public void solveSudoku(char[][] board) { backtracking(board); } private boolean backtracking(char[][] board) { //遍历整个数组找空位置填
阅读全文
摘要:51. N 皇后 class Solution { private List<List<String>> res; //存第i行放置位置 private int[] place; public List<List<String>> solveNQueens(int n) { res = new Ar
阅读全文
摘要:46. 全排列 class Solution { private List<List<Integer>> res; private LinkedList<Integer> path; public List<List<Integer>> permute(int[] nums) { res = new
阅读全文