Leetcode_1680_连续连续二进制数字_模拟

12/12

找规律
public class Solution {
    
    
    private static final int MOD = 1000000007;

    public int concatenatedBinary(int n) {
    
    
        int res = 0, shift = 0;
        for (int i = 1; i <= n; i++) {
    
    
            if ((i & (i - 1)) == 0) {
    
    
                // 说明是2的幂,则进位
                shift++;
            }
            res = (int) ((((long) res << shift) + i) % MOD);
        }
        return res;
    }
}

猜你喜欢

转载自blog.csdn.net/HDUCheater/article/details/111058911