[US Giants] 四.Math & Bit Manipulation

Trailing Zeros

Write an algorithm which computes the number of trailing zeros in n factorial.

Example: 11! = 39916800, so the out should be 2
Challenge O(log N) time

思路:1*2*3*4*5*6*7*8*9*10

           前5个数中有一个2一个5,相乘有一个0,后5个数中有一个10,也有一个0,因此,每5个数中会有一个0

class Solution {
    /*
     * param n: As desciption
     * return: An integer, denote the number of trailing zeros in n!
     */
    public long trailingZeros(long n) {
        long sum=0;
        while(n!=0){
            sum+=n/5;
            n/=5;
        }
        return sum;
    }
}
Flip Bits

Determine the number of bits required to flip if you want to convert integer nto integer m.

Both n and m are 32-bit integers.

Example

Given n = 31 (11111), m = 14 (01110), return 2.

思路:1. 先异或运算拿到结果,结果里的1的个数就是不一样的个数

               异或运算:两位不同,亦或结果为1,如图,两个1不同

           2. 再用按位与数出来上面得到的不同的结果

               按位与:只有两位同时为1时,结果才为1,如图

           3. 然后无符号移位到下一位       

               java中有三种移位运算, 例如32bits

               <<:左移运算符,num<<1,相当于num乘以2

              1的表示: 0000 0000 0000 0000 0000 0000 0000 0001,左移一位:0000 0000 0000 0000 0000 0000 0000 0010

               >>:右移运算符,num>>1,相当于num除以2

              -1的表示:(最前面的1是符号位)1000 0000 0000 0000 0000 0000 0000 0001,右移一位:1100 0000 0000 0000 0000 0000 0000 0000

               >>>:无符号右移,忽略符号位,空位都以0补齐  

               -1的表示:1000 0000 0000 0000 0000 0000 0000 0001,右移一位:0100 0000 0000 0000 0000 0000 0000 0000        

class Solution {
    /**
     *@param a, b: Two integer
     *return: An integer
     */
    public static int bitSwapRequired(int a, int b) {
        int c= a ^ b;                                   //异或运算
        int totalBit=0;
        while(c!=0){
            int bit =c & 1;                             //按位与
            totalBit+=bit;
            c=c>>>1;                                    //无符号右移
        }
        return totalBit;
    }
};
Fast Power

Calculate the an % b where a, b and n are all 32bit integers.

Example

For 231 % 3 = 2

For 1001000 % 1000 = 0

Challenge  O(logn)
class Solution {
    /*
     * @param a, b, n: 32bit integers
     * @return: An integer
     */
    public int fastPower(int a, int b, int n) {
        if(n==0) {
           return 1%b;
        }
        
        long v=fastPower(a, b, n/2);           //一直给n除以2直到n=0
        v=(v*v)%b;
        if(n%2==1){                            //如果原n不能被2整除,还有一个a要处理
           v=(v*a)%b;
        }
        return (int)v;
    }
}
Unique Binary Search Tree

Given n, how many structurally unique BSTs (binary search trees) that store values 1...n?

Example

Given n = 3, there are a total of 5 unique BST's.

1           3    3       2      1
 \         /    /       / \      \
  3      2     1       1   3      2
 /      /       \                  \
2     1          2                  3
思路:参考 点击打开链接
public class Solution {
    /**
     * @paramn n: An integer
     * @return: An integer
     */
    public int numTrees(int n) {
        int[] count=new int[n+2];
        count[0]=1;
        count[1]=1;
        for(int i=2;i<=n;i++){
            for(int j=0;j<i;j++){
                count[i]+=count[j]*count[i-j-1];
            }
        }
        return count[n];
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值