![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
涛涛酱
北邮研究僧在读
展开
-
【LeetCode 445】 Add Two Numbers II
题目描述两个链表,表示数字,高位在首部。求两个数字之和。思路用栈。或者反转链表。代码/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * Lis原创 2020-09-07 20:52:39 · 95 阅读 · 0 评论 -
【LeetCode 99】 Recover Binary Search Tree
题目描述二叉搜索树有两个节点的值被交换了,不改变树的结构,恢复二叉树。思路根据中序遍历顺序,找出不符合的节点。代码/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} *原创 2020-09-07 17:31:30 · 108 阅读 · 0 评论 -
【LeetCode 114】Flatten Binary Tree to Linked List
题目描述Given a binary tree, flatten it to a linked list in-place.For example, given the following tree: 1 / \ 2 5 / \ \3 4 6The flattened tree should look like:1 \ 2 \ 3 \ 4 \ 5 \原创 2020-07-13 13:10:50 · 152 阅读 · 0 评论 -
【面试题】Continuous Subarray Sum 变形
题目描述给你一个数组和一个target,找出和是target整数倍的连续子串。思路要找出所有这样的子串。哈希表的val改成是所有的sum%target=n时,n对应的所有位置。然后每次遇见n时,哈希表中val中的所有left位置和当前位置之间组成合法子串。代码class Solution {public: vector<vector<int>> checkSubarraySum(vector<int>& nums, int k) {原创 2020-07-08 10:03:38 · 205 阅读 · 0 评论 -
【LeetCode 523】 Continuous Subarray Sum
题目描述Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to a multiple of k, that is, sums up to n*k where n is also an integer.Example 1:Input: [23原创 2020-07-08 09:58:04 · 121 阅读 · 0 评论 -
【LeetCode 1371】 Find the Longest Substring Containing Vowels in Even Counts
题目描述Given the string s, return the size of the longest substring containing each vowel an even number of times. That is, ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’ must appear an even number of times.Example 1:Input: s = "eleetminicoworoep"Output: 13Explanation: The原创 2020-06-15 00:16:52 · 255 阅读 · 0 评论 -
【LeetCode 1248】 Count Number of Nice Subarrays
题目描述Given an array of integers nums and an integer k. A subarray is called nice if there are k odd numbers on it.Return the number of nice sub-arrays.Example 1:Input: nums = [1,1,2,1,1], k = 3Output: 2Explanation: The only sub-arrays with 3 odd numbe原创 2020-06-14 22:34:16 · 168 阅读 · 0 评论 -
【LeetCode 560】 Subarray Sum Equals K
题目描述Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.Example 1:Input:nums = [1,1,1], k = 2Output: 2Constraints:The length of the array is in range [1, 20,000].The range of原创 2020-06-09 11:44:21 · 193 阅读 · 1 评论 -
【LeetCode 918】 Maximum Sum Circular Subarray
题目描述Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty subarray of C.Here, a circular array means the end of the array connects to the beginning of the array. (Formally, C[i] = A[i] when 0 <= i < A.原创 2020-06-09 10:32:01 · 170 阅读 · 0 评论 -
【LeetCode 994】 Rotting Oranges
题目描述In a given grid, each cell can have one of three values:the value 0 representing an empty cell;the value 1 representing a fresh orange;the value 2 representing a rotten orange.Every minute, any fresh orange that is adjacent (4-directionally) to a原创 2020-06-08 23:35:42 · 124 阅读 · 0 评论 -
【LeetCode 542】 01 Matrix
题目描述Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.The distance between two adjacent cells is 1.Example 1:Input:[[0,0,0], [0,1,0], [0,0,0]]Output:[[0,0,0], [0,1,0], [0,0,0]]Example 2:Input:[[0,0,0], [0原创 2020-06-08 22:41:38 · 127 阅读 · 0 评论 -
【LeetCode 784】 Letter Case Permutation
题目描述Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string. Return a list of all possible strings we could create.Examples:Input: S = "a1b2"Output: ["a1b2", "a1B2", "A1b2", "A1B2"]Input: S原创 2020-06-04 23:16:10 · 121 阅读 · 0 评论 -
【LeetCode 95】 Unique Binary Search Trees II
题目描述Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1 … n.Example:Input: 3Output:[ [1,null,3,2], [3,2,null,1], [3,1,null,null,2], [2,1,3], [1,null,2,null,3]]Explanation:The above output原创 2020-06-04 21:48:12 · 129 阅读 · 0 评论 -
【LeetCode 530】 Minimum Absolute Difference in BST
题目描述Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.Example:Input: 1 \ 3 / 2Output:1Explanation:The minimum absolute difference is 1, which is the differ原创 2020-06-03 11:57:24 · 122 阅读 · 0 评论 -
【LeetCode 33】 Search in Rotated Sorted Array
题目描述Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).You are given a target value to search. If found in the array return its index, otherwise return -1.Y原创 2020-06-02 17:06:05 · 112 阅读 · 0 评论 -
【LeetCode 208】 Implement Trie (Prefix Tree)
题目描述Implement a trie with insert, search, and startsWith methods.Example:Trie trie = new Trie();trie.insert("apple");trie.search("apple"); // returns truetrie.search("app"); // returns falsetrie.startsWith("app"); // returns truetrie.insert(原创 2020-06-02 13:38:06 · 128 阅读 · 0 评论 -
【LeetCode 945】 Minimum Increment to Make Array Unique
题目描述Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1.Return the least number of moves to make every value in A unique.Example 1:Input: [1,2,2]Output: 1Explanation: After 1 move, the array could be [1, 2, 3原创 2020-06-01 23:19:18 · 135 阅读 · 0 评论 -
【LeetCode 887】 Super Egg Drop
题目描述You are given K eggs, and you have access to a building with N floors from 1 to N.Each egg is identical in function, and if an egg breaks, you cannot drop it again.You know that there exists a floor F with 0 <= F <= N such that any egg dropped原创 2020-06-01 19:01:50 · 102 阅读 · 0 评论 -
【LeetCode 300】 Longest Increasing Subsequence Medium
题目描述Given an unsorted array of integers, find the length of longest increasing subsequence.Example:Input: [10,9,2,5,3,7,101,18]Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note:There may be mo原创 2020-05-31 00:58:54 · 113 阅读 · 0 评论 -
【LeetCode 983】 Minimum Cost For Tickets
题目描述In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.Train tickets are sold in 3 different ways:原创 2020-05-30 01:15:15 · 123 阅读 · 0 评论 -
【LeetCode 152】 Maximum Product Subarray
题目描述Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.Example 1:Input: [2,3,-2,4]Output: 6Explanation: [2,3] has the largest product 6.Example 2:Input: [-2,0,-1]原创 2020-05-29 18:27:52 · 106 阅读 · 0 评论 -
【LeetCode 75】 Sort Colors
题目描述Given 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 order red, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red, white原创 2020-05-24 23:52:07 · 112 阅读 · 0 评论 -
【LeetCode 283】Move Zeroes
题目描述Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.Example:Input: [0,1,0,3,12]Output: [1,3,12,0,0]Note:You must do this in-place without making a copy of the array原创 2020-05-24 23:28:15 · 112 阅读 · 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: 4Explanation: The longest consecutive elements sequence is原创 2020-05-24 23:10:17 · 97 阅读 · 0 评论 -
【LeetCode 76】 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:If there is no such window in S that covers all character原创 2020-05-24 22:23:26 · 103 阅读 · 0 评论 -
【LeetCode 114】 Flatten Binary Tree to Linked List
题目描述Given a binary tree, flatten it to a linked list in-place.For example, given the following tree: 1 / \ 2 5 / \ \3 4 6The flattened tree should look like:1 \ 2 \ 3 \ 4 \ 5 \原创 2020-05-22 22:08:51 · 126 阅读 · 0 评论 -
【LeetCode 236】 Lowest Common Ancestor of a Binary Tree
题目描述Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q原创 2020-05-21 23:41:24 · 109 阅读 · 0 评论 -
【LeetCode 113】 Path Sum II
题目描述Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.Note: A leaf is a node with no children.Example:Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \原创 2020-05-21 20:34:04 · 107 阅读 · 0 评论 -
【LeetCode 687】Longest Univalue Path
题目描述Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.The length of path between two nodes is represented by the number of edges between them.Example 1原创 2020-05-21 20:17:02 · 93 阅读 · 0 评论 -
【LeetCode 315】 Count of Smaller Numbers After Self
题目描述You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].Example:Input: [5,2,6,1]Output: [2,1,1,0] Explanation:To th原创 2020-05-20 23:06:37 · 152 阅读 · 0 评论 -
【LeetCode 34】Find First and Last Position of Element in Sorted Array
题目描述Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.Your algorithm’s runtime complexity must be in the order of O(log n).If the target is not found in the array, return [-1, -1].E原创 2020-05-20 22:46:27 · 118 阅读 · 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.Example 1:Input: [1,3,5,6], 5Output: 2Example 2:原创 2020-05-20 22:23:44 · 92 阅读 · 0 评论 -
【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 n-queens puzzle.Each solution contains a distinct board configuration of t原创 2020-05-20 16:45:25 · 103 阅读 · 0 评论 -
【LeetCode 40】 Combination Sum II
题目描述Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.Each number in candidates may only be used once in the combination.Note:All num原创 2020-05-20 13:40:30 · 81 阅读 · 0 评论 -
【LeetCode 473】 Matchsticks to Square
题目描述Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, an原创 2020-05-19 23:26:32 · 131 阅读 · 0 评论 -
【LeetCode 452】 Minimum Number of Arrows to Burst Balloons
题目描述There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it’s horizontal, y-coordinates don’t matter and hence the x-coordinates of st原创 2020-05-19 21:39:11 · 80 阅读 · 0 评论 -
【LeetCode 90】 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]Output:[ [2], [1], [1,2,2], [2,2], [1,2],原创 2020-05-19 20:38:33 · 77 阅读 · 0 评论 -
【LeetCode 78】 Subsets
题目描述Given a set of distinct integers, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.Example:Input: nums = [1,2,3]Output:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]原创 2020-05-19 17:15:58 · 93 阅读 · 0 评论 -
【LeetCode 402】 Remove K Digits
题目描述Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.Note:The length of num is less than 10002 and will be ≥ k.The given num does not contain any leading zero.Exa原创 2020-05-18 22:36:50 · 100 阅读 · 0 评论 -
【LeetCode 376】Wiggle Subsequence
题目描述A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two e原创 2020-05-18 16:19:51 · 84 阅读 · 0 评论