![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
文章平均质量分 68
dream161110
这个作者很懒,什么都没留下…
展开
-
简单背包问题小结
简单的背包问题包括0-1背包问题和完全背包问题。0-1背包问题指的是在可选择项里面,每项最多选择一次,即选择0或者1次。完全背包问题指的是可选择项里面的每项可以选无数次。做题套路:两层循环,外层是可选择项的每个选项的遍历,里层是背包容量的遍历。在容量增加的情况下,里层的正向遍历会导致重复选择问题,而反序则保证了每个选择最多选择一次。如果0-1背包中每次选项都存在增加或者减少背包容量,则用next数组记录下一步得到的情况,再装回原来的dp数组。0-1背包问题:416 分割子集问题474 一和零4原创 2020-09-23 14:47:35 · 1316 阅读 · 0 评论 -
广度优先搜索(BFS)
广度优先搜索(BFS)的数据结构是队列queue。算法思路是用vector来记录每层结点,然后清空当前队列,再将该层队列的下一层加入队列。算法思路:public class BreadthFirstPaths { private boolean[] marked; // 到达该顶点的最短路径已知吗? private int[] edgeTo; // 到达该顶点的已知路径上的最后一个顶点 ...原创 2020-02-17 20:15:29 · 112 阅读 · 0 评论 -
深度优先搜索(DFS)
深度优先搜索的算法分为两种,图和二叉树。普通dfs是应用在图上,要用布尔数组记录所有结点是否访问过,防止无限递归导致死循环。// javapublic class DepthFirstSearch { private boolean[] marked; private int count; public DepthFirstSearch(Graph G, int s)...原创 2020-02-17 18:53:19 · 329 阅读 · 0 评论 -
LeetCode刷题之c++方法记录
主要是STL方法的记录遍历vectorfor (auto mn : nums) {if (mn <= first) first = mn;else if (mn <= second) second = mn;else if (first < second && second < mn) return true;}...原创 2020-02-11 13:10:33 · 417 阅读 · 0 评论 -
动态规划之复制粘贴数
LeetCode 650. 2 Keys Keyboard题目Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:Copy All: You can copy all the charact原创 2018-01-13 18:31:10 · 303 阅读 · 0 评论 -
层次遍历之寻找二叉树每层最大值
LeetCode 515 Find Largest Value in Each Tree Row题目You need to find the largest value in each row of a binary tree.Example:Input: 1 / \ 3 2 / \ \ 5 3原创 2018-01-18 08:47:39 · 5971 阅读 · 0 评论 -
动态规划之最少爬梯耗费
LeetCode 746. Min Cost Climbing Stairs题目On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).Once you pay the cost, you can either climb one or two steps. You need to原创 2018-01-13 19:39:22 · 284 阅读 · 0 评论 -
LeetCode刷题之no.1
Two Sum的总结关键知识简单介绍详细知识题目自己做法学习代码题目地址关键知识unorder_map的使用简单介绍这个数据结构采用类数组的结构,内部是由key和value作为一个单元,key是唯一的,value采用链式节点加在key的后面,类似于哈希表的链式结构。unorder_map的好处就是内部实现了哈希表,因此其查找速度非常的快。详细知识cplus介绍博客介绍题目自己...原创 2018-12-26 23:19:18 · 90 阅读 · 0 评论 -
LeetCode刷题之no.2
Add Two Number的总结关键知识简单介绍详细介绍题目自己做法学习代码关键知识链表的应用简单介绍构建链表的时候可以先建立链表第一个,再根据需要继续构建往后的结点。或者更好的方法,采用一个冗余的头结点,再通过循环的方式,一致地构建链表结点。这样的优点是代码简洁紧凑。详细介绍无题目自己做法class Solution {public: ListNode* add...原创 2018-12-27 22:58:57 · 96 阅读 · 0 评论 -
LeetCode刷题之no.3
Longest Substring Without Repeating Characters的总结关键知识简单介绍解题过程两层遍历三层遍历题目个人解答学习方法题目地址关键知识字符串处理和识别简单介绍字符串内的字符特点,可以通过两层遍历来实现。这样代码逻辑清晰易懂,但是时间效率比较差。另外一种,就是构建128个字符串标志符, 在上面记录字符串的特征。但是逻辑有点难,需要明天继续理解。解题过...原创 2018-12-27 23:41:50 · 116 阅读 · 0 评论 -
LeetCode刷题之no.206
Longest Substring Without Repeating Characters的总结关键知识简单介绍解答过程题目个人解答题目地址关键知识链表的处理简单介绍这是链表最基本的操作。链表分为带头结点和不带头结点,单向链表和双向链表。很明显,只有单向链表才有逆转链表的做法。链表通常是由一些结点和指针组成,结点含有值域和指向下一个结点的next指针,构成的是线性表。解答过程该链表不...原创 2018-12-29 00:21:50 · 94 阅读 · 0 评论 -
LeetCode刷题之no.92
Reverse Linked List II的总结关键知识解答过程题目个人解答题目地址关键知识链表的处理解答过程题目要求将m到n区间内的结点逆序。做法是首先定位到第m个结点,然后记录好第m-1结点和第m个结点。从m开始,用pre,cur和next三个指针分别记录前一结点,当前结点和下一结点。然后按顺序遍历后面n-m个结点,总共需要遍历n-m+1个结点。最后要判断m是否从1开始,从1开始要修...原创 2018-12-29 00:38:30 · 113 阅读 · 0 评论 -
LeetCode刷题之no.160
Intersection of Two Linked Lists的总结关键知识解题思路简单做法最优做法题目简答做法最优解法题目地址关键知识链表的处理解题思路一种比较简单的方法就是将以一条链表中的每个结点的跟另一条链表的每个结点相比较,首个相等的结点即为两个链表的交的起始结点。如果简单地双层遍历,会使得时间复杂到O(n^2),如果用到stl中的set,可以将平均复杂度降到O(n),但是最差情...原创 2018-12-29 01:06:53 · 90 阅读 · 0 评论 -
LeetCode刷题之no.141
Linked List Cycle的总结关键知识解题思路简单做法最佳解法题目简答做法最优解法题目地址关键知识链表的处理解题思路一种简单的做法就是顺序遍历每个结点,然后每次都判断该点是否在前面遍历过的结点中,可以采用两层遍历的做法或者是采用set来存储前面的结点,然后每次在set中find这个结点。但是set会将空间复杂度大大增加。另一种做法就是采用快慢两个指针在链表中遍历,如果快慢指针会...原创 2018-12-29 01:29:46 · 138 阅读 · 0 评论 -
LeetCode刷题之no.141
Linked List Cycle II的总结关键知识解题思路简单做法最佳解法题目简答做法最优解法题目地址关键知识链表的处理解题思路一种简单的做法就是顺序遍历每个结点,然后每次都判断该点是否在前面遍历过的结点中,可以采用两层遍历的做法或者是采用set来存储前面的结点,然后每次在set中find这个结点。但是set会将空间复杂度大大增加。另一种做法就是采用快慢两个指针在链表中遍历,如果快慢...原创 2018-12-29 01:45:51 · 97 阅读 · 0 评论 -
bfs学习之寻找二叉树每层最大值
LeetCode题目515 Find Largest Value in Each Tree Row题目:You need to find the largest value in each row of a binary tree.Example:Input: 1 / \ 3 2 / \ \ 5 3 9原创 2017-10-06 14:01:41 · 1740 阅读 · 0 评论 -
巧妙算法之查找目标数
LeetCode240 Search a 2D Matrix II题目Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted i原创 2018-01-12 00:33:35 · 281 阅读 · 0 评论 -
dfs学习之扫雷问题
LeetCode题目529 之 Minesweeper题目描述:Let’s play the minesweeper game (Wikipedia online game)!You are given a 2D char matrix representing the game board. ‘M’ represents an unrevealed mine, ‘E’ represents an原创 2017-10-09 18:52:39 · 812 阅读 · 0 评论 -
动态规划之计算二进制数中1的个数
LeetCode 338 Counting Bits题目Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.原创 2017-10-27 20:02:03 · 659 阅读 · 0 评论 -
dfs学习之寻找朋友圈数
LeetCode题目 547. Friend Circles题目There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B原创 2017-10-11 15:30:13 · 355 阅读 · 0 评论 -
bfs学习之寻找底层最左子树
LeetCode题目513 Find Bottom Left Tree Value题目:Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \1 3Output: 1 Note: You may assume the tree (i.e.,原创 2017-10-06 11:15:44 · 415 阅读 · 0 评论 -
dfs学习之打家劫舍(3)
LeetCode题目337. House Robber III题目The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besides the root, each house has one and onl原创 2017-10-12 13:44:28 · 618 阅读 · 0 评论 -
dfs学习之生成目标数方法
LeetCode题目 494. Target Sum题目You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its ne原创 2017-10-13 15:52:15 · 266 阅读 · 0 评论 -
动态规划学习之生成目标数方法
LeetCode题目 494. Target Sum题目You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its ne原创 2017-10-14 23:16:49 · 210 阅读 · 0 评论 -
分治法学习之多项式运算顺序
LeetCode题目 241Different Ways to Add Parentheses题目Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The原创 2017-10-15 23:57:22 · 1156 阅读 · 0 评论 -
动态规划之回文子串数
题目Given a string, your task is to count how many palindromic substrings in this string.The substrings with different start indexes or end indexes are counted as different substrings even they consist o原创 2017-12-11 10:34:12 · 596 阅读 · 0 评论 -
动态规划之和化乘积
LeetCode343. Integer Break题目Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.For ex原创 2017-12-11 11:05:14 · 321 阅读 · 0 评论 -
动态规划之预测赢家
LeetCode 486. Predict the Winner题目Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and原创 2017-12-11 11:34:56 · 1741 阅读 · 0 评论 -
动态规划之不同数字组成数的数量
LeetCode 357. Count Numbers with Unique Digits题目Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.Example:Given n = 2, return 91. (The answer should be the tota原创 2017-12-11 17:09:43 · 461 阅读 · 0 评论 -
动态规划之配对链最大长度
LeetCode 646. Maximum Length of Pair Chain题目You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.Now, we define a pair (c, d) can follow another pa原创 2017-12-11 17:45:19 · 242 阅读 · 0 评论 -
dfs学习之n皇后问题
LeetCode题目51 N-Queens题目描述The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the原创 2017-10-06 19:17:24 · 453 阅读 · 0 评论