1.一维度问题
1)排列问题(leetCode 46号问题)
题目描述:输入数字1,2,3,输出它们的所有组合1 2 3 ,1 3 2, 2 1 3, 2 3 1,3 1 2, 3 2 1。
代码实现:
方法一:
使用index来标记temp数组的长度,当长度为3的时候,就停止。
每次for循环都是从0开始,但是已经取过的值使用flag标记。
class HuiSu{
static ArrayList<ArrayList<Integer>> res = new ArrayList<>();
static ArrayList<Integer> temp = new ArrayList<>();
public static void main(String[] args) {
int[] nums = {1,2,3};
int[] flag = new int[nums.length];
helper(nums,0,flag);
System.out.println(res);
}
//第一种方式
public static void helper(int[] nums,int index,int[] flag){
if(index==nums.length){ //通过index来标记temp数组的长度,当长度到达3的时候就停止
res.add(new ArrayList<>(temp));
}
for(int i=0;i<nums.length;i++){ //注意这里的i是从0开始
if(flag[i]==0){
flag[i] = 1;
temp.add(nums[i]);
helper(nums,index+1,flag);//通过index来标记temp数组的长度,当长度到达3的时候就停止
temp.remove(temp.size()-1);
flag[i] = 0;
}
}
}
}
方法二,不使用index标记,直接取出数组的长度,当长度为3的时候就停止。
class HuiSu{
static ArrayList<ArrayList<Integer>> res = new ArrayList<>();
static ArrayList<Integer> temp = new ArrayList<>();
public static void main(String[] args) {
int[] nums = {1,2,3};
int[] flag = new int[nums.length];
helper(nums,flag);
System.out.println(res);
}
//第二种方式
public static void helper(int[] nums,int[] flag){
if(temp.size()==nums.length){
res.add(new ArrayList<>(temp));
}
for(int i=0;i<nums.length;i++){
if(flag[i]==0){
flag[i] = 1;
temp.add(nums[i]);
helper(nums,flag);
temp.remove(temp.size()-1);
flag[i] = 0;
}
}
}
}
2)组合问题
题目描述:从1......n这n个数字中选取k个数字的所有组合(leetCode 77号问题)
代码实现:
class HuiSu{
static ArrayList<ArrayList<Integer>> res = new ArrayList<>();
static ArrayList<Integer> temp = new ArrayList<>();
public static void main(String[] args) {
int[] nums = {1,2,3,4};
int start = 0;
int index = 1;
int k = 2;
int n = 4;
int[] flag = new int[nums.length];
helper(index,n,k);
System.out.println(res);
}
public static void helper(int index,int n,int k){
if(temp.size()==2){
res.add(new ArrayList<>(temp));
}
for(int i=index;i<=n;i++){
temp.add(i);
helper(i+1,n,k); //这边使用i + 1还是index + 1
temp.remove(temp.size()-1);
}
}
}
3)给以数字键盘,输入数字字符串,返回所有的可能的字母组合。
通过这道题对index的理解加深了,index和 i 的区别,这里的 i 在增加,但是index开始是0,后来为1.
class HuiSu {
public static void main(String[] args) {
Solution s = new Solution();
s.letterCombinations("23");
}
}
class Solution {
ArrayList<Character> temp = new ArrayList<>();
ArrayList<ArrayList<Character>> res = new ArrayList<>();
public void letterCombinations(String digits) {
String[] keyboard = new String[] { " ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
helper(keyboard, 0, digits);
System.out.println(res);
}
public void helper(String[] keyboard, int index, String digits) {
//这个停止条件
if (index == digits.length()) {
res.add(new ArrayList<Character>(temp));
return;
} //其实这题和[1,2,3]的排列相比也就多了这行
String letters = keyboard[digits.charAt(index) - '0']; // 首先letter = "abc" 第二次letter = "def"
for(int i=0;i<letters.length();i++){
temp.add(letters.charAt(i));
helper(keyboard,index+1,digits);
temp.remove(temp.size()-1);
}
}
}
4)深信服的题目
给定一个数组,求出数组中相加等于100的数的组合
class HuiSu {
static ArrayList<Integer> temp = new ArrayList<>();
static ArrayList<ArrayList<Integer>> result = new ArrayList<>();
static int[] visit;
public static void main(String[] args) {
int[] nums = {64,50,32,16,8,4,2,1,96};
int[] flag = new int[nums.length]; //用来标记已经访问过的坐标
helper(nums,100,0,flag);
System.out.println(result);
}
public static void helper(int[] nums,int target,int index,int[] flag){
if(target==0){
result.add(new ArrayList<>(temp));
}
if(index>=nums.length||target<0)
return;
for(int i=index;i<nums.length;i++){
//if(flag[index]==0){ //没有必要加上标志位
//flag[i] = 1;
temp.add(nums[i]);
helper(nums,target-nums[i],i+1,flag);
temp.remove(temp.size()-1);
//flag[i] = 0;
//}
}
}
}
5)二叉树中和为某一值的路径
输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)
代码实现:
class Solution {
ArrayList<ArrayList<Integer>> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
if(root==null||target<0)
return list1;
list2.add(root.val); //这两行代码的位置也有讲究
target = target - root.val; //这两行代码的位置也有讲究
if(root.left==null&&root.right==null&&target==0){
list1.add(new ArrayList(list2));
//return list1; //这一行代码也不能要,否则后面的list2.remove(list2.size()-1)无法执行
}
FindPath(root.left,target);
FindPath(root.right,target);
list2.remove(list2.size()-1);
return list1;
}
}
注意点:
这里画一个节点的树,就清楚 了。
list2.add(root.val); //这两行代码的位置也有讲究
target = target - root.val; //这两行代码的位置也有讲究
//return list1; //这一行代码也不能要,否则后面的
2.二维度问题
1)机器人的运动范围
地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?
代码实现:
class Solution {
int count;
public int movingCount(int threshold, int rows, int cols) //row行号,cols列号
{
int[][] flag = new int[rows][cols];
Helper(threshold,rows,cols,0,0,flag);
return count;
}
public void Helper(int threshold,int rows,int cols,int i,int j,int[][] flag){
if(i<0||i>=rows||j<0||j>=cols||flag[i][j]==1||f(i)+f(j)>threshold){
return;
}
flag[i][j] = 1;
count++;
Helper(threshold,rows,cols,i+1,j,flag);
Helper(threshold,rows,cols,i-1,j,flag);
Helper(threshold,rows,cols,i,j+1,flag);
Helper(threshold,rows,cols,i,j-1,flag);
}
public int f(int num){
int result = 0;
while(num>0){
result += num%10;
num /= 10;
}
return result;
}
}
思考:将换成下面的三行不行,原因是因为有些格子满足f(i)+f(j)<=threshold,但是机器人走不到,例如1Xn的格子,有些格子满足约束,但是机器人无法过去。
if(i<0||i>=rows||j<0||j>=cols||flag[i][j]==1{
return;
}
flag[i][j] = 1;
if(f(i)+f(j)<=threshold)
count++;
2)floodfill问题
有一个2D的由1和0构成的地图,0表示水,1表示陆地,问被水包围的陆地有多少个。
思路:使用一个同样大小的矩阵做标志矩阵,每次走过的路径,都将这个标志矩阵中对应的位设置为1,表示已经走过,不会再走了。
代码实现:
public class HuiSu {
private static int[][] array = {
{1, 1, 0, 0, 1},
{1, 1, 0, 0, 0},
{0, 0, 1, 0, 1},
{0, 0, 0, 1, 1},
};
public static void main(String[] args) {
int maxLine = 5; //列
int maxRow = 4; //行
int[][] flag = new int[maxRow][maxLine];
int countNum = 0;
for(int i=0;i<maxRow;i++){
for(int j=0;j<maxLine;j++){
if(array[i][j]==1&&flag[i][j]==0){
helper(i,j,maxRow,maxLine,flag);
countNum++;
}
}
}
System.out.println(countNum);
}
public static void helper(int i,int j,int maxRow,int maxLine,int[][] flag){
if(i<0||i>=maxRow||j<0||j>=maxLine)
return;
if(array[i][j]==1&&flag[i][j]==0){
flag[i][j]=1;
helper(i+1,j,maxRow,maxLine,flag);
helper(i-1,j,maxRow,maxLine,flag);
helper(i,j+1,maxRow,maxLine,flag);
helper(i,j-1,maxRow,maxLine,flag);
}
}
3)迷宫问题
public class HuiSu {
static int count = 0;
/**
* 定义迷宫数组
*/
private static int[][] array = {
{0, 0, 1, 0, 0, 0, 1, 0},
{0, 0, 1, 0, 0, 0, 1, 0},
{0, 0, 1, 0, 1, 1, 0, 1},
{0, 1, 1, 1, 0, 0, 1, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 1},
{0, 1, 1, 1, 1, 0, 0, 1},
{1, 1, 0, 0, 0, 1, 0, 1},
{1, 1, 0, 0, 0, 0, 0, 0}
};
public static void main(String[] args) {
int maxLine = 8; //列
int maxRow = 9; //行
System.out.println("开始时间: "+System.currentTimeMillis());
int[][] flag = new int[maxRow][maxLine];
helper(0,0,maxRow,maxLine,flag);;
System.out.println("结束时间: "+System.currentTimeMillis());
}
public static void helper(int i,int j,int maxRow,int maxLine,int[][] flag){
//如果没有找到就继续
if(i<0||i>=maxRow||j<0||j>=maxLine||array[i][j]==1||array[i][j]==5){
return;
}
if(i==maxRow-1&&j==maxLine-1){
print(maxRow,maxLine); //将能到达目的的路径打印出来
return;
}
array[i][j] = 5; //已经走过的路径设置为5
helper(i-1,j,maxRow,maxLine,flag);
helper(i+1,j,maxRow,maxLine,flag);
helper(i,j+1,maxRow,maxLine,flag);
helper(i,j-1,maxRow,maxLine,flag);
array[i][j] = 0; //从坐标[i,j]开始,都不行,就清除标志
}
//打印
private static void print(int maxRow,int maxLine) {
System.out.println("得到第"+(++count)+"解");
for (int i = 0; i < maxRow; i++) {
for (int j = 0; j < maxLine; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}