LeetCode
李小先
stay hungry,stay foolish
展开
-
LeetCode 34. Search for a Range
Given an array of integers 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 ta转载 2017-09-06 10:31:47 · 175 阅读 · 0 评论 -
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree. 解题思路:这题和105的解题思路类似,主要区别在于:将先序遍历换成后续后,根节点将是每部分孩子节点的最后一原创 2017-05-19 17:29:18 · 145 阅读 · 0 评论 -
LeetCode 62. Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bo原创 2017-06-05 10:59:09 · 155 阅读 · 0 评论 -
LeetCode 6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I I转载 2017-06-04 17:25:23 · 175 阅读 · 0 评论 -
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree. 解题思路:根据先序遍历和中序遍历的特点,先序遍历数组中的第一个数一定是树根的值,找到这个值在中序遍历中的位置原创 2017-05-19 16:53:39 · 157 阅读 · 0 评论 -
LeetCode 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原创 2017-05-30 00:02:52 · 216 阅读 · 0 评论 -
LeetCode 143. Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to {1,4原创 2017-04-27 00:05:27 · 168 阅读 · 0 评论 -
LeetCode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3转载 2017-05-07 21:33:40 · 142 阅读 · 0 评论 -
LeetCode 99. Recover Binary Search Tree Add to List
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a转载 2017-05-07 20:13:59 · 162 阅读 · 0 评论 -
LeetCode 234. Palindrome Linked List Add to List
Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?解题思路:计算链表总节点长度,然后将前一半节点利用头插法反转过来,形成一个新的链表,再与后面的一半节点数的链表进行比较。注意:当原链表总节点为奇数时,将中间原创 2017-04-25 14:35:28 · 191 阅读 · 0 评论 -
LeetCode 98. Validate Binary Search Tree Add to List
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原创 2017-05-07 17:21:46 · 157 阅读 · 0 评论 -
LeetCode 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.解题思路:树的形状与树节点的值无关,只与数组递增排序后的长度有关。为了得到一棵平衡二叉树,只需要保持每个子树的根节点是该子树中序遍历得到的数组的中间一个数就可以了,所以在解题过程中,得到当前排序数组的元原创 2017-05-07 14:28:28 · 163 阅读 · 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.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1转载 2017-05-07 13:59:20 · 153 阅读 · 0 评论 -
LeetCode 19. Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the l原创 2017-04-25 10:22:47 · 149 阅读 · 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原创 2017-06-05 12:37:11 · 178 阅读 · 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 positi转载 2017-06-05 10:26:25 · 200 阅读 · 0 评论 -
LeetCode 121. Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),原创 2017-06-05 13:18:10 · 194 阅读 · 0 评论 -
LeetCode 516. Longest Palindromic Subsequence
转载自 点击打开链接class Solution {public: int longestPalindromeSubseq(string s) { if(s.length()==0){ return 0; }else{ int len=s.length(); int **dp;转载 2017-06-27 11:03:15 · 199 阅读 · 0 评论 -
算法概论习题解答8.22
8.22 问题描述: 在任务调度,常常会用到图。其中节点应用于任务,任务i到j的有向边表示i是j的先期条件。这样的图描述了调度问题中的任务先后关系(约束) 。显然,一个调度是可行的当且仅当该图无环;如果调度不可行,我们需要使其无环所需的最小约束数量。给定一个有向图G =(V,E),子集E'⊆E称为一个反馈弧集,反馈弧集是指将其移除后,将使得G无环。反馈弧集(FAS):给定一个有向图原创 2017-07-05 22:03:23 · 701 阅读 · 0 评论 -
LeetCode 109. 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。解题思路:对于一个给定个数的有序列表,其形成的平衡二叉树的形状只与它的个数有关,与具体的数值无关。首先可以先计算链表中节点的总个数,然后将中间节点构造成子树根节点,递归构造左右子原创 2017-06-27 16:34:46 · 221 阅读 · 0 评论 -
LeetCode 300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], theref原创 2017-06-11 20:46:26 · 162 阅读 · 0 评论 -
LeetCode 64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right whichminimizes the sum of all numbers along its path.Note: You can only move either down or right at原创 2017-06-11 16:08:37 · 193 阅读 · 0 评论 -
LeetCode 213. House Robber II
Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time原创 2017-06-11 15:44:21 · 210 阅读 · 0 评论 -
LeetCode 72. Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convertword1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:转载 2017-06-21 20:13:00 · 167 阅读 · 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.You may原创 2017-02-23 20:03:52 · 295 阅读 · 0 评论 -
LeetCode 215. Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, return 5.转载 2017-06-07 14:24:01 · 173 阅读 · 0 评论 -
LeetCode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appearsmore than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element alw转载 2017-06-07 10:55:37 · 164 阅读 · 0 评论 -
LeetCode 435. Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.Note:You may assume the interval's end point is alway转载 2017-06-16 15:58:26 · 258 阅读 · 0 评论 -
LeetCode 63. Unique Paths II
Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the grid.原创 2017-06-05 15:44:03 · 154 阅读 · 0 评论 -
LeetCode 455. Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a coo原创 2017-06-16 09:56:16 · 160 阅读 · 0 评论 -
LeetCode 392. Is Subsequence
Given a string s and a string t, check if s is subsequence of t.You may assume that there is only lower case English letters in both s andt. t is potentially a very long (length ~= 500,000) stri原创 2017-06-16 09:50:41 · 168 阅读 · 0 评论 -
LeetCode 61. Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.public class Solution { publi原创 2017-04-25 10:07:16 · 139 阅读 · 0 评论 -
155. Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Ge转载 2017-05-25 21:31:50 · 196 阅读 · 0 评论 -
LeetCode 27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory.The order of elements can be changed. It doesn't matter wha原创 2017-03-15 21:37:58 · 156 阅读 · 0 评论 -
LeetCode 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, suc原创 2017-03-07 14:40:41 · 153 阅读 · 0 评论 -
LeetCode 9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.原创 2017-03-02 16:20:04 · 204 阅读 · 0 评论 -
LeetCode 5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example: Input: "babad"Output: "bab"Note: "aba" is also a valid answer.Example: Input: "cbbd"Output: "bb"原创 2017-03-01 14:29:39 · 164 阅读 · 0 评论 -
LeetCode 4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).Example 1:nums1 = [1, 3]nums2 = [2]The median is 2.0Example 2:nums1 = [1, 2]原创 2017-02-28 11:23:23 · 191 阅读 · 0 评论 -
LeetCode 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "b", with the length of 1.Given "pwwkew", the answer is "wke",原创 2017-02-28 10:37:14 · 479 阅读 · 0 评论 -
LeetCode 8. String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.Notes: It is intended for this problem to be specifi原创 2017-02-26 16:30:46 · 276 阅读 · 0 评论