
DFS
文章平均质量分 77
dyllanzhou
这个作者很懒,什么都没留下…
展开
-
[Leetcode]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:"((()))", "(()())", "(())()", "()(())", "()()()"原创 2015-09-24 17:26:35 · 306 阅读 · 0 评论 -
[Leetcode]Paint House
There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the ho原创 2015-09-14 12:36:51 · 633 阅读 · 0 评论 -
[Leetcode] Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If the原创 2015-09-22 12:51:54 · 267 阅读 · 0 评论 -
[Leetcode]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], and [2,1,1].原创 2015-10-28 00:12:08 · 416 阅读 · 0 评论 -
[Leetcode]Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree./** * Definition for a binary tree node. * struct Tre原创 2015-10-26 23:47:17 · 257 阅读 · 0 评论 -
[Leetcode]Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST./** * Definition for singly-linked list. * struct ListNode { * int val; * Lis原创 2015-10-12 22:03:00 · 285 阅读 · 0 评论 -
[Leetcode]Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string原创 2015-10-27 21:34:48 · 346 阅读 · 0 评论 -
[Leetcode]N-Queens
The n-queens puzzle is the problem of placing n queens on ann×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.Ea原创 2015-12-15 16:55:08 · 268 阅读 · 0 评论 -
[Leetcode]N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.class Solution {public: /*algorithm: DFS */ vectorgetNex原创 2015-12-15 17:22:33 · 295 阅读 · 0 评论 -
[Leetcode]Additive Number
Additive number is a string whose digits can form additive sequence.A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the s原创 2015-12-02 23:22:40 · 594 阅读 · 0 评论 -
[Leetcode]Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences ofT in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none原创 2016-01-08 10:43:05 · 310 阅读 · 0 评论 -
[Leetcode]Remove Invalid Parentheses
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.Note: The input string may contain letters other than the parentheses ( and).原创 2016-01-14 20:14:43 · 345 阅读 · 0 评论 -
[Leetcode]Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solution.A sudoku puzzle.原创 2016-03-10 19:42:23 · 304 阅读 · 0 评论 -
[Leetcode]Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST./** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left;原创 2015-09-22 19:18:30 · 370 阅读 · 0 评论 -
[Leetcode]Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \原创 2015-10-26 23:17:23 · 254 阅读 · 0 评论 -
[Leetcode]Permutations
Given a collection of 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], and [3,2,1].class Solution原创 2015-10-01 16:22:40 · 225 阅读 · 0 评论 -
[Leetcode]Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inC where the candidate numbers sums to T.Each number in C may only be used once in the combinatio原创 2015-11-04 15:45:14 · 227 阅读 · 0 评论 -
[Leetcode]Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of itsneighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use # as a separator for each原创 2015-11-05 21:25:37 · 272 阅读 · 0 评论 -
[Leetcode]Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume原创 2015-09-01 12:38:40 · 292 阅读 · 0 评论 -
[Leetcode]Combinations
Given two integers n and k, return all possible combinations ofk 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], [1,4],]原创 2015-10-08 18:06:27 · 294 阅读 · 0 评论 -
[Leetcode]Word Break
Given a string s and a dictionary of words dict, determine ifs can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet",原创 2015-11-08 16:30:34 · 328 阅读 · 0 评论 -
[Leetcode]Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree./** * Definition for a binary tree node. * struct Tr原创 2015-10-24 14:44:54 · 291 阅读 · 0 评论 -
[Leetcode]Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab",Return 1 s原创 2015-10-25 18:13:55 · 286 阅读 · 0 评论 -
[Leetcode]Paint House II
There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two ad原创 2015-09-15 17:28:51 · 539 阅读 · 0 评论 -
[Leetcode]Perfect Squares
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 because12 = 4 + 4 + 4; given n = 13,原创 2015-09-10 18:00:28 · 477 阅读 · 0 评论 -
[Leetcode]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原创 2015-10-26 19:54:14 · 367 阅读 · 0 评论 -
[Leetcode]Graph Valid Tree
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.For example:Given n =原创 2015-10-27 16:41:59 · 877 阅读 · 0 评论 -
[Leetcode]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 o原创 2016-03-14 14:49:11 · 356 阅读 · 0 评论