Approach
# 陣列向左移就是*=2
Appending a digit to the right of a binary number is equivalent to multiplying the current value by 2.
# 小心溢位
Be careful with overflow.
Time Complexity
# n = length of nums
O(n)
Space Complexity
# n = length of nums
O(n)
Code
class Solution {
public:
vector<bool> prefixesDivBy5(vector<int>& nums) {
int l = nums.size(), sum = 0;
vector<bool> ans;
for (int i = 0 ; i < l ; i++) {
sum %= 5;
sum *= 2;
sum += nums[i];
if (sum % 5 == 0) {
ans.push_back(true);
}
else {
ans.push_back(false);
}
}
return ans;
}
};