Binary Prefix Divisible By 5
找了半天规律,发现没意思。直接破解了。然后submit发现有overflow,看了下输入是个数组,那真是大。立刻想到取模。取模有分配律,对答案无影响
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Solution { public List<Boolean> prefixesDivBy5(int[] A) { List<Boolean> res = new ArrayList<>(); long n = 0; for(int i : A){ n = n*2 + i; //convert int[] A to binary n %= 5;// mod distribution law if(n == 0) res.add(true); else res.add(false); } return res; } } |