- 博客(54)
- 收藏
- 关注

原创 【Algorithm】习题8.14证明
Prove that the following problem is NP-complete: given an undirected graph G=(V,E) and an integer k, return a clique of size k as well as an independent set of size k, provided both exist.
2017-07-01 18:22:35
706
原创 【Sicily】1007. 小黄车
题目描述随着共享经济的兴起,大学城如今到处可见ofo小黄车. 小左现在打算每天都骑小黄车从宿舍去实验室. 假设大学城的地图可以简化为一个有向图,图中有N个地点(节点),用0到N-1进行编号,有些地点之间存在有向的道路(有向边). 小左的宿舍所在地点编号为0,实验室所在地点编号为N-1. 小左希望为连续的M天规划线路,使得每天从宿舍到实验室,都至少会经过一条之前没有走过的道路(有向边). 小左想知道M
2017-06-28 01:07:43
678
原创 【Sicily】1006. 单词变换
题目描述对于两个只含有小写英文字母(’a’-‘z’)的单词word1和word2,你可以对word1进行以下3种操作:1) 插入一个字母; 2) 删除一个字母; 3) 替换一个字母.请计算将word1变换成word2的最少操作数.word1和word2的长度均不大于1000.
2017-06-28 00:52:11
1129
原创 【Sicily】1005. 最大和
题目描述从数列A[0], A[1], A[2], …, A[N-1]中选若干个数,要求相邻的数不能都选,也就是说如果选了A[i], 就不能选A[i-1]和A[i+1]. 求能选出的最大和.1 <= N <= 100000, 1 <= A[i] <= 1000
2017-06-28 00:36:23
381
原创 【Sicily】1004. 无环图
题目描述在图论中,如果一个有向图从任意顶点出发无法经过若干条边回到该点,则这个图是一个有向无环图(Directed Acyclic Graph,DAG). 对于一个n个节点的有向图(节点编号从0到n-1),请判断其是否为有向无环图.图的节点数和边数均不多于100000.
2017-06-28 00:26:14
381
原创 【Sicily】1003. 相连的1
题目描述对于一个01矩阵A,求其中有多少片连成一片的1. 每个1可以和上下左右的1相连.请为下面的Solution类实现解决这一问题的函数countConnectedOnes,函数参数A为给出的01矩阵,A的行数和列数均不大于1000. 函数的返回值是问题的答案
2017-06-28 00:12:23
285
原创 【Sicily】1001. 会议安排
题目描述N个会议要同时举行,参会人数分别为A[0], A[1], …, A[N-1]. 现有M个会议室,会议室可容纳人数分别为B[0], B[1], …, B[M-1]. 当A[i]<=B[j]时,可以把会议i安排在会议室j,每间会议室最多安排一个会议,每个会议最多只能安排一个会议室. 求最多安排多少个会议.
2017-06-27 21:06:34
315
原创 【Sicily】1000. 函数求值
题目描述定义超级和函数F如下: F(0, n) = n,对于所有的正整数n.. F(k, n) = F(k – 1, 1) + F(k – 1, 2) + … + F(k – 1, n),对于所有的正整数k和n. 请实现下面Solution类中计算F(k, n)的函数(1 <= k, n <= 14)
2017-06-27 20:59:55
240
原创 【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], there
2017-04-16 14:59:41
369
原创 【LeetCode】79. Word Search
题目描述Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically
2017-03-23 21:45:40
250
原创 【LeetCode】75. Sort Colors
题目描述Given 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.Here, we will use the integers 0
2017-03-19 21:23:46
189
原创 【LeetCode】74. Search a 2D Matrix
题目描述Write 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 from left to right.The first integer of each ro
2017-03-19 21:18:02
229
原创 【LeetCode】73. Set Matrix Zeroes
题目描述Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.Follow up:Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea
2017-03-19 21:13:56
218
原创 【LeetCode】71. Simplify Path
题目描述Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"解题思路队列。 从index = 1开始遍历string,当遇到’/’时,计算上一个’/’到现在之间中间的内容,存在如下三种情
2017-03-10 22:29:16
220
原创 【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 positive
2017-03-10 22:22:38
181
原创 【LeetCode】69. Sqrt(x)
题目描述Implement int sqrt(int x).Compute and return the square root of x.解题思路二分。 每次找中间值,判断其平方与目标值的大小关系。AC代码class Solution {public: int mySqrt(int x) { int start = 0, end = x; while (
2017-03-10 22:18:32
187
原创 【LeetCode】68. Text Justification
题目描述Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You should pack your words in a greedy approach; that is
2017-03-10 22:14:51
194
原创 【LeetCode】67. Add Binary
题目描述Given two binary strings, return their sum (also a binary string).For example, a = "11" b ="1" Return "100". 解题思路大数相加二进制版。 先把两个数从最小位开始相加,最后把较长那个数与carry相加,直到结束。AC代码class Solution {public: s
2017-03-10 22:06:37
187
原创 【LeetCode】66. Plus One
题目描述Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digits ar
2017-03-09 22:03:58
195
原创 【LeetCode】65. Valid Number
题目描述Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambiguous. You shoul
2017-03-09 22:01:25
291
原创 【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 which minimizes the sum of all numbers along its path.Note: You can only move either down or right at
2017-03-09 21:50:14
161
原创 【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.Fo
2017-03-09 21:44:16
164
原创 【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 b
2017-03-09 18:00:57
182
原创 【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.解题思路先遍历一遍确定链表中元素的个数n,然后k对n求模,因为移动n的倍数还
2017-03-09 17:51:25
184
原创 【LeetCode】60. Permutation Sequence
题目描述The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3):"123""132""213""231""312"
2017-03-09 17:42:09
187
原创 【LeetCode】59. Spiral Matrix II
题目描述Given an integer n, generate a square matrix filled with elements from 1 to n2n^2 in spiral order.For example, Given n = 3, You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [
2017-03-09 17:29:28
163
原创 【LeetCode】58. 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 define
2017-03-09 17:24:23
161
原创 【机试】2015年腾讯后台开发暑期实习生校招一面
题目大意有一栋100层高的大楼,给你两个完全相同的玻璃球,假设从某一层开始丢下玻璃球会摔碎,怎么利用手中的两个玻璃球,用什么最优策略(最少次数)知道这个临界的层是第几层解题思路动态规划。 这个题目首先是关于“最优”的定义,考虑best-worse case最坏情况下最优。 记n层楼2球的问题为Q(n,2),对应的最坏情况最优解为ba(n,2); 对于从第k层处抛球,对应的有如下两种情况:球破
2017-03-08 16:34:41
353
原创 【机试】2017年03月06日阿里测试岗内推在线测试
题目大意给定一组数字,判断该组数字可否被分为总和相同的四部分,其中每个部分中间有一个间隔点,抛弃不计。 例如: 对于{5, 2, 1, 1, 1, 1, 4, 3, 7, 2, 7} , 可以划分为{{5,2},1,{1,1,1,4},3,{7},2,{7} 。 其中每个部分的总和都是7。 分隔点{1,3,2} 不列入计算。解题思路深搜。 计算第一个部分的总和,作为目标值,向下继续搜索。
2017-03-08 16:10:29
692
原创 【LeetCode】56. Merge Intervals
题目描述Given a collection of intervals, merge all overlapping intervals.For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 解题思路先对区间根据第一个元素进行排序。 然后每次判断下一个区间是否和当前区间有交集,若有,则扩展区间。否
2017-03-08 15:51:02
162
原创 【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 if you
2017-03-08 15:47:25
184
原创 【机试】2017年03月07日阿里测试岗内推在线测试
题目大意给定一个只包含数字的字符串,恢复所有的非私网IP地址。 私网地址段:A类:10.0.0.0~10.255.255.255 B类:172.16.0.0~172.31.255.255 C类:192.168.0.0~192.168.255.255解题思路深搜+剪枝 网络IP地址分为四个部分,每个部分的限制条件大同小异,所以可以考虑使用深搜。 注意仔细处理非法地址和私网地址,进行剪枝即可。
2017-03-07 23:21:28
543
原创 【LeetCode】54. Spiral Matrix
题目描述Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example, Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You shou
2017-03-07 14:46:02
205
原创 【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-03-07 14:37:54
180
原创 【LeetCode】50. Pow(x, n)
题目描述Implement pow(x, n). 解题思路快速幂。 注意一下当n为负数的情况。 还有当n为INT_MIN时的边界情况,因为此时-n会溢出。AC代码class Solution {public: double myPow(double x, int n) { bool isMin = false; if (n < 0) {
2017-03-07 14:29:00
139
原创 【LeetCode】 49. 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"]]Note: All inputs wil
2017-03-07 14:26:19
195
原创 【LeetCode】48. Rotate Image
题目描述You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up: Could you do this in-place?解题思路每一次以一圈为单位,在一圈中,每次寻找四个受影响的点。顺时针改变这四个点,就可以实现in-place的旋转了。A
2017-03-07 14:24:00
166
原创 【LeetCode】47. Permutations II
题目描述Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example, [1,1,2] have the following unique permutations:[ [1,1,2], [1,2,1], [2,1,1]]
2017-03-05 14:57:09
260
原创 【LeetCode】46. Permutations
题目描述Given a collection of distinct numbers, return all possible permutations.For example, [1,2,3] have the following permutations:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]解题
2017-03-05 14:52:46
175
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人