
LeetCode
永远的EMT
每天时刻保持超越自我的意识
展开
-
【LeetCode】129. Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the tota...原创 2018-10-15 23:13:15 · 277 阅读 · 0 评论 -
【LeetCode】Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed at a ti...原创 2018-10-29 02:02:22 · 180 阅读 · 0 评论 -
【LeetCode】Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given a non-empty string containing only digits, determine t...原创 2018-10-23 13:06:43 · 269 阅读 · 1 评论 -
【LeetCode】Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key. The ...原创 2018-10-21 22:49:10 · 167 阅读 · 0 评论 -
【LeetCode】Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.Example 1:Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"Output: trueExample 2:Input: s1 = "aabcc", s2 = "...原创 2018-10-22 01:54:48 · 211 阅读 · 0 评论 -
【LeetCode】Merge Sorted Array
Given 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 assume tha...原创 2018-10-28 00:56:09 · 192 阅读 · 0 评论 -
【LeetCode】Search a 2D Matrix
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 from left to right. The first integer of each row...原创 2018-11-06 23:54:01 · 160 阅读 · 0 评论 -
【LeetCode】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 in ascending from left to right. Integers in each...原创 2018-11-07 00:00:08 · 210 阅读 · 0 评论 -
【LeetCode】Edit Distance
Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.You have the following 3 operations permitted on a word:Insert a character Delete a chara...原创 2018-11-07 00:10:37 · 179 阅读 · 0 评论 -
【LeetCode】Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3 But the follo...原创 2018-10-20 22:46:22 · 196 阅读 · 0 评论 -
【LeetCode】Construct Binary Tree from Inorder and Postorder Traversal
题解:就是用中序和后序重建二叉树保证每个点不重复值上这里顺便列下中序和先序的代码,思路都一样用map存储可以节省查找时间,注意边界处理class Solution {private: unordered_map<int, int> inm; // inorder map [inorder[i], i]public: TreeNode* build...原创 2018-10-20 16:00:46 · 164 阅读 · 0 评论 -
【LeetCode】Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.Example:Input: [1,2,2]...原创 2018-10-22 22:58:49 · 208 阅读 · 0 评论 -
【LeetCode】128. Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Your algorithm should run in O(n) complexity.Example:Input: [100, 4, 200, 1, 3, 2]Output: 4Ex...原创 2018-10-16 00:30:07 · 186 阅读 · 0 评论 -
【LeetCode】198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house...原创 2018-10-09 16:23:30 · 136 阅读 · 0 评论 -
【LeetCode】70. Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Note: Given n will be a positive...原创 2018-10-09 17:24:44 · 119 阅读 · 0 评论 -
【LeetCode】126. Word Ladder II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:Only one letter can be changed at a time Eac...原创 2018-10-17 01:07:30 · 273 阅读 · 0 评论 -
【LeetCode】Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.题解:给定一个链表有序的转成平衡BST,关键这里我用额外空间MLE所以应该在链表上直接进行,最优化的应该是设计两个指针,一个slow一个fast用来遍历中间节点,然后左右子...原创 2018-10-19 22:22:06 · 177 阅读 · 0 评论 -
【LeetCode】Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n?Example:Input: 3Output: 5Explanation:Given n = 3, there are a total of 5 unique BST's: 1 ...原创 2018-10-22 15:29:12 · 139 阅读 · 0 评论 -
【LeetCode】Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.Example:Input: "25525511135"Output: ["255.255.11.135", "255.255.111.35"]题解:递归判断是不是合法ip...原创 2018-10-22 20:33:24 · 212 阅读 · 0 评论 -
【LeetCode】Reverse Linked List II
Reverse 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->NULL, m = 2, n = 4Output: 1->4->3->2->5-...原创 2018-10-22 21:39:00 · 158 阅读 · 0 评论 -
【LeetCode】Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.Example 1:Input: [ [1,1,1], [1,0,1], [1,1,1]]Output: [ [1,0,1], [0,0,0], [1,0,1]]E...原创 2018-11-07 00:17:32 · 174 阅读 · 0 评论 -
【LeetCode】Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.Example:Input: n = 4, k = 2Output:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]题解:三种解法,最直接...原创 2018-11-07 01:17:24 · 208 阅读 · 0 评论 -
【LeetCode】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 n...原创 2018-11-07 01:26:43 · 222 阅读 · 0 评论 -
【LeetCode】Longest Substring with At Most Two Distinct Characters
给定一个字符串 s ,找出至多包含两个不同字符的最长子串 t 。示例 1:输入: "eceba"输出: 3解释: t 是 "ece",长度为3。示例 2:输入: "ccaabbb"输出: 5解释: t 是 "aabbb",长度为5。来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/longest-substrin...原创 2019-07-01 00:47:50 · 281 阅读 · 0 评论 -
【LeetCode】904. Fruit Into Baskets
In a row of trees, the i-th tree produces fruit with type tree[i].You start at any tree of your choice, then repeatedly perform the following steps:Add one piece of fruit from this tree to your b...原创 2019-07-01 00:53:42 · 221 阅读 · 0 评论 -
【LeetCode】239. Sliding Window Maximum
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window ...原创 2019-07-01 01:00:58 · 147 阅读 · 0 评论 -
【LeetCode】Container With Most Water
Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two...原创 2019-07-01 01:14:32 · 201 阅读 · 0 评论 -
【LeetCode】Largest Rectangle in Histogram
Givennnon-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 ea...原创 2019-07-01 01:31:04 · 306 阅读 · 1 评论 -
【LeetCode】42. Trapping Rain Water
Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.The above elevation map is represented by array...原创 2019-07-01 01:41:29 · 227 阅读 · 0 评论 -
【LeetCode】901. Online Stock Span
Write a classStockSpannerwhich collects daily price quotes for some stock, and returns thespanof that stock's price for the current day.The span of the stock's price todayis defined as the maxi...原创 2019-07-01 01:55:54 · 245 阅读 · 0 评论 -
【LeetCode】739. Daily Temperatures
Given a list of daily temperaturesT, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which...原创 2019-07-01 02:00:17 · 187 阅读 · 0 评论 -
【LeetCode】Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).Example:Input: S = "ADOBECODEBANC", T = "ABC"Output: "BANC"Note:I...原创 2019-07-01 00:26:19 · 169 阅读 · 0 评论 -
【LeetCode】713. Subarray Product Less Than K
Your are given an array of positive integers nums.Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k.Example 1:Input: nums ...原创 2019-07-01 00:17:05 · 199 阅读 · 0 评论 -
【LeetCode】862. Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.If there is no non-empty subarray with sum at least K, return -1. Example 1:Input: A = [1], K = 1Ou...原创 2019-06-30 23:57:03 · 202 阅读 · 0 评论 -
【LeetCode】Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of t...原创 2018-11-07 01:45:20 · 159 阅读 · 0 评论 -
【LeetCode】212. Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board.Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horiz...原创 2018-11-10 19:00:07 · 167 阅读 · 0 评论 -
【LeetCode】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&quo原创 2018-11-18 23:35:15 · 189 阅读 · 0 评论 -
【LeetCode】SQL语句题解(一)
编写一个 SQL 查询,查找Person 表中所有重复的电子邮箱。示例:+----+---------+| Id | Email |+----+---------+| 1 | a@b.com || 2 | c@d.com || 3 | a@b.com |+----+---------+根据以上输入,你的查询应返回以下结果:+---------+| Emai...原创 2019-06-17 22:02:27 · 402 阅读 · 0 评论 -
【LeetCode】813. 最大平均值和的分组
我们将给定的数组A分成K个相邻的非空子数组 ,我们的分数由每个子数组内的平均值的总和构成。计算我们所能得到的最大分数是多少。注意我们必须使用 A 数组中的每一个数进行分组,并且分数不一定需要是整数。示例:输入: A = [9,1,2,3,9]K = 3输出: 20解释: A 的最优分组是[9], [1, 2, 3], [9]. 得到的分数是 9 + (1 + 2 + ...原创 2019-06-30 20:22:24 · 282 阅读 · 0 评论 -
【LeetCode】1040. 移动石子直到连续 II
在一个长度无限的数轴上,第 i 颗石子的位置为stones[i]。如果一颗石子的位置最小/最大,那么该石子被称作端点石子。每个回合,你可以将一颗端点石子拿起并移动到一个未占用的位置,使得该石子不再是一颗端点石子。值得注意的是,如果石子像stones = [1,2,5]这样,你将无法移动位于位置 5 的端点石子,因为无论将它移动到任何位置(例如 0 或 3),该石子都仍然会是端点石子...原创 2019-06-30 22:30:39 · 764 阅读 · 0 评论