- 博客(46)
- 收藏
- 关注
原创 【LeetCode】75. Sort Colors
Difficulty: MediumGiven an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Her
2017-01-14 23:28:37 329
原创 【LeetCode】41. First Missing Positive
Difficulty: HardGiven an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n)
2017-01-14 23:22:27 309
原创 《算法概论》习题8.20
问题描述在一个无向图G=(V,E)中,我们称D⊆V为一个占优集,是指每个v∈V都属于D或与D中一个节点为邻。在占优集问题中,输入为一个图和预算b,目标是求图中的一个规模不超过b的控制集——如果该集存在,证明该问题是NP-完全的。解答可以将顶点覆盖问题归约到占优集问题。若要在图G(V, E)中求得不大于b的一个顶点覆盖,可以先对图G 做一个预处理:对每条边(u,v)∈E ,添
2017-01-14 23:09:59 435
原创 《算法概论》习题8.19
问题描述所谓风筝图是这样的,其顶点数为偶数,如2n,且其中的n个顶点构成了一个团,剩余的n个顶点则由一条称为尾巴的路径连接,尾巴的某个端点与团的一个顶点相连。给定一个图和目标g,风筝图问题要求图的一个包含2g个顶点的风筝子图。请证明该问题是NP-完全。解答可以将团问题归约到 KITE 问题。若要求图G(V, E)的最大团,可以在图G 中添加V 个新顶点,
2017-01-14 22:55:26 402
原创 《算法概论》习题8.12
k-生成树问题:给定K大于等于2输入:无向图G=(V,E),输出:G的一个生成树,其中保证树上任意点度不超过k,或告知不存在这样的生成树。 a:k-生成树问题是一个搜索问题。 b:k-生成树问题是NP-完全的。(提示:由k=2开始,考虑与Rudrata路径问题的关联)a.显然 k -SPANNING TREE 问题是可在多项式时间内验证的,因此是搜索问题.
2017-01-14 20:56:36 334
原创 【LeetCode】3. Longest Substring Without Repeating Characters
Difficulty: MediumGiven a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Gi
2017-01-14 16:55:32 416
原创 【LeetCode】122. Best Time to Buy and Sell Stock II
Difficulty: MediumSay you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transact
2017-01-14 16:45:58 318
原创 【LeetCode】45. Jump Game II
Difficulty: HardGiven an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that po
2017-01-14 16:39:09 275
原创 【LeetCode】69. Sqrt(x)
Difficulty: MediumImplement int sqrt(int x).Compute and return the square root of x.Subscribe to see which companies asked this question二分查找,时间复杂度O(logn)class Solution {publ
2017-01-14 16:35:44 259
原创 【LeetCode】50. Pow(x, n)
Difficulty: MediumImplement pow(x, n).Subscribe to see which companies asked this question分析二分法,x^n=x^(n/2) * x^(n/2) * x^(n%2),时间复杂度O(logn)class Solution {public:
2017-01-14 16:21:49 312
原创 【LeetCode】131. Palindrome Partitioning
131. Palindrome PartitioningTotal Accepted: 84076Total Submissions: 272482Difficulty: MediumContributors: AdminGiven a string s, partition s such that every substring of the pa
2017-01-14 00:00:48 257
原创 【LeetCode】130. Surrounded Regions
Difficulty: MediumGiven a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded reg
2017-01-13 23:53:14 288
原创 【LeetCode】6. ZigZag Conversion
6. ZigZag Conversion Add to ListQuestionEditorial Solution My SubmissionsTotal Accepted: 129677Total Submissions: 500836Difficulty: EasyContributors: AdminThe
2017-01-09 08:57:04 292
原创 【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-01-08 17:00:02 328
原创 【LeetCode】241. Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.Example 1
2017-01-08 16:55:40 292
原创 【LeetCode】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
2017-01-08 16:52:50 270
原创 【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-01-08 16:46:57 810
原创 【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] ha
2017-01-08 10:42:39 318
原创 【LeetCode】55. Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine i
2017-01-08 10:30:12 236
原创 【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.
2017-01-08 09:58:31 198
原创 【LeetCode】241. Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.Difficulty: M
2017-01-07 20:05:18 313
原创 【LeetCode】258. Add Digits
Difficulty: EasyGiven a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1
2017-01-07 20:02:10 237
原创 【LeetCode】268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.Note:Your algorithm shoul
2017-01-07 19:56:44 184
原创 【LeetCode】273. Integer to English Words
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.For example,123 -> "One Hundred Twenty Three"12345 -> "Twelve Thousand Th
2017-01-07 19:53:03 262
原创 【LeetCode】10. Regular Expression Matching
Difficulty: HardImplement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cov
2017-01-07 19:49:41 329
原创 【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 =
2017-01-07 19:43:09 214
原创 【LeetCode】104. Maximum Depth of Binary Tree
Difficulty: EasyGiven a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node./** * Defin
2017-01-07 19:12:17 192
原创 【LeetCode】100. Same Tree
Difficulty: EasyGiven two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the sam
2017-01-07 18:53:35 211
原创 【LeetCode】21. Merge Two Sorted Lists
Difficulty: EasyMerge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.思路比较简单,不断比较取出来的2个节点的大小,把小的加入新的链表,时间
2017-01-07 12:05:18 209
原创 【LeetCode】19. Remove Nth Node From End of List
Difficulty: EasyGiven 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
2017-01-07 10:49:38 245
原创 《算法概论》习题8.8
题目在精确的4SAT(EXACT 4SAT)问题中,输入为一组子句,每个子句都是恰好4个文字的析取,且每个变量最多在子句中出现一次。目标是求它的满足赋值——如果该赋值存在。证明精确的4SAT是NP-完全问题。证明为了证明4-SAT问题是NP-complete,需要证明4-SAT问题是NP的和NP-hard。先证明4-SAT问题是NP的。我们可以写出一个非
2016-11-22 19:30:41 781
原创 【LeetCode】120. Triangle
Difficulty: MediumGiven a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[
2016-11-01 21:48:39 200
原创 【LeetCode】322. Coin Change
Difficulty: MediumYou are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount.
2016-11-01 20:35:07 277
原创 【LeetCode】96. Unique Binary Search Trees
Difficulty: MediumGiven n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3
2016-11-01 19:56:17 190
原创 【LeetCode】392. Is Subsequence
Difficulty: MediumGiven 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 and t. t is potentially a very long (
2016-11-01 17:36:41 303
原创 【LeetCode】416. Partition Equal Subset Sum
Difficulty: MediumGiven a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.Note
2016-11-01 16:48:53 229
原创 【LeetCode】303. Range Sum Query - Immutable
Difficulty: EasyGiven an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sum
2016-10-25 21:49:12 248
原创 【LeetCode】70. Climbing Stairs
Difficulty: EasyYou 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?Su
2016-10-25 21:12:03 254
原创 【LeetCode】121. Best Time to Buy and Sell Stock
Difficulty: EasySay you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell
2016-10-20 15:41:43 228
原创 【LeetCode】300. Longest Increasing Subsequence
Difficulty: MediumGiven 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
2016-10-20 12:09:14 347
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人