通过较小的子例来解决一个实例;
对于一个较小的实例,可能需要许多个解决方案;
把较小实例的解决方案存储在一个表中,一旦遇上,就很容易解决;
附加空间用来节省时间。
上面所列的爬台阶问题完全符合这四个属性,因此,可以使用动态规划来解决:
public static int[] A = new int[100];
public static int f3(int n) {
if (n <= 2)
A[n]= n;
从一个给定的数n中找位i(i从0开始,然后向右开始)
public static boolean getBit(int num, int i){
int result = num & (1<<i);
if(result == 0){
return false;
}else{
return true;
}
}
例如,获取10的第二位:
i=1, n=10
1<<1= 10
1010&10=10
10 is not 0, so return true;
典型的位算法:
Find Single Number
Maximum Binary Gap