自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(63)
  • 收藏
  • 关注

原创 2017.1.10 算法测试题集 - 1006 - 编辑距离问题

Problem给定两个字符串s和t,要求给出由s编辑到t的最小步数。编辑方式有三种,每次只能对一个字母或一个字母位置进行操作:1. 插入,如 snowy->sunnowy2. 删除,如 snowy->snwy3. 替换,如 snowy->snonyExamples= "snowy"t= "sunny"Return 3Algorithm编辑距离问题,是《算法概论》中在动态规划一节举的例

2017-01-13 17:15:19 667

原创 2017.1.10 算法测试题集 - 1005 - 最大间隔和

Problem给定一组整数,要求只能间隔地取数出来,试问取出来的数最大和是多少?ExampleA={1,2,3};Return 4A={3,1,1,2};Return 5;Algorithm典型的动态规划问题。 列状态转移方程:设sum[i]表示前i个数的最大间隔和,要求sum[i],则有两种情况。若选第i个数,则sum[i]=sum[i-2]+nums[i]若不选第i个数,则sum[

2017-01-13 17:13:43 645

原创 2017.1.10 算法测试题集 - 1004 - 无环图

Problem给定一个有向图,判断该有向图是否无环。图的表示是一个整数n和一个pair为元素的向量。整数n表示点的个数ExampleA={1,2,3};B={3,2,4};Return 3A={3,4,3,1};B={1,2,2,6};Return 2;Algorithm会议室安排问题,与会议排期问题类似,可以联想到尝试贪心算法。每次都在还未安排的容量最大的会议室安排尽可能多的参会人数

2017-01-13 17:12:33 649

原创 2017.1.10 算法测试题集 - 1003 - 1的块数

Problem给定一个0-1矩阵matrix,相邻的1视为“同一块”,问矩阵matrix有多少块1?Example[1, 0, 0][0, 1, 0][0, 0, 1]Return 3[1, 0, 0, 1][0, 1, 1, 0][0, 0, 0, 1]Return 4;Algorithm通过深度优先搜索来解决。遍历矩阵,若访问到1,则将这个1以及与它相连的所有1都置为0,同时块数

2017-01-13 17:06:10 553

原创 2017.1.10 算法测试题集 - 1002 - 相等二叉树

Problem给定两棵二叉树,判断这两棵二叉树是否相等。当且仅当两棵树结构相同且对应节点的取值也相同时,两棵二叉树相等。ExampleA: 1 / \ 2 3 / \ \5 6 7B: 1 / \ 2 3 / \ / 5 6 7 C: 1 / \ 2 3 / \ \5 6 7A==C,A!=BAlgorith

2017-01-13 17:03:58 598

原创 2017.1.10 算法测试题集 - 1001 - 会议安排问题

Problem会议安排问题。有一组会议A和一组会议室B,A[i]表示第i个会议的参加人数,B[j]表示第j各会议室最多可以容纳的人数。当且仅当A[i]<=B[j]时,第j个会议室可以用于举办第i个会议。给定数组A和数组B,试问最多可以同时举办多少个会议。ExampleA={1,2,3};B={3,2,4};Return 3A={3,4,3,1};B={1,2,2,6};Return 2;

2017-01-13 16:40:42 4120

原创 算法概论 - 8.19

Problem所谓风筝图是这样的,其顶点数为偶数(比如2n),且其中的n个顶点构成了一个团,剩余的n个顶点则由一条称为“尾巴”的路径连接,尾巴的某个端点与团中的一个顶点相连。给定一个图和目标g,风筝图问题要求该图的一个包含2g各顶点的风筝子图。请证明该问题是NP完全的。Solution可将最大团问题归约到风筝图问题。设有图G(V,E),在G中添加V个新顶点,将这V各新顶点分别与G中各个不同的顶点相连

2017-01-13 16:24:19 605

原创 算法概论 - 8.15

Problem最大公共子图问题。 证明如下问题是NP完全的: 输入:两个图 G1=(V1,E1)G_1=(V_1,E_1) 和 G2=(V2,E2)G_2=(V_2,E_2),预算bb; 输出:两个节点集合V′1⊆V1V_1'\subseteq V_1和V′2⊆V2V_2'\subseteq V_2,它们被移除后,将在两图中分别留下至少bb个节点,且图的剩余部分完全一样。Solution可将最

2017-01-13 16:20:41 395

原创 算法概论 - 8.3

Problem吝啬SAT问题是这样的:给定一组子句(每个子句都是其中文字的析取)和整数k,求一个最多有k各变量为true的满足赋值 —— 如果该赋值存在。证明吝啬SAT是NP完全问题。Solution可以将SAT问题归约到此问题。首先,若得到了吝啬SAT问题的一个解,按照此解对吝啬SAT的每个子句赋值,即可验证这个解是否成立,这个过程是多项式时间的,所以吝啬SAT是NP问题。 下面将SAT问题归约

2017-01-12 17:36:10 359

原创 LeetCode #39 - Combination Sum - Medium

Combination Sum Series Combination Sum I: Combination Sum II: Combination Sum III: http://blog.csdn.net/Arcome/article/details/53758448ProblemGiven a set of candidate numbers (C) (without

2016-12-26 20:05:22 413

原创 LeetCode #90 - Subsets II - Medium

Subsets Series Subsets I: http://blog.csdn.net/arcome/article/details/53888806 Subsets II: ProblemGiven a collection of integers that might contain duplicates, nums, return all possible subsets

2016-12-26 20:01:22 431

原创 LeetCode #78 - Subsets - Medium

ProblemGiven a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.ExampleFor example,If nums = [1,2,3], a solution is:[ [3], [1

2016-12-26 19:55:34 438

原创 LeetCode #210 - Course Schedule II - Medium

Course Schedule Series Course Schedule I: http://blog.csdn.net/Arcome/article/details/53790947 Course Schedule II: ProblemThere are a total of n courses you have to take, labeled from 0 to n -

2016-12-21 20:36:36 439

原创 LeetCode #207 - Course Schedule - Medium

Course Schedule Series Course Schedule I: Course Schedule II: ProblemThere are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example t

2016-12-21 20:33:53 612

原创 LeetCode #329 - Longest Increasing Path in a Matrix - Hard

ProblemGiven 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 outs

2016-12-21 20:29:58 702

原创 LeetCode #77 - Combinations - Medium

ProblemGiven two integers n and k, return all possible combinations of k numbers out of 1 ... n.ExampleFor example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [

2016-12-20 08:12:02 360

原创 LeetCode #89 - Gray Code - Medium

ProblemThe gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence o

2016-12-20 08:07:58 355

原创 LeetCode #46 - Permutations - Medium

Permutations Series Permutations I: Permutations II: ProblemGiven a collection of distinct numbers, return all possible permutations.Example[1,2,3] have the following permutations:[ [1,2,3

2016-12-20 08:03:56 363

原创 LeetCode #22 - Generate Parentheses - Medium

ProblemGiven n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.Examplegiven n = 3, a solution set is:[ "((()))", "(()())", "(())()", "()(())",

2016-12-20 08:00:20 430

原创 LeetCode #216 - Combination Sum III - Medium

Combination Sum Series Combination Sum I: Combination Sum II: Combination Sum III: ProblemFind all possible combinations of k numbers that add up to a number n, given that only numbers f

2016-12-20 07:56:49 646

原创 LeetCode #357 - Count Numbers with Unique Digits - Medium

ProblemGiven a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.Hints:A direct way is to use the backtracking approach.Backtracking should contains three states whi

2016-12-19 18:56:38 274

原创 LeetCode #235 - Lowest Common Ancestor of a Binary Search Tree Easy

ProblemGiven 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 defined be

2016-12-19 18:37:46 295

原创 LeetCode #145 - Binary Tree Postorder Traversal - Hard

相似题目: 二叉树的前序遍历:http://blog.csdn.net/Arcome/article/details/53672912 二叉树的中序遍历:http://blog.csdn.net/arcome/article/details/53672748 二叉树的后序遍历:ProblemGiven a binary tree, return the postorder trav

2016-12-19 15:32:14 334

原创 LeetCode #173 - Binary Search Tree Iterator - Medium

ProblemImplement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next()

2016-12-16 18:07:38 301

原创 LeetCode #437 - Path Sum III - Medium

ProblemYou are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it m

2016-12-16 18:02:27 388

原创 LeetCode #230 - Kth Smallest Element in a BST - Medium

ProblemGiven 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.Follow up:What if the BST i

2016-12-16 17:59:43 269

原创 LeetCode #144 - Binary Tree Preorder Traversal - Medium

相似题目: 二叉树的前序遍历:http://blog.csdn.net/arcome/article/details/53672748 二叉树的中序遍历: 二叉树的后序遍历:ProblemGiven a binary tree, return the preorder traversal of its nodes' values.Note: Recursive solution

2016-12-15 17:12:39 349

原创 LeetCode #94 - Binary Tree Inorder Traversal - Medium

ProblemGiven a binary tree, return the inorder traversal of its nodes' values.Note: Recursive solution is trivial, could you do it iteratively?ExampleGiven binary tree [1,null,2,3], 1 \ 2

2016-12-15 17:04:51 238

原创 LeetCode #404 - Sum of Left Leaves - Easy

ProblemFind the sum of all left leaves in a given binary tree.Example 3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.

2016-12-15 16:58:09 251

原创 LeetCode #226 - Invert Binary Tree - Easy

ProblemInvert a binary tree.Example 4 / \ 2 7 / \ / \1 3 6 9-> 4 / \ 7 2 / \ / \9 6 3 1Algorithm整理一下题意:翻转一棵二叉树。首先想到的当然是递归解法。对每个节点,都翻转左右子树。代码如下。//递归版本,用时0

2016-12-15 16:46:46 285

原创 LeetCode #168 - Excel Sheet Column Title - Easy

ProblemGiven a positive integer, return its corresponding column title as appear in an Excel sheet.Example1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB Algorithm整理一下题意:给定一个正整数,要求返回其在Excel中代表的列号。

2016-12-14 21:20:29 332

原创 LeetCode #7 - Reverse Integer - Easy

ProblemReverse digits of an integer.ExampleExample1: x = 123, return 321Example2: x = -123, return -321Algorithm整理一下题意:给定一个整数,要求返回其数字倒序后的数。此题比较简单,直接利用除10加模10的方法即可得到倒序。注意当给定的整数大于INT_MAX和小于INT_MIN的情况。

2016-12-14 21:03:53 262

原创 LeetCode #396 - Rotate Function - Easy

ProblemGiven an array of integers A and let n to be its length.Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow:F(k) =

2016-12-14 20:58:25 294

原创 LeetCode #223 - Rectangle Area - Easy

ProblemFind the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.Assume that the total area

2016-12-14 17:41:07 439

原创 LeetCode #279 - Perfect Squares - Medium

ProblemGiven a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.Examplegiven n = 12, return 3 because 12 = 4 + 4 + 4; given n =

2016-12-11 11:53:24 360

原创 LeetCode #9 - Palindrome Number - Easy

ProblemDetermine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string, not

2016-12-10 11:03:49 441

原创 LeetCode #231 - Power of Two - Easy

ProblemGiven an integer, write a function to determine if it is a power of two.ExampleInput: 2Output: trueAlgorithm整理一下题意:给定一个整数,判断其是否是2的幂。简单判断即可。注意特殊情况,如负数,0,1。代码如下。class Solution {public: boo

2016-12-07 20:39:16 279

原创 LeetCode #319 - Bulb Switcher - Medium

ProblemThere are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s off or turn

2016-12-07 20:34:04 481

原创 LeetCode #13 - Roman to Integer - Easy

ProblemGiven a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.ExampleInput: IOutput: 1Algorithm整理一下题意:给定一个罗马数字,要求返回其整数值解题首先要了解罗马数字与阿拉伯数字的对应规则。 基本

2016-12-07 19:46:46 425

原创 LeetCode #268 - Missing Number - Medium

ProblemGiven an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.Note:Your algorithm should run in linear runtime complexity. Could yo

2016-12-06 15:29:10 288

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除