leetcode
文章平均质量分 71
bugMaker_Anna
Code shows everything ヾ(●´▽`●)ノ
展开
-
leetcode中01背包问题思路梳理
leetcode中01背包问题思路梳理416. 分割等和子集思路空间优化474. 一和零待续。。494. 目标和待续。。416. 分割等和子集给你一个 只包含正整数 的 非空 数组 nums 。请你判断是否可以将这个数组分割成两个子集,使得两个子集的元素和相等。思路乍一看与背包问题无关,但其实可以转化成01背包问题:给定N个物品,每个物品的重量为nums[i],和一个容量为sum/2的背包,问是否存在一种方法,可以恰好装满背包。定义dp[i][j]:用前i个数字,是否可能恰好凑成和为i。b原创 2021-07-08 13:31:54 · 783 阅读 · 0 评论 -
leetcode中完全背包问题思路梳理——零钱兑换
322. 零钱兑换给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。你可以认为每种硬币的数量是无限的。思路定义dp[i][j]:用前i种面额的硬币,凑成总金额j,所需要的最少硬币个数。basecase:...原创 2021-07-07 17:42:53 · 167 阅读 · 0 评论 -
经典背包问题思路梳理
01背包n件物品,第i件物品重量w[i],价值c[i],背包容量为V,如何选择物品,使背包内物品总价值最大?【每种物品只有一件】思路定义dp[i][j]:使用前i种物品,背包容量为V,能装下的最大价值。base case:dp[0][…] = 0,dp[…][0] = 0状态转移:装第i件物品:dp[i][j] = dp[i-1][j];不装第i件物品:dp[i][j] = dp[i-1][j-w[i]]+c[i];综上,dp[i][j] = max(dp[i-1][j], dp[i-原创 2021-07-06 22:03:07 · 166 阅读 · 0 评论 -
【leetcode】1143. 最长公共子序列(动态规划)
【leetcode】1143. 最长公共子序列题目描述解题思路1. DP基础方法代码2. DP优化(一维数组)代码题目描述给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度。一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。例如,“ace” 是 “abcde” 的子序列,但 “aec” 不是 “abcde” 的子序列。两个字符串的「公共子序列」是这两个字符串所共同拥有的子序列原创 2020-10-13 17:01:27 · 167 阅读 · 0 评论 -
【leetcode】64. 最小路径和(动态规划)
【leetcode】64. 最小路径和题目描述解题思路1. 可以修改原数组的方法代码2. 不能修改原数组,空间复杂度O(n)代码题目描述给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。说明:每次只能向下或者向右移动一步。示例:输入:[[1,3,1],[1,5,1],[4,2,1]]输出: 7解释: 因为路径 1→3→1→1→1 的总和最小。解题思路整体思路就是动态规划,只不过根据是否能修改原数组,选择二维数组(原数组),或者原创 2020-10-12 22:05:16 · 155 阅读 · 0 评论 -
【leetcode】300. 最长上升子序列(动态规划)
【leetcode】300. 最长上升子序列题目描述解题思路1. 动态规划,时间复杂度O(n2)代码2. 贪心+二分查找,时间复杂度O(nlogn)代码题目描述给定一个无序的整数数组,找到其中最长上升子序列的长度。示例:输入: [10,9,2,5,3,7,101,18]输出: 4解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。说明:可能会有多种最长上升子序列的组合,你只需要输出对应的长度即可。你算法的时间复杂度应该为 O(n2) 。进阶: 你能将算法的时间复杂度降低到原创 2020-10-12 20:25:11 · 89 阅读 · 0 评论 -
[leetcode] 75. Sort Colors
75. Sort ColorsDescriptionAnalysisCodeDescriptionGiven an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the o...原创 2019-03-07 11:51:28 · 131 阅读 · 0 评论 -
[leetcode] 96. Unique Binary Search Trees
96. Unique Binary Search TreesDescriptionAnalysisCodeDescriptionGiven n, how many structurally unique BST’s (binary search trees) that store values 1 … n?Example:Input: 3Output: 5Explanation:Gi...原创 2019-03-14 21:01:53 · 216 阅读 · 0 评论 -
[leetcode] 48. Rotate Image
48. Rotate ImageDescriptionAnalysisCodeDescriptionYou are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, whi...翻译 2019-03-07 17:14:28 · 132 阅读 · 0 评论 -
[leetcode] 141. Linked List Cycle
141. Linked List CycleDescriptionAnalysisCodeDescriptionGiven a linked list, determine if it has a cycle in it.To represent a cycle in the given linked list, we use an integer pos which represents ...原创 2019-03-08 11:18:52 · 127 阅读 · 0 评论 -
[leetcode] 137. Single Number II
137. Single Number IIDescriptionAnalysisCodeDescriptionGiven a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note:...原创 2019-07-30 17:38:15 · 64 阅读 · 0 评论 -
[leetcode]108 &109. Convert Sorted Array/List to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree & 109. Convert Sorted List to Binary Search TreeDescriptionAnalysisCodeDescriptionGiven an array where elements are sorted in ascending order, con...原创 2019-08-01 13:16:15 · 67 阅读 · 0 评论 -
[leetcode] 116. Populating Next Right Pointers in Each Node
116. Populating Next Right Pointers in Each NodeDescriptionAnalysisCodeDescriptionYou are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The b...原创 2019-08-01 17:12:40 · 96 阅读 · 0 评论 -
[leetcode] 117. Populating Next Right Pointers in Each Node II
117. Populating Next Right Pointers in Each Node IIDescriptionAnalysisCodeDescriptionGiven a binary treestruct Node {int val;Node *left;Node *right;Node *next;}Populate each next pointer to p...原创 2019-08-01 17:24:52 · 82 阅读 · 0 评论 -
[leetcode] 80. Remove Duplicates from Sorted Array II
80. Remove Duplicates from Sorted Array IIDescriptionAnalysisCodeDescriptionGiven a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new l...原创 2019-03-07 10:25:04 · 125 阅读 · 0 评论 -
[leetcode] 61. Rotate List
61. Rotate ListDescriptionAnalysisCodeDescriptionGiven a linked list, rotate the list to the right by k places, where k is non-negative.Example 1:Input: 1->2->3->4->5->NULL, k = 2O...原创 2019-03-07 09:54:00 · 134 阅读 · 0 评论 -
[leetcode] 66. Plus One
66. Plus OneDescriptionGiven a non-empty array of digits representing a non-negative integer, plus one to the integer.The digits are stored such that the most significant digit is at the head of th...原创 2018-10-18 16:30:12 · 146 阅读 · 0 评论 -
[leetcode] 88. Merge Sorted Array
DescriptionGiven two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:The number of elements initialized in nums1 and nums2 are m and n respectively.You may a...原创 2018-10-18 22:05:42 · 97 阅读 · 0 评论 -
[leetcode] 26.Remove Duplicates from Sorted Array & 27. Remove Element
本质上这两道题用的是同一种方法。设定一个temp下标表示更新过的数组元素下标,若符合题目要求,则将元素赋值给temp当前所在位置。最后返回temp。26. Remove Duplicates from Sorted ArrayGiven a sorted array nums, remove the duplicates in-place such that each element appe...原创 2018-10-16 10:23:16 · 110 阅读 · 0 评论 -
[leetcode]35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.思路...原创 2018-10-16 10:44:02 · 108 阅读 · 0 评论 -
[leetcode]1. Two Sum
[leetcode]1. Two SumDescription思路实现代码DescriptionGiven an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have ex...原创 2018-10-19 15:03:51 · 77 阅读 · 0 评论 -
[leetcode] 11. Container With Most Water
[leetcode] 11. Container With Most WaterDescriptionAnalysisCodeDescriptionGiven n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are draw...原创 2018-10-19 15:32:39 · 116 阅读 · 0 评论 -
[Leetcode] 36. Valid Sudoku
[Leetcode] 36. Valid SudokuDescriptionAnalysisCodeDescriptionDetermine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:Each row must cont...原创 2019-03-05 11:20:49 · 124 阅读 · 0 评论 -
[leetcode]15. 3Sum
15. 3SumDescriptionAnalysisCodeDescriptionGiven an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of ...原创 2019-03-05 19:57:19 · 98 阅读 · 0 评论 -
[leetcode] 16. 3Sum Closest
16. 3Sum ClosestDescriptionAnalysisCodeDescriptionGiven an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the t...原创 2019-03-05 21:19:23 · 114 阅读 · 0 评论 -
[leetcode] 18. 4Sum
18. 4SumDescriptionAnalysisCodeDescriptionGiven an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadrup...原创 2019-03-05 22:12:18 · 107 阅读 · 0 评论 -
[leetcode] 82.Remove Duplicates from Sorted List II
82. Remove Duplicates from Sorted List IIDescriptionAnalysisCodeDescriptionGiven a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original l...原创 2019-03-06 10:41:28 · 96 阅读 · 0 评论 -
[leetcode] 92. Reverse Linked List II
92. Reverse Linked List IIDecriptionAnalysisCodeDecriptionReverse a linked list from position m to n. Do it in one-pass.Note: 1 ≤ m ≤ n ≤ length of list.Example:Input: 1->2->3->4->5-&...原创 2019-03-06 14:25:55 · 133 阅读 · 0 评论 -
[leetcode] 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder TraversalDescriptionAnalysis & CodeDescriptionGiven a binary tree, return the inorder traversal of its nodes’ values.Example:Input: [1,null,2,3]12/3Output: [1,3,2...原创 2019-03-09 17:25:11 · 131 阅读 · 0 评论 -
[leetcode] 53. Maximum Subarray
53. Maximum SubarrayDescribe:Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example:Input: [-2,1,-3,4,-1,2,...原创 2018-10-18 15:47:30 · 107 阅读 · 0 评论