-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcount-and-say.rs
44 lines (38 loc) · 1.04 KB
/
count-and-say.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
#![allow(dead_code, unused, unused_variables)]
fn main() {
assert_eq!("1211".to_string(), Solution::count_and_say(4));
}
struct Solution;
impl Solution {
pub fn count_and_say(n: i32) -> String {
if n == 1 {
return "1".to_string();
}
let mut n = n;
let mut array = vec![1; 1];
let mut index = 0;
while n > 1 {
let start = array.len();
let (mut num, mut x) = (1, array[index]);
for i in index + 1..array.len() {
if array[i] == x {
num += 1;
} else {
array.push(num);
array.push(x);
num = 1;
x = array[i];
}
}
array.push(num);
array.push(x);
index = start;
n -= 1;
}
let mut result = String::new();
for i in index..array.len() {
result.push_str(array[i].to_string().as_str());
}
result
}
}