Leetcode
17. Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.
|
|
Although the above answer is in lexicographical order, your answer could be in any order you want.
|
|
93. Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
Example:
|
|
|
|
131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
Example:
given s = “aab”, return
|
|
|
|
46. Permutations
Given a collection of distinct numbers, return all possible permutations.
Example:
[1,2,3] have the following permutations:
|
|
|
|
47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
Example:
[1,1,2] have the following unique permutations:
|
|
|
|
77. Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 … n.
Example:
If n = 4 and k = 2, a solution is:
|
|
|
|
回溯剪枝
|
|
39. Combination Sum
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
Example:
given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
|
|
|
|
剪枝
|
|
40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
Example:
given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
A solution set is:
|
|
|
|
剪枝
|
|
216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
Example:
|
|
|
|
|
|
剪枝
|
|
78. Subsets
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note:
The solution set must not contain duplicate subsets.
Example:
|
|
|
|
90. Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note:
The solution set must not contain duplicate subsets.
Example:
|
|
|
|
401. Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).
|
|
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 neighboring. The same letter cell may not be used more than once.
example:
|
|
|
|
200. 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 all four edges of the grid are all surrounded by water.
Example 1:
|
|
Example 2:
|
|
|
|
130. Surrounded Regions
Given a 2D board containing ‘X’ and ‘O’ (the letter O), capture all regions surrounded by ‘X’.
A region is captured by flipping all ‘O’s into ‘X’s in that surrounded region.
解题思路:
这道题使用DFS
Example:
|
|
|
|
StackOverflow做法
当测试数量大,并且包含大量的’O’,递归调用breakSurrounded太深,爆
|
|
优化做法
|
|
417. Pacific Atlantic Water Flow
Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the “Pacific ocean” touches the left and top edges of the matrix and the “Atlantic ocean” touches the right and bottom edges.
Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.
Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.
Note:
- he order of returned grid coordinates does not matter.
- Both m and n are less than 150.
|
|
解题思路:
东南方向和西北方向的深度优先搜索
|
|
51. N-Queens
Example:
|
|
|
|
52. N-Queens II
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
|
|
37. 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.
解题思路:
很脑瓜子疼的一道题目,
想不明白
|
|