Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fafc4f9

Browse files
author
guangsheng.li01
committed
Solved 1018
1 parent 8585955 commit fafc4f9

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* [1018] binary-prefix-divisible-by-5
3+
*/
4+
5+
pub struct Solution {}
6+
7+
// solution impl starts here
8+
9+
impl Solution {
10+
pub fn prefixes_div_by5(a: Vec<i32>) -> Vec<bool> {
11+
let mut result = Vec::with_capacity(a.len());
12+
let mut n = 0;
13+
14+
for i in a {
15+
n <<= 1;
16+
n += i;
17+
result.push(n % 5 == 0);
18+
n %= 5;
19+
}
20+
result
21+
}
22+
}
23+
24+
// solution impl ends here
25+
26+
// solution tests starts here
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::*;
31+
32+
#[test]
33+
fn test_case0() {
34+
assert_eq!(
35+
Solution::prefixes_div_by5(vec![0, 1, 1]),
36+
vec![true, false, false]
37+
);
38+
}
39+
40+
#[test]
41+
fn test_case1() {
42+
assert_eq!(
43+
Solution::prefixes_div_by5(vec![1, 1, 1]),
44+
vec![false, false, false]
45+
);
46+
}
47+
#[test]
48+
fn test_case2() {
49+
assert_eq!(
50+
Solution::prefixes_div_by5(vec![0, 1, 1, 1, 1, 1]),
51+
vec![true, false, false, false, true, false]
52+
);
53+
}
54+
55+
#[test]
56+
fn test_case3() {
57+
assert_eq!(
58+
Solution::prefixes_div_by5(vec![1, 1, 1, 0, 1]),
59+
vec![false, false, false, false, false]
60+
);
61+
}
62+
}
63+
64+
// solution tests ends here

‎src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ mod a0937_reorder_data_in_log_files;
3535
mod a0949_largest_time_for_given_digits;
3636
mod a0973_k_closest_points_to_origin;
3737
mod a0980_unique_paths_iii;
38+
mod a1018_binary_prefix_divisible_by_5;
3839
mod a1021_remove_outermost_parentheses;
3940
mod a1047_remove_all_adjacent_duplicates_in_string;
4041
mod a1200_minimum_absolute_difference;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /