Implement pow(x, n).
注意n为负数的情况
Source
public class Solution {
public double pow(double x, int n) {
if(n == 0) return 1;
if(n < 0){
n = -n;
x = 1 / x;
}
if(n % 2 == 0){
return pow(x * x, n/2);
}
else return x * pow(x * x, n/2);
}
}
Test
public static void main(String[] args){
int[][] matrix = {{1, 3, 5, 7},{10, 11, 16, 20},{23, 30, 34, 50}};
System.out.println(new Solution().searchMatrix(matrix, 4));
}