- 博客(34)
- 收藏
- 关注
原创 《算法概论》 习题8.9
题目:在碰撞集问题中,给定一组集合{S1, S2, ..., Sn}和预算b,我们希望求一个所有Si都相交并且规模不超过b的集合H,当然,前提是这样的集合确实存在。换句话说,我们希望对所有的i满足H∩Si≠∅。请证明该问题是NP-完全的。证明:利用归约证明,将顶点覆盖问题归约到碰撞集问题。对于图G(V, E),设图中每一条边都对应一个集合Si,集合中的元素即边的两个顶点,如
2017-01-15 14:00:12 340
原创 《算法概论》 习题8.8
题目:在精确的4SAT问题中,输入为一组子句,每个子句都是恰好4个文字的析取,且每个变量最多在每个子句中出现一次。目标是求它的满足赋值——如果该赋值存在。证明精确的4SAT是NP-完全问题。
2017-01-15 12:49:48 320
原创 《算法概论》习题8.3
题目:吝啬SAT问题是这样的:给定一组子句(每个子句都是其中文字的析取)和整数k,求一个最多有k个变量为true的满足赋值——如果该赋值存在。证明吝啬SAT是NP-完全问题。证明:可以利用归约的方法证明。现在证明SAT问题可以在一个多项式时间内归约到吝啬SAT问题,那么就可以说明吝啬SAT问题是一个NP-完全问题。SAT问题是一个NP完全问题,当在SAT问题的条件中加上k=n,
2017-01-15 10:47:41 293
原创 Leetcode179: Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be ve
2016-11-26 18:49:23 309
原创 Leetcode71: Simplify Path
Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"题意:给出一个unix的完全路径,然后简化这个路径。主要的就是 /.. 表示返回上级目录, /. 表示在当前
2016-11-25 17:23:32 167
原创 Leetcode136: Single Number
Single Number 系列三道题目,解题思路都相似。136. Single NumberGiven an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear run
2016-11-15 20:12:43 164
原创 Leetcode38: Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as
2016-11-15 00:35:46 152
原创 Leetcode329: Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path.From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside
2016-11-07 23:36:37 142
原创 Leetcode397: Integer Replacement
Given a positive integer n and you can do operations as follow:If n is even, replace n with n/2.If n is odd, you can replace n with either n + 1 or n - 1.What is the minimum number o
2016-11-06 01:06:08 234
原创 Leetcode409: Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.This is case sensitive, for example "Aa" is not con
2016-11-05 22:59:40 152
原创 Leetcode187: Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.Wri
2016-11-04 00:24:50 209
原创 Leetcode139: Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet"
2016-11-03 00:49:52 180
原创 Leetcode22: Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "(()())", "(())()", "()(())
2016-10-30 22:03:49 203
原创 Leetcode35: 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.
2016-10-29 22:06:30 154
原创 Leetcode52: N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.题意:依然是N皇后问题,只不过在上次的八皇后问题中,是需要输出每个棋盘的布局的,而这一题只需要输出所有的解的数目即可。
2016-10-27 19:25:01 139
原创 Leetocde51: 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.
2016-10-27 01:36:32 163
原创 Leetcode63: 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
2016-10-17 23:23:07 171
原创 Leetcode62: 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
2016-10-17 00:59:21 166
原创 Leetcode49: Group Anagrams
Given an array of strings, group anagrams together.For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return:[ ["ate", "eat","tea"], ["nat","tan"], ["bat"]]题意:给出一个字
2016-10-02 23:53:37 343
原创 Leetcode58: Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is def
2016-10-01 13:38:31 152
原创 Leetcode5: Longest Palindromic Substring
5. Longest Palindromic SubstringGiven a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromi
2016-09-25 16:42:36 154
原创 Leetcode104: Maximum Depth of Binary Tree
Given 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.题意:求一个数的最大深度,也就是从根节点到叶子节点的路径中拥有的最多的节
2016-09-23 23:48:02 213
原创 Leetcode357: Count Numbers with Unique Digits
357. Count Numbers with Unique DigitsGiven a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x n.Example:Given n = 2, return 91. (The answer should be the
2016-09-23 21:44:04 172
原创 Leetcode373: Find K Pairs with Smallest Sums
373. Find K Pairs with Smallest SumsYou are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.Define a pair (u,v) which consists of one element from
2016-09-23 21:43:12 259 1
原创 Leetcode392: Is Subsequence
392. Is SubsequenceGiven 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
2016-09-23 21:42:12 147
原创 Leetcode378: Kth Smallest Element in a Sorted Matrix
378. Kth Smallest Element in a Sorted Matrix Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
2016-09-23 21:41:15 257
原创 Leetcode240: Search a 2D Matrix II
240. Search a 2D Matrix IIWrite 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
2016-09-23 21:40:00 259
原创 Leetcode53: Maximum Subarray
53. Maximum SubarrayFind 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 c
2016-09-23 21:38:50 197
原创 Leetcode215: Kth Largest Element in an Array
215. Kth Largest Element in an ArrayFind 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 e
2016-09-23 21:37:50 291
原创 Leetcode169: Majority Element
Leetcode169:Majority ElementGiven 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-e
2016-09-23 21:36:47 207
原创 Leetcode9: Palindrome Number
Leetcode9:Palindrome NumberDetermine whether an integer is a palindrome. Do this without extra space.题意:判断一个数是不是回文数,并且不能使用额外的空间。解答:负数因为有负号所以不是回文数,所以在开始的时候就判断一下。因为不能使用额外的空间,所以这题的思路可
2016-09-23 21:34:57 169
原创 Leetcode205: Isomorphic Strings
205. Isomorphic StringsGiven two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of
2016-09-23 21:33:23 249
原创 Leetcode28: Implement strStr()
28. Implement strStr()Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.题意:在一个字符串haystack中寻找有没有子字符串needle,要是有的话,返回第一次出现的位置,否则返回
2016-09-23 21:31:35 181
原创 Leetcode371: Sum of Two Integers
371. Sum of Two IntegersCalculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example:Given a = 1 and b = 2, return 3.题意:计算两个整数的值,不能用加法和减法。解答:一
2016-09-23 21:11:51 136
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人