# Android程序员会遇到的算法(part 3 深度优先搜索-回溯backtracking)

1.回溯

2.深度优先

3.暴力搜索

### 1.理解递归

[图片上传失败...(image-1a1a5f-1518421713974)]

``````static void a(){
b();
}

static void b(){
c();
}

static void c(){
System.out.println("methods")
}

public static void main(String[] args){
a();
}

``````

### 2.理解回溯

{1,2,3}
{1,3,2}
{2,1,3}
{2,3,1}
{3,1,2}
{3,2,1}

1开头的集合答案

``````public List<List> getPermutation(List<Integer> list){
List result = new ArrayList<>();
permutateHelper(result,new ArrayList<>(), list, new HashSet<Integer>());
return result;
}

private void permutateHelper(List result, List<Integer> temp,List<Integer> list, HashSet<Integer> visited){
//如果temp，temp答案集合满了，我们加入到最终的结果集合内。
if(temp.size() == list.size()){
}
else{
//直接使用for循环进行对原集合的遍历
for(int i = 0; i< list.size();i++){
//如果没有visit过，进行递归
if(!visited.contains(list.get(i))){
int current = list.get(i);
//进入下一层递归
permutateHelper(result,temp,list,visited);
visited.remove(current);
//这里需要直接remove掉最后一个元素，因为我们的全部的下一层递归已经结束，所以可以把该层的数字删掉，进入for循环的下一个遍历的开始了。这里这个remove的动作就是我们所谓的“回溯”
temp.remove(temp.size()-1);
}
}
}

}

``````

``````private void permutateHelper(List result, List<Integer> temp,List<Integer> list, HashSet<Integer> visited){
if(temp.size() == list.size()){
}
else{
for(int i = 0; i< list.size();i++){
if(!visited.contains(list.get(i))){
int current = list.get(i);
//进入下一层递归,不删除最后一个元素，每次都创建一个新的temp列表
permutateHelper(result,new ArrayList<>(temp),list,visited);
visited.remove(current);
}
}
}

}

``````

### 3. 电话键盘

Screen Shot 2018-02-12 at 3.14.47 PM.png

``````public List<String> letterCombinations(String digits) {
if (digits == null || digits.length() == 0) {
return new ArrayList<>();
}

///先初始化手机按键的数字和字母的关系
String[] one = { "" };
String[] two = { "a", "b", "c" };
String[] three = { "d", "e", "f" };
String[] four = { "g", "h", "i" };
String[] five = { "j", "k", "l" };
String[] six = { "m", "n", "o" };
String[] seven = { "p", "q", "r", "s" };
String[] eight = { "t", "u", "v" };
String[] nine = { "w", "x", "y", "z" };
String[] zero = { "" };

HashMap<Integer, String[]> map = new HashMap<>();
map.put(0, zero);
map.put(1, one);
map.put(2, two);
map.put(3, three);
map.put(4, four);
map.put(5, five);
map.put(6, six);
map.put(7, seven);
map.put(8, eight);
map.put(9, nine);

ArrayList<String> result = new ArrayList<>();
int[] allNum = new int[digits.length()];
for (int i = 0; i < digits.length(); i++) {
allNum[i] = Integer.parseInt(digits.substring(i, i + 1));
}
phoneNumberHelper(result, new StringBuilder(), 0, allNum, map);
return result;

}

private void phoneNumberHelper(ArrayList<String> result, StringBuilder current, int index, int[] allNum,
HashMap<Integer, String[]> com) {
//如果找到一个排列，加入答案中
if (index == allNum.length) {
return;
} else {
//使用for循环，遍历当前该数字的字母集合
String[] possibilities = com.get(allNum[index]);
for (int i = 0; i < possibilities.length; i++) {
phoneNumberHelper(result, current.append(possibilities[i]), index + 1, allNum, com);
//一定要把StringBuilder的最后一位删除掉。
current.deleteCharAt(current.length()-1);
}
}
}

``````

``````
public List<String> letterCombinations(String digits) {
if (digits == null || digits.length() == 0) {
return new ArrayList<>();
}
String[] one = { "" };
String[] two = { "a", "b", "c" };
String[] three = { "d", "e", "f" };
String[] four = { "g", "h", "i" };
String[] five = { "j", "k", "l" };
String[] six = { "m", "n", "o" };
String[] seven = { "p", "q", "r", "s" };
String[] eight = { "t", "u", "v" };
String[] nine = { "w", "x", "y", "z" };
String[] zero = { "" };

HashMap<Integer, String[]> map = new HashMap<>();
map.put(0, zero);
map.put(1, one);
map.put(2, two);
map.put(3, three);
map.put(4, four);
map.put(5, five);
map.put(6, six);
map.put(7, seven);
map.put(8, eight);
map.put(9, nine);

ArrayList<String> result = new ArrayList<>();
int[] allNum = new int[digits.length()];
for (int i = 0; i < digits.length(); i++) {
allNum[i] = Integer.parseInt(digits.substring(i, i + 1));
}
phoneNumberHelper(result, "", 0, allNum, map);
return result;

}

private void phoneNumberHelper(ArrayList<String> result, String current, int index, int[] allNum,
HashMap<Integer, String[]> com) {
if (index == allNum.length) {
return;
} else {
String[] possibilities = com.get(allNum[index]);
for (int i = 0; i < possibilities.length; i++) {
//不使用StringBuilder，直接使用String连接一个String，这个做法其实和new String()是一样的。创建了一个新的String，
phoneNumberHelper(result, current + possibilities[i], index + 1, allNum, com);
}
}
}

``````

### 4.N皇后问题

u=4191624954,1499040036&fm=27&gp=0.jpg

u=3534538955,3983762870&fm=27&gp=0.jpg