Grasping `all-the-apples-at-once' › 解法样本
Sample solutions
› Rust (Group II) [#285]
Grasping `all-the-apples-at-once' › 解法样本 Sample solutions › Rust (Group II) [#285]
fn run_length_encode(ins: &str) -> Vec<(char, usize)> {
let mut out = vec![];
let mut i = ins.chars();
if let Some(mut c) = i.next() {
let mut count = 1;
for new_c in i {
if new_c == c {
count += 1;
} else {
out.push((c, count));
count = 1;
c = new_c;
}
}
out.push((c, count));
}
out}
就像 GO 的解法一样⸺但是更干净,并且更好地处理边缘情况。作者确实掌握了问题的本质:
游程编码
。
It is quite like the solution in Go -- but cleaner and with a better handling of edge cases. The author did grasp the nature of the problem: run-length encoding.