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

原创 算法概论8.15 最大公共子图
题目:证明如下问题是NP-完全的:输入:两个图G1 = (V1,E1),G2 = (V2,E2);预算b。输出:两个节点集合V1‘⊆V1,V2’⊆V2,将它们移除后,将在两图中分别留下至少b个结点,且图的剩余部分完全相同。解答:我们已知求给定图的最大独立集问题是NP完全问题,由此可以将最大独立集问题归约到此问题。假设给定的G1 = (V,E),G2 =
2017-01-13 21:59:52
2509
原创 376. Wiggle Subsequence 类别:动态规划 难度:medium
题目:A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be e
2017-01-08 22:24:10
297
原创 279. Perfect Squares 类别:medium 难度:medium
题目:Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4;
2017-01-08 22:21:34
291
原创 64. Minimum Path Sum 类别:动态规划 难度:medium
题目: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.思路:转移方程:dp[i][j] = min(grid[m -
2017-01-08 22:19:40
242
原创 416. Partition Equal Subset Sum 类别:动态规划 难度:medium
题目:Given 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:Each of
2017-01-08 22:17:56
840
原创 198. House Robber 类别:动态规划 难度:easy
题目: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 adjac
2017-01-08 22:15:00
246
原创 70. Climbing Stairs 类别:动态规划 难度:easy
题目: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?思路:类似斐波那契序列。
2017-01-08 22:12:56
240
原创 62. Unique Paths 类别:动态规划 难度:medium
题目: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 r
2017-01-08 22:11:13
251
原创 343. Integer Break 类别:动态规划 难度:medium
题目:Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.For example, given n
2017-01-08 22:08:49
311
原创 357. Count Numbers with Unique Digits 类别:动态规划 难度:medium
题目:Given 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 total numbers in the range of 0 ≤ x [11,
2017-01-08 22:06:14
245
原创 442. Find All Duplicates in an Array 难度:medium
题目:Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements that appear twice in this array.Could you do it w
2017-01-08 21:34:23
224
原创 463. Island Perimeter 难度:easy
题目:You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is co
2017-01-08 21:09:27
193
原创 448. Find All Numbers Disappeared in an Array 难度:easy
题目: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 a
2017-01-08 21:04:03
171
原创 419. Battleships in a Board 难度:medium
题目:Given an 2D board, count how many different battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules:Y
2017-01-08 20:58:52
168
原创 461. Hamming Distance 难度:easy
题目:The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.Note:
2017-01-08 20:53:45
168
原创 118. Pascal's Triangle 难度:easy
题目:Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]思路:每一层的第i个位置,等于
2017-01-07 12:44:24
182
原创 75. Sort Colors 难度:medium
题目: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
2017-01-07 12:41:46
198
原创 66. Plus One 难度:easy
题目: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.
2017-01-07 12:32:09
225
原创 27. Remove Element 难度:easy
题目: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 me
2017-01-07 12:01:07
154
原创 367. Valid Perfect Square 难度:medium
题目:Given a positive integer num, write a function which returns True if num is a perfect square else False.Note: Do not use any built-in library function such as sqrt.Example 1:Input:
2017-01-07 11:47:57
176
原创 334. Increasing Triplet Subsequence 难度:medium
题目:Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.Formally the function should:Return true if there exists i, j, k such that
2017-01-07 11:38:07
158
原创 77. Combinations 难度:medium
题目:Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3],
2017-01-07 10:47:16
266
原创 235. Lowest Common Ancestor of a Binary Search Tree 难度:easy
题目:Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is def
2017-01-06 21:51:10
242
原创 263. Ugly Number 难度:easy
题目:Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not
2017-01-06 21:35:41
206
原创 191. Number of 1 Bits 难度:easy
题目:Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representati
2017-01-06 21:31:12
192
原创 153. Find Minimum 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).Find the minimum element.思路:用二分法
2017-01-06 21:26:24
161
原创 199. Binary Tree Right Side View 难度:medium
题目:Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tree
2017-01-06 21:17:13
186
原创 202. Happy Number 难度:easy
题目:Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of th
2017-01-06 20:14:12
193
原创 386. Lexicographical Numbers 难度:medium
题目:Given an integer n, return 1 - n in lexicographical order.For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9].思路:优先将数字乘10;如果数字末位<9,考虑将数字加1。程序:class Solution {p
2017-01-06 20:02:20
221
原创 394. Decode String 难度:medium
题目:Given an encoded string, return it's decoded string.The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note
2017-01-04 20:37:02
220
原创 137. Single Number II 难度:medium
题目:Given an array of integers, every element appears three times except for one. Find that single one.思路:那么对这32位中的每一位做相同的处理,也就是说,逐位把所有的输入加起来,并且看看第i位的和除以3的余数,这个余数就是single numer在第i位的取值。这样就
2017-01-04 19:41:13
150
原创 337. House Robber III 难度:medium
题目: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. Aft
2017-01-04 19:30:13
354
原创 230. Kth Smallest Element in a BST 难度:medium
题目:Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.思路:
2017-01-01 23:31:00
171
原创 318. Maximum Product of Word Lengths 难度:easy
题目:Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lowe
2017-01-01 20:57:01
199
原创 447. Number of Boomerangs 难度:easy
题目:Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k) such that the distance between i andj equals the distance between i and k (the o
2017-01-01 20:21:36
156
原创 454. 4Sum II 难度:medium
题目:Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.To make problem a bit easier, all A, B, C, D ha
2017-01-01 20:02:00
210
原创 378. Kth Smallest Element in a Sorted Matrix 难度:medium
题目: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.Note that it is the kth smallest element in the sorted o
2017-01-01 18:14:28
233
原创 477. Total Hamming Distance 难度:medium
题目: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 o
2017-01-01 17:55:42
195
原创 268. Missing Number 难度:medium
题目: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.思路:等差数列求和,再逐个减去数
2016-12-30 22:25:28
168
原创 350. Intersection of Two Arrays II 难度:easy
题目:Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].Note:Each element in the result should app
2016-12-30 22:19:59
152
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人