基本情形: F(0) = 0;
F(1) = 1;
递归规则: F(n) = F(n - 1) + F(n - 2)
// Fibonacci calculation
int Fibonacci (int n) {
//Base Case
if(n == 0) return 0;
if(n == 1) return 1;
//recursion rule
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
f(5)
= f(4) + f(3)
= f(3) + f(2) f(2) + {f(1) = 1}
= f(2) + {f(1) = 1} {f(1)=1} + {f(0)=0} {f(1)=1} + {f(0)=0}
={f(1)=1} + {f(0)=0} 1
2
3
4
1
2
3
4
function solveProblem() {
divide into small problems;
...
...
solveProblem();
}
大多数编程语言通过允许函数调用自身来支持递归.
function tellStory() {
mountain();
temple();
oldMonk();
tellStory();
}
从前有个山,
山里有个庙,
庙里有个老和尚,
老和尚在给小和尚讲故事,
从前有个山...
讲故事
function factorial(int n) {
int m = factorial(n-1);
return m*n;
}
n的阶乘
function factorial(int n) {
if (n==1) return 1;
int m = factorial(n-1);
return m*n;
}
n的阶乘
function factorial(int n) {
if (n==1) return 1;
return n * factorial(n-1);
}
function fibo(int n) {
if (n == 1) return 1;
if (n == 2) return 1;
return fibo(n-1) + fibo(n-2);
}
第n个斐波那契数
1, 1, 2, 3, 5, 8, 13, 21, 34, ...
一栋楼有n个楼梯, 一个人一次可以爬1或者2登楼梯, 那么这个人有多少种不同的方式可以爬到顶层.
There is a building with n stairs, one person can climb 1 or 2 stairs at one time, then how many different ways there are for this person to climb to the top.
public static int climb(int n) {
if (n <= 2) return n;
return climb(n-1) + climb(n-2);
}
一栋楼有n个楼梯, 一个人一次可以爬1或者2登楼梯, 那么这个人有多少种不同的方式可以爬到顶层..
public static void climb(int n, String preWay) {
if (n == 1) {
System.out.println(preWay + " 1");
return;
}
if (n == 2) {
System.out.println(preWay + " 2");
System.out.println(preWay + " 1 1");
return;
}
String preWay1 = preWay + " 1";
climb(n-1, preWay1);
String preWay2 = preWay + " 2";
climb(n-2, preWay2);
}
把所有盘子从A移动到C一共需要多少步.
public static int hanoi(int n) {
if (n == 1) return 1;
return hanoi(n-1) + 1 + hanoi(n-1);
}
把所有盘子从A移动到C一共需要多少步.
public static int hanoi(int n) {
if (n == 1) return 1;
return 2 * hanoi(n-1) + 1;
}
请输出把所有盘子从A移动到C的所有步骤.
public static void hanoi(int n, char source, char spare, char target) {
if (n == 1) {
System.out.println("Move " + source + " to " + target);
return;
}
hanoi(n-1, source, target, spare);
System.out.println("Move " + source + " to " + target);
hanoi(n-1, spare, source, target);
}
请输出把所有盘子从A移动到C的所有步骤.
public static void hanoi(int n, char source, char spare, char target) {
if (n > 0) {
hanoi(n-1, source, target, spare);
System.out.println("Move " + source + " to " + target);
hanoi(n-1, spare, source, target);
}
}
计算链表中所有节点之和.
计算链表中所有节点之和.
public static int sum(ListNode head) {
if (head == null) return 0;
return head.val + sum(head.next);
}
把链表中所有的节点值等于给定值val的节点全部删除.
public static ListNode removeElements(ListNode head, int val) {
if (head == null) return null;
ListNode newHead = removeElements(head.next, val);
if (head.val == val) {
return newHead;
} else {
head.next = newHead;
return head;
}
}
1
2
3
4
1
2
3
4
1
4
3
2
1
2
3
4
1
4
3
2
1
4
3
2
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode newHead = reverseList(head.next);
head.next.next = head;
head.next = null;
return newHead;
}给定一个可以装s磅物品的背包, 以及一组重量为 w1, w2, ... wn 的物品. 返回我们是否可以挑选特定物品, 以便它们的总重量为s.
Example Input:
s = 20;
w = [14, 8, 7, 5, 3];
Example Output:
True;
给定一个可以装s磅物品的背包, 以及一组重量为 w1, w2, ... wn 的物品. 返回我们是否可以挑选特定物品, 以便它们的总重量为s.
Example Input:
s = 20;
w = [14, 8, 7, 5, 3];
Example Output:
True;
14
Given a knapsack which can hold s pounds of items, and a set of items with weight w1, w2, ... wn. Return whether we can pick specific items so that their total weight s.
Example Input:
s = 20;
w = [14, 8, 7, 5, 3];
Example Output:
True;
14, 8
Given a knapsack which can hold s pounds of items, and a set of items with weight w1, w2, ... wn. Return whether we can pick specific items so that their total weight s.
Example Input:
s = 20;
w = [14, 8, 7, 5, 3];
Example Output:
True;
14, 8, 7
Given a knapsack which can hold s pounds of items, and a set of items with weight w1, w2, ... wn. Return whether we can pick specific items so that their total weight s.
Example Input:
s = 20;
w = [14, 8, 7, 5, 3];
Example Output:
True;
14, 7, 5
Given a knapsack which can hold s pounds of items, and a set of items with weight w1, w2, ... wn. Return whether we can pick specific items so that their total weight s.
Example Input:
s = 20;
w = [14, 8, 7, 5, 3];
Example Output:
True;
14, 5, 3
Given a knapsack which can hold s pounds of items, and a set of items with weight w1, w2, ... wn. Return whether we can pick specific items so that their total weight s.
Example Input:
s = 20;
w = [14, 8, 7, 5, 3];
Example Output:
True;
14, 5, 3, 8
Given a knapsack which can hold s pounds of items, and a set of items with weight w1, w2, ... wn. Return whether we can pick specific items so that their total weight s.
Example Input:
s = 20;
w = [14, 8, 7, 5, 3];
Example Output:
True;
8, 7
Given a knapsack which can hold s pounds of items, and a set of items with weight w1, w2, ... wn. Return whether we can pick specific items so that their total weight s.
Example Input:
s = 20;
w = [14, 8, 7, 5, 3];
Example Output:
True;
8, 7, 5
给定一个可以装s磅物品的背包, 以及一组重量为 w1, w2, ... wn 的物品. 返回我们是否可以挑选特定物品, 以便它们的总重量为s.
Example Input:
s = 20;
w = [14, 8, 7, 5, 3];
Example Output:
True;
给定一个物品, 只有两种选择:
给定一个可以装s磅物品的背包, 以及一组重量为 w1, w2, ... wn 的物品. 返回我们是否可以挑选特定物品, 以便它们的总重量为s.
public static boolean knapsack(int s, int[] weights, int index) {
if (s == 0) {
return true;
}
if (s < 0 || index >= weights.length) {
return false;
}
return knapsack(s - weights[index], weights, index+1) ||
knapsack(s, weights, index+1);
}给定一个迷宫以及一个起点和一个终点, 判断是否可以到达目标.
示例输入:
起点: (0, 0); 终点:(5, 5);
Maze: char[][] = {
{'.', 'X', '.', '.', '.', 'X'},
{'.', '.', '.', 'X', '.', 'X'},
{'X', 'X', '.', 'X', '.', '.'},
{'.', 'X', 'X', 'X', '.', 'X'},
{'.', '.', '.', '.', '.', 'X'},
{'.', '.', '.', '.', '.', '.'}
}
示例输出: True
给定一个迷宫以及一个起点和一个终点, 判断是否可以到达目标.
示例输入:
起点: (0, 0); 终点:(5, 5);
Maze: char[][] = {
{'.', 'X', '.', '.', '.', 'X'},
{'.', '.', '.', 'X', '.', 'X'},
{'X', 'X', '.', 'X', '.', '.'},
{'.', 'X', 'X', 'X', '.', 'X'},
{'.', '.', '.', '.', '.', 'X'},
{'.', '.', '.', '.', '.', '.'}
}
示例输出: True
给定一个迷宫以及一个起点和一个终点, 判断是否可以到达目标.
给定一个迷宫以及一个起点和一个终点, 判断是否可以到达目标.
给定一个迷宫以及一个起点和一个终点, 判断是否可以到达目标.
给定一个迷宫以及一个起点和一个终点, 判断是否可以到达目标.
public static boolean solveMaze(char[][] maze,
int startX, int startY, int targetX, int targetY,
boolean[][] visited) {
if (startX < 0 || startX >= maze.length ||
startY < 0 || startY >= maze[0].length ||
maze[startX][startY] == 'X' || visited[startX][startY]) {
return false;
}
if (startX == targetX && startY == targetY) {
return true;
}
visited[startX][startY] = true;
if (solveMaze(maze, startX + 1, startY, targetX, targetY, visited) ||
solveMaze(maze, startX, startY + 1, targetX, targetY, visited) ||
solveMaze(maze, startX - 1, startY, targetX, targetY, visited) ||
solveMaze(maze, startX, startY - 1, targetX, targetY, visited)) {
return true;
}
return false;
}给定一个迷宫以及一个起点和一个终点, 判断是否可以到达目标.
public static boolean solveMaze(char[][] maze,
int startX, int startY, int targetX, int targetY) {
if (startX < 0 || startX >= maze.length ||
startY < 0 || startY >= maze[0].length ||
maze[startX][startY] == 'X') {
return false;
}
if (startX == targetX && startY == targetY) {
return true;
}
maze[startX][startY] = 'X';
if (solveMaze(maze, startX + 1, startY, targetX, targetY) ||
solveMaze(maze, startX, startY + 1, targetX, targetY) ||
solveMaze(maze, startX - 1, startY, targetX, targetY) ||
solveMaze(maze, startX, startY - 1, targetX, targetY)) {
return true;
}
return false;
}Given a maze and a start point and a target point, return whether the target can be reached.
public static boolean solveMaze(char[][] maze,
int startX, int startY, int targetX, int targetY) {
if (startX < 0 || startX >= maze.length ||
startY < 0 || startY >= maze[0].length ||
maze[startX][startY] == 'X') {
return false;
}
if (startX == targetX && startY == targetY) {
return true;
}
maze[startX][startY] = 'X';
int[] dx = {1, 0, -1, 0};
int[] dy = {0, 1, 0, -1};
for (int i = 0; i < 4; i++) {
if (solveMaze(maze, startX + dx[i], startY + dy[i], targetX, targetY)) {
return true;
}
}
return false;
}Given a maze and a start point and a target point, return whether the target can be reached.
public static boolean solveMaze(char[][] maze,
int startX, int startY, int targetX, int targetY) {
if (startX == targetX && startY == targetY) {
return true;
}
maze[startX][startY] = 'X';
int[] dx = {1, 0, -1, 0};
int[] dy = {0, 1, 0, -1};
for (int i = 0; i < 4; i++) {
int newX = startX + dx[i], newY = startY + dy[i];
if (newX < 0 || newX >= maze.length || newY < 0 || newY >= maze.length ||
maze[newX][newY] == 'X') {
continue;
}
if (solveMaze(maze, newX, newY, targetX, targetY)) {
return true;
}
}
return false;
}Given a maze and a start point and a target point, return whether the target can be reached.
Problem
sub-problem-1
sub-sub-problem
sub-sub-sub-problem
base-problem
Problem
sub-problem-1
base-problem
sub-problem-2
sub-problem-3
sub-sub-problem-2
sub-sub-sub-problem
sub-sub-problem-1
...
...
...
Problem
sub-problem-1
sub-sub-problem
sub-sub-sub-problem
base-problem
Problem
sub-problem-1
base-problem
sub-problem-2
sub-problem-3
sub-sub-problem-2
sub-sub-sub-problem
sub-sub-problem-1
...
...
...
Problem
sub-problem-1
sub-sub-problem
sub-sub-sub-problem
base-problem
Problem
sub-problem-1
base-problem
sub-problem-2
sub-problem-3
sub-sub-problem-2
sub-sub-sub-problem
sub-sub-problem-1
...
...
...
Problem
sub-problem-1
sub-sub-problem
sub-sub-sub-problem
base-problem
Problem
sub-problem-1
base-problem
sub-problem-2
sub-problem-3
sub-sub-problem-2
sub-sub-sub-problem
sub-sub-problem-1
...
...
...
Problem
sub-problem-1
sub-sub-problem
sub-sub-sub-problem
base-problem
Problem
sub-problem-1
base-problem
sub-problem-2
sub-problem-3
sub-sub-problem-2
sub-sub-sub-problem
sub-sub-problem-1
...
...
...
Problem
sub-problem-1
sub-sub-problem
sub-sub-sub-problem
base-problem
Problem
sub-problem-1
sub-sub-problem
sub-sub-sub-problem
base-problem
Problem
sub-problem-1
base-problem
sub-problem-2
sub-problem-3
sub-sub-problem-2
sub-sub-sub-problem
sub-sub-problem-1
...
...
...
回溯(Backtracking)
public static boolean solveMaze(char[][] maze,
int startX, int startY, int targetX, int targetY,
String path) {
if (startX < 0 || startX >= maze.length ||
startY < 0 || startY >= maze[0].length ||
maze[startX][startY] == 'X') {
return false;
}
if (startX == targetX && startY == targetY) {
System.out.println(path);
return true;
}
maze[startX][startY] = 'X';
int[] dx = {1, 0, -1, 0};
int[] dy = {0, 1, 0, -1};
char[] direction = {'D', 'R', 'U', 'L'};
for (int i = 0; i < 4; i++) {
String newPath = path + direction[i] + " ";
if (solveMaze(maze, startX+dx[i], startY+dy[i], targetX, targetY, newPath)) {
return true;
}
}
return false;
}给定一个迷宫以及一个起点和一个终点, 输出到达终点的路径.
public static boolean solveMaze(char[][] maze,
int startX, int startY, int targetX, int targetY,
ArrayList<Character> path) {
if (startX < 0 || startX >= maze.length ||
startY < 0 || startY >= maze[0].length ||
maze[startX][startY] == 'X') {
return false;
}
if (startX == targetX && startY == targetY) {
return true;
}
maze[startX][startY] = 'X';
int[] dx = {1, 0, -1, 0};
int[] dy = {0, 1, 0, -1};
char[] direction = {'D', 'R', 'U', 'L'};
for (int i = 0; i < 4; i++) {
path.add(direction[i]);
if (solveMaze(maze, startX+dx[i], startY+dy[i], targetX, targetY, path)) {
return true;
}
path.remove(path.size()-1);
}
return false;
}Given a maze and a start point and a target point, return the path to reach the target.
八皇后难题: 将8个国际象棋皇后放在一个8×8的棋盘上, 使得任意两个皇后之间不会相互威胁.
..Q.....
.....Q..
...Q....
.Q......
.......Q
....Q...
......Q.
Q.......
八皇后难题: 将8个国际象棋皇后放在一个8×8的棋盘上, 使得任意两个皇后之间不会相互威胁.
| Q | X | X | X | X | X | X | X |
|---|---|---|---|---|---|---|---|
| X | X | X | X | ||||
| X | X | X | X | ||||
| X | X | X | X | X | |||
| X | X | X | X | X | X | Q | X |
| X | X | X | X | ||||
| X | X | X | |||||
| X | X | X | X |
八皇后难题: 将8个国际象棋皇后放在一个8×8的棋盘上, 使得任意两个皇后之间不会相互威胁.
| X | Q | X | X | X | X | X | X |
|---|---|---|---|---|---|---|---|
| X | X | X | |||||
| X | X | ||||||
| X | X | ||||||
| X | X | ||||||
| X | X | ||||||
| X | X | ||||||
| X |
八皇后难题: 将8个国际象棋皇后放在一个8×8的棋盘上, 使得任意两个皇后之间不会相互威胁.
| X | Q | X | X | X | X | X | X |
|---|---|---|---|---|---|---|---|
| X | X | X | Q | X | X | X | X |
| X | X | X | X | ||||
| X | X | X | X | ||||
| X | X | X | X | X | X | ||
| X | X | X | |||||
| X | X | X | |||||
| X | X |
八皇后难题: 将8个国际象棋皇后放在一个8×8的棋盘上, 使得任意两个皇后之间不会相互威胁.
| X | Q | X | X | X | X | X | X |
|---|---|---|---|---|---|---|---|
| X | X | X | Q | X | X | X | X |
| X | X | X | X | X | Q | X | X |
| X | X | X | X | X | |||
| X | X | X | X | X | X | ||
| X | X | X | X | X | |||
| X | X | X | X | X | |||
| X | X | X | X |
八皇后难题: 将8个国际象棋皇后放在一个8×8的棋盘上, 使得任意两个皇后之间不会相互威胁.
| X | Q | X | X | X | X | X | X |
|---|---|---|---|---|---|---|---|
| X | X | X | Q | X | X | X | X |
| X | X | X | X | X | Q | X | X |
| X | X | X | X | X | X | X | Q |
| X | X | X | X | X | X | ||
| X | X | X | X | X | X | ||
| X | X | X | X | X | X | ||
| X | X | X | X | X |
八皇后难题: 将8个国际象棋皇后放在一个8×8的棋盘上, 使得任意两个皇后之间不会相互威胁.
| X | Q | X | X | X | X | X | X |
|---|---|---|---|---|---|---|---|
| X | X | X | Q | X | X | X | X |
| X | X | X | X | X | Q | X | X |
| X | X | X | X | X | X | X | Q |
| X | X | Q | X | X | X | X | X |
| X | X | X | X | X | X | ||
| X | X | X | X | X | X | X | |
| X | X | X | X | X | X |
八皇后难题: 将8个国际象棋皇后放在一个8×8的棋盘上, 使得任意两个皇后之间不会相互威胁.
| X | Q | X | X | X | X | X | X |
|---|---|---|---|---|---|---|---|
| X | X | X | Q | X | X | X | X |
| X | X | X | X | X | Q | X | X |
| X | X | X | X | X | X | X | Q |
| X | X | Q | X | X | X | X | X |
| Q | X | X | X | X | X | X | X |
| X | X | X | X | X | X | X | |
| X | X | X | X | X | X |
八皇后难题: 将8个国际象棋皇后放在一个8×8的棋盘上, 使得任意两个皇后之间不会相互威胁.
| X | Q | X | X | X | X | X | X |
|---|---|---|---|---|---|---|---|
| X | X | X | Q | X | X | X | X |
| X | X | X | X | X | Q | X | X |
| X | X | X | X | X | X | X | Q |
| X | X | Q | X | X | X | X | X |
| Q | X | X | X | X | X | X | X |
| X | X | X | X | X | X | Q | X |
| X | X | X | X | X | X | X |
八皇后难题: 将8个国际象棋皇后放在一个8×8的棋盘上, 使得任意两个皇后之间不会相互威胁.
| X | Q | X | X | X | X | X | X |
|---|---|---|---|---|---|---|---|
| X | X | X | Q | X | X | X | X |
| X | X | X | X | X | Q | X | X |
| X | X | X | X | X | X | X | Q |
| X | X | Q | X | X | X | X | X |
| Q | X | X | X | X | X | X | X |
| X | X | X | X | X | X | Q | X |
| X | X | X | X | Q | X | X | X |
八皇后难题: 将8个国际象棋皇后放在一个8×8的棋盘上, 使得任意两个皇后之间不会相互威胁.
void putQueens(char[][] board, int row,
List<List<String>> results) {
if (row == board.length) {
saveResult(board, results);
return;
}
for (int col = 0; col < board[0].length; col++) {
if (isLegal(board, row, col)) {
board[row][col] = 'Q';
putQueens(board, row+1, results);
board[row][col] = '.';
}
}
return;
}The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other.
void saveResult(char[][] board,
List<List<String>> results) {
List<String> result = new ArrayList<>();
for (int i = 0; i < board.length; i++) {
result.add(new String(board[i]));
}
results.add(result);
}The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other.
boolean isLegal(char[][] board, int row, int col) {
for (int i = row-1; i >= 0; i--) {
if (board[i][col] == 'Q')
return false;
int tempCol = col - row + i;
if (tempCol >= 0 &&
tempCol < board.length &&
board[i][tempCol] == 'Q')
return false;
tempCol = col + row - i;
if (tempCol >= 0 &&
tempCol < board.length &&
board[i][tempCol] == 'Q')
return false;
}
return true;
}The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other.
public List<List<String>> solveNQueens(int n) {
List<List<String>> results = new ArrayList<>();
char[][] board = new char[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = '.';
}
}
putQueens(board, 0, results);
return results;
}Copyright © 直通硅谷
http://www.zhitongguigu.com/
public List<List<String>> solveNQueens(int n) {
List<List<String>> results = new ArrayList<>();
char[][] board = new char[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = '.';
}
}
putQueens(board, 0, results);
return results;
}
void putQueens(char[][] board, int row, List<List<String>> results) {
if (row == board.length) {
saveResult(board, results);
return;
}
for (int col = 0; col < board[0].length; col++) {
if (isLegal(board, row, col)) {
board[row][col] = 'Q';
putQueens(board, row+1, results);
board[row][col] = '.';
}
}
return;
}
void saveResult(char[][] board, List<List<String>> results) {
List<String> result = new ArrayList<>();
for (int i = 0; i < board.length; i++) {
result.add(new String(board[i]));
}
results.add(result);
}
boolean isLegal(char[][] board, int row, int col) {
for (int i = row-1; i >= 0; i--) {
if (board[i][col] == 'Q')
return false;
int tempCol = col - row + i;
if (tempCol >= 0 && tempCol < board.length && board[i][tempCol] == 'Q')
return false;
tempCol = col + row - i;
if (tempCol >= 0 && tempCol < board.length && board[i][tempCol] == 'Q')
return false;
}
return true;
}The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public List<List<String>> solveNQueens(int n) {
List<List<String>> results = new ArrayList<>();
int[] board = new int[n];
putQueens(board, 0, results);
return results;
}
void putQueens(int[] board, int row, List<List<String>> results) {
if (row == board.length) {
saveResult(board, results);
return;
}
for (int col = 0; col < board.length; col++) {
if (isLegal(board, row, col)) {
board[row] = col;
putQueens(board, row+1, results);
}
}
return;
}
boolean isLegal(int[] board, int row, int col) {
for (int i = row-1; i >= 0; i--) {
if (board[i] == col || Math.abs(board[i] - col) == Math.abs(row - i)) {
return false;
}
}
return true;
}
void saveResult(int[] board, List<List<String>> results) {
List<String> result = new ArrayList<>();
for (int i = 0; i < board.length; i++) {
StringBuilder oneLine = new StringBuilder();
for (int j = 0; j < board.length; j++) {
if (j == board[i]) {
oneLine.append('Q');
} else {
oneLine.append('.');
}
}
result.add(oneLine.toString());
}
results.add(result);
}给定一组不相同的数字, 返回所有可能的排列.
例如,
[1,2,3]有如下排列:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
public List<List<Integer>> permute(int[] nums) {
// Implement this method.
}
Given a collection of distinct numbers, return all possible permutations.
public List<List<Integer>> permute(int[] num) {
List<Integer> numList = new ArrayList<Integer>();
Arrays.sort(num);
for (int i = 0; i < num.length; i++)
numList.add(num[i]);
return permute(new ArrayList<Integer>(), numList);
}
public List<List<Integer>> permute(List<Integer> cur,
List<Integer> num) {
List<List<Integer>> results = new ArrayList<ArrayList<Integer>>();
if (num.size() == 0) {
results.add(cur);
return results;
}
for (int i = 0; i < num.size(); i++) {
ArrayList<Integer> newCur = new ArrayList<Integer>(cur);
newCur.add(num.get(i));
ArrayList<Integer> newNum = new ArrayList<Integer>(num);
newNum.remove(i);
results.addAll(permute(newCur, newNum));
}
return results;
}Given a collection of distinct numbers, return all possible permutations.
public List<List<Integer>> permute(int[] num) {
Arrays.sort(num);
return permute(new ArrayList<Integer>(), num);
}
public List<List<Integer>> permute(ArrayList<Integer> cur, int[] num) {
List<List<Integer>> results = new ArrayList<List<Integer>>();
if (cur.size() == num.length) {
results.add(new ArrayList<Integer>(cur));
return results;
}
for (int i = 0; i < num.length; i++) {
if (cur.contains(num[i])) {
continue;
}
cur.add(num[i]);
results.addAll(permute(cur, num));
cur.remove(cur.size() - 1);
}
return results;
}Given a collection of distinct numbers, return all possible permutations.
public List<List<Integer>> permute(int[] num) {
List<List<Integer>> results = new ArrayList<List<Integer>>();
Arrays.sort(num);
permute(results, new ArrayList<Integer>(), num);
return results;
}
public void permute(List<List<Integer>> results,
ArrayList<Integer> cur, int[] num) {
if (cur.size() == num.length) {
results.add(new ArrayList<Integer>(cur));
return;
}
for (int i = 0; i < num.length; i++) {
if (cur.contains(num[i])) {
continue;
}
cur.add(num[i]);
permute(results, cur, num);
cur.remove(cur.size() - 1);
}
}给定一组不相同的数字, 返回所有可能的组合.
例如,
[2, 6, 8]有如下的所有组合:
[], [2], [6], [8], [2, 6], [2, 8], [6, 8], [2, 6, 8].
public List<List<Integer>> combine(int[] nums) {
// Implement this method.
}
Given a collection of distinct numbers, return all possible combinations.
public List<List<Integer>> combine(int[] nums) {
List<List<Integer>> results = new ArrayList<>();
combination(results, nums, 0, new ArrayList<Integer>());
return results;
}
public void combination(List<List<Integer>> results,
int[] nums, int index,
ArrayList<Integer> items) {
if (index == nums.length) {
results.add(items);
return;
}
ArrayList<Integer> newItems1 = new ArrayList<Integer>(items);
combination(results, nums, index+1, newItems1);
ArrayList<Integer> newItems2 = new ArrayList<Integer>(items);
newItems2.add(nums[index]);
combination(results, nums, index+1, newItems2);
}Given a collection of distinct numbers, return all possible combinations.
public List<List<Integer>> combine(int[] nums) {
List<List<Integer>> results = new ArrayList<>();
combination(results, nums, 0, new ArrayList<Integer>());
return results;
}
public void combination(List<List<Integer>> results,
int[] nums, int index,
ArrayList<Integer> items) {
if (index == nums.length) {
results.add(new ArrayList<Integer>(items));
return;
}
combination(results, nums, index+1, items);
items.add(nums[index]);
combination(results, nums, index+1, items);
items.remove(items.size()-1);
}Given a collection of distinct numbers, return all possible combinations.
public List<List<Integer>> combine(int[] nums) {
List<List<Integer>> results = new ArrayList<>();
combination(results, nums, 0, new ArrayList<Integer>());
return results;
}
public void combination(List<List<Integer>> results,
int[] nums, int index,
ArrayList<Integer> items) {
if (index == nums.length) {
results.add(new ArrayList<Integer>(items));
return;
}
for (int i = index; i < nums.length; i++) {
items.add(nums[i]);
combination(results, nums, i+1, items);
items.remove(items.size()-1);
}
}Missing Empty Set
Given a collection of distinct numbers, return all possible combinations.
public List<List<Integer>> combine(int[] nums) {
List<List<Integer>> results = new ArrayList<>();
combination(results, nums, 0, new ArrayList<Integer>());
return results;
}
public void combination(List<List<Integer>> results,
int[] nums, int index,
ArrayList<Integer> items) {
if (index == nums.length) {
results.add(new ArrayList<Integer>(items));
return;
}
for (int i = index; i <= nums.length; i++) {
if (i == nums.length) {
combination(results, nums, i, items);
return;
}
items.add(nums[i]);
combination(results, nums, i+1, items);
items.remove(items.size()-1);
}
}Given a collection of distinct numbers, return all possible combinations.
public List<List<Integer>> combine(int[] nums) {
List<List<Integer>> results = new ArrayList<>();
combination(results, nums, 0, new ArrayList<Integer>());
return results;
}
public void combination(List<List<Integer>> results,
int[] nums, int index,
ArrayList<Integer> items) {
if (index == nums.length) {
results.add(new ArrayList<Integer>(items));
return;
}
for (int i = index; i < nums.length; i++) {
items.add(nums[i]);
combination(results, nums, i+1, items);
items.remove(items.size()-1);
}
combination(results, nums, nums.length, items);
}Recursion
subproblem
-> problem
subproblem-1 ||
subproblem-2 ||
subproblem-N
-> problem
subproblem-1 &&
subproblem-2 &&
subproblem-N
-> problem
Factorial,
Sum of LinkedList,
Remove Linked List Element, etc.
Maze,
Sudoku,
Most DFS problems, etc.
Knapsack
Permutations,
Combinations,
Eight Queen,
kSum, etc.
Level N
Level N-1
Level N-1
Level N-1
public static boolean solveMaze(char[][] maze,
int startX, int startY, int targetX, int targetY, String path) {
...
for (int i = 0; i < 4; i++) {
String newPath = path + direction[i] + " ";
if (solveMaze(maze, startX+dx[i], startY+dy[i],
targetX, targetY, newPath)) {
return true;
}
}
return false;
}parameters
parameters
parameters
这些参数(parameters)互不影响.
Level N
Level N-1
Level N-1
Level N-1
public static boolean solveMaze(char[][] maze,
int startX, int startY, int targetX, int targetY,
ArrayList<Character> path) {
...
for (int i = 0; i < 4; i++) {
path.add(direction[i]);
if (solveMaze(maze, startX+dx[i], startY+dy[i],
targetX, targetY, path)) {
return true;
}
path.remove(path.size()-1);
}
return false;
}Shared Parameters