mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1990 from fwqaaq/patch-25
Update 0844.比较含退格的字符串.md about rust
This commit is contained in:
@ -535,8 +535,57 @@ 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> 双栈法
|
||||||
|
|
||||||
|
```rust
|
||||||
|
impl Solution {
|
||||||
|
pub fn backspace_compare(s: String, t: String) -> bool {
|
||||||
|
Self::get_string(s) == Self::get_string(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_string(string: String) -> String {
|
||||||
|
let mut s = String::new();
|
||||||
|
for c in string.chars() {
|
||||||
|
if c != '#' {
|
||||||
|
s.push(c);
|
||||||
|
} else if !s.is_empty() {
|
||||||
|
s.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
Reference in New Issue
Block a user