Skip to content

Commit 3fef498

Browse files
committed
Add solution #1018
1 parent 87f20e4 commit 3fef498

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,092 LeetCode solutions in JavaScript
1+
# 1,093 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -826,6 +826,7 @@
826826
1015|[Smallest Integer Divisible by K](./solutions/1015-smallest-integer-divisible-by-k.js)|Medium|
827827
1016|[Binary String With Substrings Representing 1 To N](./solutions/1016-binary-string-with-substrings-representing-1-to-n.js)|Medium|
828828
1017|[Convert to Base -2](./solutions/1017-convert-to-base-2.js)|Medium|
829+
1018|[Binary Prefix Divisible By 5](./solutions/1018-binary-prefix-divisible-by-5.js)|Easy|
829830
1022|[Sum of Root To Leaf Binary Numbers](./solutions/1022-sum-of-root-to-leaf-binary-numbers.js)|Easy|
830831
1023|[Camelcase Matching](./solutions/1023-camelcase-matching.js)|Medium|
831832
1028|[Recover a Tree From Preorder Traversal](./solutions/1028-recover-a-tree-from-preorder-traversal.js)|Hard|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 1018. Binary Prefix Divisible By 5
3+
* https://leetcode.com/problems/binary-prefix-divisible-by-5/
4+
* Difficulty: Easy
5+
*
6+
* You are given a binary array nums (0-indexed).
7+
*
8+
* We define xi as the number whose binary representation is the subarray nums[0..i]
9+
* (from most-significant-bit to least-significant-bit).
10+
*
11+
* For example, if nums = [1,0,1], then x0 = 1, x1 = 2, and x2 = 5.
12+
*
13+
* Return an array of booleans answer where answer[i] is true if xi is divisible by 5.
14+
*/
15+
16+
/**
17+
* @param {number[]} nums
18+
* @return {boolean[]}
19+
*/
20+
var prefixesDivBy5 = function(nums) {
21+
const result = new Array(nums.length);
22+
23+
for (let i = 0, current = 0; i < nums.length; i++) {
24+
current = (current * 2 + nums[i]) % 5;
25+
result[i] = current === 0;
26+
}
27+
28+
return result;
29+
};

0 commit comments

Comments
 (0)