Day 4
剑指 offer 11 旋转数组的最小数字
题目描述
解答
//二分查找的时间复杂度是 log n
class Solution {
public int minArray(int[] numbers) {
int low=0,high=numbers.length-1,mid;
while(low<high){
mid=(low+high)/2;
if(numbers[mid]<numbers[high]){
high=mid;
}else if(numbers[mid]>numbers[high]){
low+=1;
}else high-=1;
}
return numbers[low];
}
}
剑指 offer 13 机器人的运动范围
题目描述
解答
class Solution {
public int movingCount(int m, int n, int k) {
boolean[][] visited = new boolean[m][n];
return dfs(0, 0, m, n, k, visited);
}
private int dfs(int i, int j, int m, int n, int k, boolean visited[][]) {
// i表示当前所在行,j表示当前所在列
// visited[i][j]=true 表明i行j列的元素已被访问过
if (i < 0 || i >= m || j < 0 ||
j >= n || (i/10 + i%10 + j/10 + j%10) > k || visited[i][j]) {
return 0;
}
visited[i][j] = true;
return dfs(i + 1, j, m, n, k, visited)
+dfs(i, j + 1, m, n, k, visited)
+1;
}
}