LeetCode
good-destiny
这个作者很懒,什么都没留下…
展开
-
829. Consecutive Numbers Sum
Given a positive integerN, how many ways can we write it as a sum ofconsecutive positive integers?Example 1:Input: 5Output: 2Explanation: 5 = 5 = 2 + 3Example 2:Input: 9Output: 3Explan...原创 2020-03-06 21:54:57 · 120 阅读 · 0 评论 -
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 new symbol.Find out how m...原创 2020-02-05 15:45:08 · 140 阅读 · 0 评论 -
560. Subarray Sum Equals K
Given an array of integers and an integerk, you need to find the total number of continuous subarrays whose sum equals tok.Example 1:Input:nums = [1,1,1], k = 2Output: 2Note:The length of...原创 2020-02-05 14:36:28 · 111 阅读 · 0 评论 -
394. Decode String
Given an encoded string, return its decoded string.The encoding rule is:k[encoded_string], where theencoded_stringinside the square brackets is being repeated exactlyktimes. Note thatkis guar...原创 2020-02-05 11:57:00 · 144 阅读 · 0 评论 -
347. Top K Frequent Elements
Given a non-empty array of integers, return thekmost frequent elements.Example 1:Input: nums = [1,1,1,2,2,3], k = 2Output: [1,2]Example 2:Input: nums = [1], k = 1Output: [1]Note:You...原创 2020-02-04 22:31:24 · 88 阅读 · 0 评论 -
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 only one parent house. After a tour...原创 2020-02-04 21:53:26 · 103 阅读 · 0 评论 -
312. Burst Balloons
Givennballoons, indexed from0ton-1. Each balloon is painted with a number on it represented by arraynums. You are asked to burst all the balloons. If the you burst ballooniyou will getnums[le...原创 2020-02-04 20:46:05 · 116 阅读 · 0 评论 -
239. Sliding Window Maximum
Given an arraynums, there is a sliding window of sizekwhich is moving from the very left of the array to the very right. You can only see theknumbers in the window. Each time the sliding window m...原创 2020-02-04 14:26:17 · 91 阅读 · 0 评论 -
238. Product of Array Except Self
Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Example:Input: [1,2,3,4]Output: [...原创 2020-02-03 23:07:22 · 72 阅读 · 0 评论 -
207. Course Schedule
There are a total ofncourses you have to take, labeled from0ton-1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair...原创 2020-02-03 22:46:12 · 120 阅读 · 0 评论 -
146. LRU Cache
Design and implement a data structure forLeast Recently Used (LRU) cache. It should support the following operations:getandput.get(key)- Get the value (will always be positive) of the key if th...原创 2020-02-03 19:47:24 · 115 阅读 · 0 评论 -
124. Binary Tree Maximum Path Sum
Given anon-emptybinary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connectio...原创 2020-02-03 15:10:35 · 89 阅读 · 0 评论 -
85. 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",...原创 2020-02-02 14:33:42 · 158 阅读 · 0 评论 -
84. 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 eac...原创 2020-02-01 21:09:40 · 98 阅读 · 0 评论 -
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:I...原创 2020-02-01 16:27:39 · 105 阅读 · 0 评论 -
49. Group Anagrams
Given an array of strings, group anagrams together.Example:Input: ["eat", "tea", "tan", "ate", "nat", "bat"],Output:[ ["ate","eat","tea"], ["nat","tan"], ["bat"]]思路:这道题要将相同元素的字符串放在相同的数...原创 2020-02-01 11:59:10 · 111 阅读 · 0 评论 -
32. Longest Valid Parentheses
Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.Example 1:Input: "(()"Output: 2Explanation: The longest valid...原创 2020-01-31 17:16:56 · 101 阅读 · 0 评论 -
1143. Longest Common Subsequence
Given two stringstext1andtext2, return the length of their longest common subsequence.Asubsequenceof a string is a new string generated from the original string with some characters(can be none...原创 2019-12-04 19:03:03 · 205 阅读 · 0 评论 -
300. Longest Increasing Subsequence
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,10...原创 2019-12-04 18:06:05 · 94 阅读 · 0 评论 -
98. 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 keysless thanthe node's key. The ...原创 2019-07-28 16:31:19 · 90 阅读 · 0 评论 -
208. Implement Trie (Prefix Tree)
Implement a trie withinsert,search, andstartsWithmethods.Example:Trie trie = new Trie();trie.insert("apple");trie.search("apple"); // returns truetrie.search("app"); // returns fals...原创 2019-08-01 21:08:56 · 106 阅读 · 0 评论 -
413. Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.For example, these are arithmetic sequence:1,...原创 2018-03-26 21:38:53 · 138 阅读 · 0 评论 -
322. Coin Change
You are given coins of different denominations and a total amount of moneyamount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money ...原创 2019-08-13 23:30:19 · 88 阅读 · 0 评论 -
279. Perfect Squares
Given a positive integern, find the least number of perfect square numbers (for example,1, 4, 9, 16, ...) which sum ton.Example 1:Input: n = 12Output: 3 Explanation: 12 = 4 + 4 + 4.Exampl...原创 2019-08-12 23:37:44 · 98 阅读 · 0 评论 -
49. Group Anagrams
Given an array of strings, group anagrams together.Example:Input: ["eat", "tea", "tan", "ate", "nat", "bat"],Output:[ ["ate","eat","tea"], ["nat","tan"], ["bat"]]Note:All inputs will...原创 2019-07-18 21:42:46 · 120 阅读 · 0 评论 -
142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.To represent a cycle in the given linked list, we use an integerposwhich represents the position (0-i...原创 2019-09-19 23:16:12 · 99 阅读 · 0 评论 -
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 thedefinition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p...原创 2019-08-07 22:19:51 · 92 阅读 · 0 评论 -
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]...原创 2019-07-22 21:52:14 · 71 阅读 · 0 评论 -
150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are +,-,*,/ 。Each operand may be an integer or another expression.Note:Division between two integers sh...原创 2019-07-30 23:10:24 · 119 阅读 · 0 评论 -
20. Valid Parentheses
Given a string containing just the characters'(',')','{','}','['and']', determine if the input string is valid.An input string is valid if:Open brackets must be closed by the same type of b...原创 2019-06-25 23:23:20 · 95 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array
Given a sorted arraynums, remove the duplicatesin-placesuch that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this bymodifyi...原创 2019-06-25 23:20:26 · 93 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
Given a string, find the length of thelongest substringwithout repeating characters.Example 1:Input: "abcabcbb"Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2:...原创 2019-03-17 17:05:32 · 94 阅读 · 0 评论 -
169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element a...原创 2018-10-06 09:35:19 · 309 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.(简言之:找...原创 2018-10-05 23:35:17 · 151 阅读 · 0 评论 -
477. Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Now your job is to find the total Hamming distance between all pairs of the given n...原创 2018-10-05 17:35:20 · 184 阅读 · 0 评论 -
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.Example:For num = 5 you sho...原创 2018-03-26 20:44:31 · 117 阅读 · 0 评论 -
53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the ...原创 2018-02-24 11:53:37 · 161 阅读 · 0 评论 -
144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree [1,null,2,3], 1 \ 2 / 3return [1,2,3].Note: Recursive solution is trivial, coul...原创 2018-02-17 21:39:41 · 143 阅读 · 0 评论 -
94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree [1,null,2,3], 1 \ 2 / 3return [1,3,2].Note: Recursive solution is trivial, could...原创 2018-02-17 21:36:11 · 153 阅读 · 0 评论 -
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-02-26 11:17:23 · 186 阅读 · 0 评论