加载中...

存档 - 2020 年 02 月

72 篇文章

daily leetcode - maximal-rectangle - !

题目地址 https://leetcode.com/problems/maximal-rectangle/ 题目描述 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. Example: Input: [ ["1","0","1","0","0"], ["1","0","1","1","1"], ["1","1","1","1","1"], ["1","0","0","1","0"] ] Output: 6 思路 此题是之前那道的 Largest Rectangle in Histogram 的扩展,这道题的二维矩阵每一层向上都可以看做一个直方图,输入矩阵有多少行,就可以形成多少个直方图,对每个直方图都调用 Largest Rectangle in Histogram 中的方法,就可以得到最大的矩形面积。那么这道题唯一要做的就是将每一层都当作直方图的底层,并向上构造整个直方图,由于题目限定了输入矩阵的字....

daily leetcode - largest-rectangle-in-histogram - !

题目地址 https://leetcode.com/problems/largest-rectangle-in-histogram/ 题目描述 Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest rectangle is shown in the shaded area, which has area = 10 unit. For example, Given height = [2,1,5,6,2,3], return 10. 思路 这道题让求直方图中最大的矩形,刚开始看到求极值问题以为要用 DP 来做,可是想不出递推式,只得作罢。这道题....

daily leetcode - remove-duplicates-from-sorted-list - !

题目地址 https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 题目描述 Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Output: 1->2 Example 2: Input: 1->1->2->3->3 Output: 1->2->3 思路 这道题让我们移除给定有序链表的重复项,那么可以遍历这个链表,每个结点和其后面的结点比较,如果结点值相同了,只要将前面结点的 next 指针跳过紧挨着的相同值的结点,指向后面一个结点。这样遍历下来,所有重复的结点都会被跳过,留下的链表就是没有重复项的了。 关键点解析 代码 解法一: class Solution { public: ListNode* deleteDuplicates(ListNode* head) { Lis....

daily leetcode - remove-duplicates-from-sorted-list-ii - !

题目地址 https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题目描述 Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Example 1: Input: 1->2->3->3->4->4->5 Output: 1->2->5 Example 2: Input: 1->1->1->2->3 Output: 2->3 思路 和之前那道 Remove Duplicates from Sorted List 不同的地方是这里要删掉所有的重复项,由于链表开头可能会有重复项,被删掉的话头指针会改变,而最终却还需要返回链表的头指针。所以需要定义一个新的节点,然后链上原链表,然后定义一个前驱指针和一个现指针,每当前驱指针指向新建的....

daily leetcode - search-in-rotated-sorted-array-ii - !

题目地址 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题目描述 Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]). You are given a target value to search. If found in the array return true, otherwise return false. Example 1: Input: nums = [2,5,6,0,0,1,2], target = 0 Output: true Example 2: Input: nums = [2,5,6,0,0,1,2], target = 3 Output: false Follow up: This is a follow up problem to....

随笔 2020-2-28

🔥 随便记点什么吧! 今天是 2020 年 2 月 28 日,距离我当初为期六个月攒钱的计划已经过去了一半,钱是没攒到多少,倒是攒下了工作中满满的不如意。在网上买的高数 36 今天中午到了,久违的高数啊,今天晚上吃过晚饭,洗好碗后,就迫不及待的拆开包装,打开一看懵了,那些曾经非常熟悉,了然于心,背的滚瓜烂熟的数学公式在时隔一年多之后的今天竟然忘得一塌糊涂,惭愧呀!🐧 幸好当初离开学校的时候,把专业课、高数等这些硬书给寄了回来。回望一下基础,应当会加深对以往知识的理解。刚刚看了两节,果然还是当初熟悉的味道,还是那么的一看就明白,做题就是不会的感觉!😄 🚧 今天在这里定下一个小小的目标,在 4 月份之前,争取看完高数的 18 讲。

daily leetcode - remove-duplicates-from-sorted-array-ii - !

题目地址 https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题目描述 Given a sorted array nums , remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1: Given _nums_ = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of _nums_ being 1, 1, 2, 2 and 3 respectively. It does....

daily leetcode - word-search - !

题目地址 https://leetcode.com/problems/word-search/ 题目描述 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. For example, Given board = [ ["ABCE"], ["SFCS"], ["ADEE"] ] word = "ABCCED", -> returns true, word = "SEE", -> returns true, word = "ABCB", -> returns false. 思路 这道题是典型的深度优先遍....

daily leetcode - subsets - !

题目地址 https://leetcode.com/problems/subsets/ 题目描述 Given a set of distinct integers, S , return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] 思路 这道求子集合的问题,由于其要列出所有结果,按照以往的经验,肯定要是要用递归来做。这道题其实它的非递归解法相对来说更简单一点,下面我们先来看非递归的解法,由于题目要求子集合中数字的顺序是非降序排列的,所有我们需要预处理,先给输入数组排序,然后再进一步处理,最开始我在想的时候,是想按照子集的长度由少到多全部写出来,比如子集长度为 0 的就是....

daily leetcode - combinations - !

题目地址 https://leetcode.com/problems/combinations/ 题目描述 Given two integers n and k , return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 思路 这道题让求 1 到 n 共 n 个数字里 k 个数的组合数的所有情况,还是要用深度优先搜索 DFS 来解,根据以往的经验,像这种要求出所有结果的集合,一般都是用 DFS 调用递归来解。那么我们建立一个保存最终结果的大集合 res,还要定义一个保存每一个组合的小集合 out,每次放一个数到 out 里,如果 out 里数个数到了 k 个,则把 out 保存到最终结果中,否则在下一层中继续调用递归。网友u010500263 的博客里有一张图很好的说明了递归调用的顺序,请点击这里。 关键点解析 代码 解法一: cl....

avatar
Lonus Lan
It's better to burn out than to fade away!
公告
暂无更新通知!
最新文章
网站资讯
文章数目 :
156
已运行时间 :
0 天
本站在线访客数 :
10
本站总访问量 :
0