LeetCode Solutions

1018. Binary Prefix Divisible By 5

Time:

Space:

			

class Solution {
 public:
  vector<bool> prefixesDivBy5(vector<int>& A) {
    vector<bool> ans;
    int num = 0;

    for (int a : A) {
      num = (num * 2 + a) % 5;
      ans.push_back(num % 5 == 0);
    }

    return ans;
  }
};
			

class Solution {
  public List<Boolean> prefixesDivBy5(int[] A) {
    List<Boolean> ans = new ArrayList<>();
    int num = 0;

    for (int a : A) {
      num = (num * 2 + a) % 5;
      ans.add(num % 5 == 0);
    }

    return ans;
  }
}
			

class Solution:
  def prefixesDivBy5(self, A: List[int]) -> List[bool]:
    ans = []
    num = 0

    for a in A:
      num = (num * 2 + a) % 5
      ans.append(num % 5 == 0)

    return ans