-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
52 lines (48 loc) · 1.65 KB
/
main.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
fn main() {
let x = Solution::count_and_say(6);
println!("x = {:#?}", x);
}
struct Solution {}
impl Solution {
pub fn count_and_say(n: i32) -> String {
if n == 1 { return "1".to_string() }
let s: Vec<u8> = Self::count_and_say(n - 1).bytes().collect();
let mut ret = "".to_string();
let mut number = s[0];
let mut cnt = 1;
let mut i = 1;
while i <= s.len() {
if i >= s.len() {
ret.push_str(format!("{}", cnt).as_str());
ret.push(number as char);
break
} else if s[i] != number {
ret.push_str(format!("{}", cnt).as_str());
ret.push(number as char);
number = s[i];
cnt = 1;
} else {
cnt += 1;
}
i += 1;
}
ret
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::count_and_say(1), "1".to_string());
assert_eq!(Solution::count_and_say(2), "11".to_string());
assert_eq!(Solution::count_and_say(3), "21".to_string());
assert_eq!(Solution::count_and_say(4), "1211".to_string());
assert_eq!(Solution::count_and_say(5), "111221".to_string());
assert_eq!(Solution::count_and_say(6), "312211".to_string());
assert_eq!(Solution::count_and_say(7), "13112221".to_string());
assert_eq!(Solution::count_and_say(8), "1113213211".to_string());
assert_eq!(Solution::count_and_say(9), "31131211131221".to_string());
assert_eq!(Solution::count_and_say(10), "13211311123113112211".to_string());
}
}