-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp412.rs
31 lines (29 loc) · 877 Bytes
/
p412.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
struct Solution {}
/// String, formate!
impl Solution {
pub fn fizz_buzz(n: i32) -> Vec<String> {
let mut res = vec![];
for i in 1..=n {
let mut s = String::new();
if i % 3 == 0 {
s.push_str("Fizz");
}
if i % 5 == 0 {
s.push_str("Buzz");
}
if s.is_empty() {
s.push_str(format!("{}", i).as_str());
}
res.push(s);
}
res
}
}
#[test]
fn test() {
assert_eq!(vec!["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"],
Solution::fizz_buzz(15));
assert_eq!(vec!["1","2"], Solution::fizz_buzz(2));
assert_eq!(vec!["1","2","Fizz"], Solution::fizz_buzz(3));
assert_eq!(vec!["1","2","Fizz","4","Buzz","Fizz"], Solution::fizz_buzz(6));
}