Update 0844.比较含退格的字符串.md

This commit is contained in:
fw_qaq
2023-03-29 18:15:26 +08:00
committed by GitHub
parent d2bbea431e
commit 59c86a7023

View File

@ -535,6 +535,35 @@ function getIndexAfterDel(s: string, startIndex: number): number {
} }
``` ```
### Rust
> 双指针
```rust
impl Solution {
pub fn backspace_compare(s: String, t: String) -> bool {
let (s, t) = (
s.chars().collect::<Vec<char>>(),
t.chars().collect::<Vec<char>>(),
);
Self::get_string(s) == Self::get_string(t)
}
pub fn get_string(mut chars: Vec<char>) -> Vec<char> {
let mut slow = 0;
for i in 0..chars.len() {
if chars[i] == '#' {
slow = (slow as u32).saturating_sub(1) as usize;
} else {
chars[slow] = chars[i];
slow += 1;
}
}
chars.truncate(slow);
chars
}
}
```