-
Notifications
You must be signed in to change notification settings - Fork 17
/
Que66.java
46 lines (34 loc) · 1.05 KB
/
Que66.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* @author: hyl
* @date: 2019/08/19
**/
public class Que66 {
public int movingCount(int threshold, int rows, int cols) {
int[][] flag = new int[rows][cols];
return helper(0 , 0 , rows , cols , flag , threshold);
}
private int helper(int i, int j, int rows, int cols, int[][] flag, int threshold) {
if (i < 0 || i >= rows || j < 0 || j >= cols || (numSum(i) + numSum(j)) > threshold || flag[i][j] == 1){
return 0;
}
flag[i][j] = 1;
//前后左右移动
return helper(i - 1 , j , rows , cols , flag , threshold)
+ helper(i + 1 , j , rows , cols , flag , threshold)
+ helper(i , j - 1 , rows , cols , flag , threshold)
+ helper(i , j + 1 , rows , cols , flag , threshold) + 1;
}
/**
* 求出i的各位之和
* @param i
* @return
*/
private int numSum(int i) {
int sum = 0;
while (i > 0){
sum += (i % 10);
i /= 10;
}
return sum;
}
}