leetcode
文章平均质量分 70
taoqick
这个作者很懒,什么都没留下…
展开
-
LeetCode 665. Non-decreasing Array
Given an arraynumswithnintegers, your task is to check if it could become non-decreasing by modifyingat most one element.We define an array is non-decreasing ifnums[i] <= nums[i + 1]holds for everyi(0-based) such that (0 <= i <= n - 2)....原创 2021-08-08 21:45:23 · 119 阅读 · 0 评论 -
LeetCode 1325. Delete Leaves With a Given Value
Given a binary treerootand an integertarget, delete all theleaf nodeswith valuetarget.Notethat once you delete a leaf node with valuetarget,if it's parent node becomes a leaf node and has the valuetarget, it should also be deleted (you need to ...原创 2021-07-16 10:04:18 · 146 阅读 · 0 评论 -
LeetCode 947. Most Stones Removed with Same Row or Column 并查集
On a 2D plane, we placenstones at some integer coordinate points. Each coordinate point may have at most one stone.A stone can be removed if it shares eitherthe same row or the same columnas another stone that has not been removed.Given an arraysto...原创 2021-07-13 23:43:36 · 175 阅读 · 0 评论 -
720. Longest Word in Dictionary 题目有坑
Given an array of stringswordsrepresenting an English Dictionary, returnthe longest word inwordsthat can be built one character at a time by other words inwords.If there is more than one possible answer, return the longest word with the smallest le...原创 2021-07-12 23:36:56 · 134 阅读 · 0 评论 -
LeetCode 992. Subarrays with K Different Integers 滑动窗口 at most
Given an arraynumsof positive integers, call a (contiguous, not necessarily distinct) subarray ofnumsgoodif the number of different integers in that subarray is exactlyk.(For example,[1,2,3,1,2]has3different integers:1,2, and3.)Return the ...原创 2021-07-11 21:13:25 · 139 阅读 · 0 评论 -
LeetCode 980. Unique Paths III 使用位运算缓存路径
On a 2-dimensionalgrid, there are 4 types of squares:1represents the starting square. There is exactly one starting square. 2represents the ending square. There is exactly one ending square. 0represents empty squares we can walk over. -1represe...原创 2021-05-26 09:15:16 · 129 阅读 · 0 评论 -
Python heapq LeetCode 692. Top K Frequent Words
默认最小在最前,这个和sorted一样,没有reverse,但是可以用tuple来玩heapq是module(文件),所以importheapq.heappush是直接调用这个module最外层的函数import heapqh1 = []heapq.heappush(h1, (5, 'write code'))heapq.heappush(h1, (7, 'release p...原创 2020-01-01 00:06:22 · 230 阅读 · 0 评论 -
LeetCode 722. Remove Comments 删除注释
Given a C++ program, remove comments from it. The programsourceis an array wheresource[i]is thei-th line of the source code. This represents the result of splitting the original source code string by the newline character\n.In C++, there are two ty...原创 2021-05-16 21:01:28 · 195 阅读 · 0 评论 -
Leetcode 721. Accounts Merge 并查集
Given a list ofaccountswhere each elementaccounts[i]is a list of strings, where the first elementaccounts[i][0]is a name, and the rest of the elements areemailsrepresenting emails of the account.Now, we would like to merge these accounts. Two acc...原创 2021-05-06 10:17:10 · 186 阅读 · 0 评论 -
LeetCode 424. Longest Repeating Character Replacement 滑动窗口
Given a stringsthat consists of only uppercase English letters, you can perform at mostkoperations on that string.In one operation, you can chooseanycharacter of the string and change it to any other uppercase English character.Find the length of...原创 2021-02-14 18:09:02 · 141 阅读 · 1 评论 -
LeetCode 1032. Stream of Characters 4行Trie树
Implement theStreamCheckerclass as follows:StreamChecker(words): Constructor, init the data structure with the given words. query(letter): returns true if and only if for somek >= 1, the lastkcharacters queried (in order from oldest to newest, i...原创 2021-01-04 00:25:36 · 166 阅读 · 0 评论 -
LeetCode 741. Cherry Pickup 传纸条 动态规划
You are given an n x n grid representing a field of cherries, each cell is one of three possible integers.0 means the cell is empty, so you can pass through,1 means the cell contains a cherry that you can pick up and pass through, or-1 means the cell co原创 2020-12-27 22:08:55 · 255 阅读 · 0 评论 -
LeetCode 480. Sliding Window Median 延迟双堆
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.Examples:[2,3,4], the median is3[2,3], the median is(2 + 3) / 2 = 2.5Given an arr...原创 2020-12-06 23:22:43 · 136 阅读 · 0 评论 -
LeetCode 871. Minimum Number of Refueling Stops 动态规划类似背包 贪心
A car travels from a starting position to a destination which istargetmiles east of the starting position.Along the way, there are gas stations. Eachstation[i]represents a gas station that isstation[i][0]miles east of the starting position, and ha...原创 2020-11-29 16:27:22 · 165 阅读 · 0 评论 -
LeetCode 174. Dungeon Game 逆序动态规划
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way thro原创 2020-11-15 23:49:29 · 149 阅读 · 0 评论 -
利用栈/递归求解算术表达式 LeetCode 224. Basic Calculator
利用堆栈转自: http://blog.csdn.net/liuhuiyi/article/details/8433203概括一句话就是: 遇到)或者优先级比即将读入元素低的符号,都要把栈弹出到不能再次弹出为止符号和运算符可以分为两个栈1 本文目标分析用堆栈解析算术表达式的基本方法。给出的示例代码能解析任何包括+,-,*,/,()和0到9数字组成的算术表达式。...原创 2014-03-07 17:24:24 · 1196 阅读 · 0 评论 -
LeetCode 1031. Maximum Sum of Two Non-Overlapping Subarrays滑动窗口 前缀和
Given an arrayAof non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengthsLandM. (For clarification, theL-length subarray could occur before or after theM-length subarray.)Formall...原创 2020-11-03 23:28:42 · 230 阅读 · 0 评论 -
LeetCode 1326. Minimum Number of Taps to Open to Water a Garden 动态规划 离散化 贪心
There is a one-dimensional garden on the x-axis. The garden starts at the point0and ends at the pointn. (i.e The length of the garden isn).There aren + 1taps locatedat points[0, 1, ..., n]in the garden.Given an integernand an integer arrayra...原创 2020-10-22 23:58:06 · 369 阅读 · 0 评论 -
LeetCode 925. Long Pressed Name 有坑
Your friend is typing hisnameinto a keyboard. Sometimes, when typing a characterc, the key might getlong pressed, and the character will be typed 1 or more times.You examine thetypedcharacters of the keyboard. ReturnTrueif it is possible that i...原创 2020-10-15 22:51:50 · 215 阅读 · 0 评论 -
LeetCode 1209. Remove All Adjacent Duplicates in String II 有坑
Given a strings, akduplicate removalconsists of choosingkadjacent and equal letters fromsand removingthem causing the left and the right side of the deleted substring to concatenate together.We repeatedly makekduplicate removals onsuntil we ...原创 2020-10-14 23:56:23 · 314 阅读 · 0 评论 -
LeetCode 767. Reorganize String Python字符串
Given a stringS, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same.If possible, output any possible result. If not possible, return the empty string.Example 1:Input: S = "aab"Output: "a..原创 2020-10-11 23:54:45 · 281 阅读 · 0 评论 -
LeetCode 652. Find Duplicate Subtrees 递归小坑
Given therootof a binary tree, return allduplicate subtrees.For each kind of duplicate subtrees, you only need to return the root node of anyoneof them.Two trees areduplicateif they have thesame structurewith thesame node values.Example ...原创 2020-10-11 17:46:49 · 165 阅读 · 0 评论 -
LeetCode 274. H-Index 二分
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.According to thedefinition of h-index on Wikipedia: "A scientist has indexhifhof his/herNpapers haveat lea...原创 2020-10-11 14:49:23 · 129 阅读 · 0 评论 -
LeetCode 1277. Count Square Submatrices with All Ones 二维前缀
Given am * nmatrix of ones and zeros, return how manysquaresubmatrices have all ones.Example 1:Input: matrix =[ [0,1,1,1], [1,1,1,1], [0,1,1,1]]Output: 15Explanation: There are 10 squares of side 1.There are 4 squares of side 2.The...原创 2020-10-11 12:23:25 · 143 阅读 · 0 评论 -
LeetCode 959. Regions Cut By Slashes 并查集
In a N x Ngridcomposed of 1 x 1 squares, each 1 x 1 square consists of a/,\, or blank space. These characters divide the square into contiguous regions.(Note that backslash characters are escaped, so a\is represented as"\\".)Return the number o...原创 2020-10-09 23:22:50 · 147 阅读 · 0 评论 -
LeetCode 844. Backspace String Compare 坑
Given twostringsSandT,return if they are equal when both are typed into empty text editors.#means a backspace character.Note that afterbackspacing an empty text, the text will continue empty.Example 1:Input: S = "ab#c", T = "ad#c"Output: tr...原创 2020-09-21 23:48:57 · 140 阅读 · 0 评论 -
1145. Binary Tree Coloring Game 树上博弈
Two players play a turn based game on a binary tree. We are giventhe root of this binary tree, and the number of nodes nin the tree. n is odd, andeach node has a distinct value from 1 to n.Initially, the first player names a value x with 1 <= x &...原创 2020-08-29 13:56:08 · 218 阅读 · 0 评论 -
LeetCode 729. My Calendar I 红黑树 线段树 二分查找树
Implement a MyCalendar class to store your events. A new event can be added if adding the event will not cause a double booking.Your class will have the method, book(int start, int end). Formally, this represents a booking on the half open interval [star原创 2020-08-26 11:58:39 · 289 阅读 · 0 评论 -
LeetCode 354. Russian Doll Envelopes 最长递增子序列 Trick
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.What is the m原创 2020-08-25 15:09:42 · 148 阅读 · 0 评论 -
LeetCode 375. Guess Number Higher or Lower II 扔鸡蛋变种
We are playing the Guess Game. The game is as follows:I pick a number from 1 to n. You have to guess which number I picked.Every time you guess wrong, I'll tell you whether the number I picked is higher or lower.However, when you guess a particular n原创 2020-08-21 18:09:10 · 149 阅读 · 0 评论 -
LeetCode 792. Number of Matching Subsequences 桶排序
Given string S and adictionary of words words, find the number of words[i] that is a subsequence of S.Example :Input: S = "abcde"words = ["a", "bb", "acd", "ace"]Output: 3Explanation: There are three words in words that are a subsequence ofS : "..原创 2020-08-19 19:19:56 · 200 阅读 · 0 评论 -
LeetCode 840. Magic Squares In Grid 找规律
A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.Given an gridof integers, how many 3 x 3 "magic square" subgrids are there? (Each subgrid is contiguous)...原创 2020-08-18 15:50:58 · 256 阅读 · 0 评论 -
LeetCode 552. Student Attendance Record II 斐波拉契 log(n)
Given a positive integer n, return the number of all possible attendance records with length n, which will be regarded as rewardable. The answer may be very large, return it after mod 109 + 7.A student attendance record is a string that only contains the原创 2020-08-11 15:52:30 · 159 阅读 · 0 评论 -
LeetCode 837. New 21 Game 滑动窗口 逆向动态规划
Alice plays the following game, loosely based on the card game "21".Alice starts with 0 points, and draws numbers while she has less than K points. During each draw, she gains an integer number of points randomly from the range [1, W], where W is an int.原创 2020-08-11 14:55:36 · 207 阅读 · 0 评论 -
LeetCode 593. Valid Square 平面四点能否正方形
Given the coordinates of four points in 2D space, return whether the four points could construct a square.The coordinate (x,y) of a point is represented by an integer array with two integers.Example:Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0原创 2020-08-11 14:15:13 · 298 阅读 · 0 评论 -
LeetCode 803. Bricks Falling When Hit 时光倒流 注意标记
We have a grid of 1s and 0s; the 1s in a cell represent bricks. A brick will not drop if and only if it is directly connected to the top of the grid, or at least one of its (4-way) adjacent bricks will not drop.We will do some erasuressequentially. Eac..原创 2020-08-04 22:43:39 · 185 阅读 · 0 评论 -
LeetCode 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit 双端队列 deque
Given anarray of integers nums and aninteger limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal tolimit.Example 1:Input: nums = [8,2,4,7], li...原创 2020-08-04 15:09:02 · 304 阅读 · 0 评论 -
LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold 1074. 二维前缀
Given a m x nmatrix mat and an integer threshold. Return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such square.Example 1:Input: mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2..原创 2020-08-02 00:00:35 · 263 阅读 · 0 评论 -
LeetCode 914. X of a Kind in a Deck of Cards gcd 辗转相除 reduce
In a deck of cards, each card has an integer written on it.Return true if and only if you can chooseX >= 2 such thatit is possible to split the entire deckinto 1 or more groups of cards, where:Each group has exactly X cards. All the cards in eac...原创 2020-07-25 15:38:56 · 210 阅读 · 0 评论 -
LeetCode 1146. Snapshot Array 二分 空
Implement a SnapshotArray that supports the following interface:SnapshotArray(int length) initializes an array-like data structure with the given length. Initially, each element equals 0.void set(index, val) sets the element at the given index to be eq.原创 2020-07-21 22:40:35 · 186 阅读 · 0 评论