Skip to content

Commit

Permalink
leetcode: Add 0151
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed May 12, 2024
1 parent 190c165 commit 946a261
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/leetcode/0151.reverse-words-in-a-string/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "lc-0151-reverse-words-in-a-string"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
4 changes: 4 additions & 0 deletions src/leetcode/0151.reverse-words-in-a-string/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#

[问题描述](../problems/)
71 changes: 71 additions & 0 deletions src/leetcode/0151.reverse-words-in-a-string/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

// 使用 str::split_ascii_whitespace().
pub fn reverse_words1(s: String) -> String {
let parts: Vec<&str> = s.split_ascii_whitespace().rev().collect();
parts.join(" ")
}

// 手动实现空格的分隔操作
pub fn reverse_words2(s: String) -> String {
debug_assert!(!s.is_empty());

let mut index: usize = 0;
let bytes: &[u8] = s.as_bytes();
let len: usize = bytes.len();
let mut parts: Vec<&str> = Vec::new();

while index < len {
if bytes[index] != b' ' {
// 贪心, 读取尽可能多的字符.
let start: usize = index;
while index < len && bytes[index] != b' ' {
index += 1;
}
parts.push(&s[start..index]);
}

index += 1;
}

parts.reverse();
parts.join(" ")
}

pub type SolutionFn = fn(String) -> String;

fn check_solution(func: SolutionFn) {
let s = "the sky is blue".to_owned();
let exp = "blue is sky the".to_owned();
assert_eq!(func(s), exp);

let s = " hello world ".to_owned();
let exp = "world hello".to_owned();
assert_eq!(func(s), exp);

let s = "a good example".to_owned();
let exp = "example good a".to_owned();
assert_eq!(func(s), exp);
}

fn main() {
check_solution(reverse_words1);
check_solution(reverse_words2);
}

#[cfg(test)]
mod tests {
use super::{check_solution, reverse_words1, reverse_words2};

#[test]
fn test_reverse_words1() {
check_solution(reverse_words1);
}

#[test]
fn test_reverse_words2() {
check_solution(reverse_words2);
}
}

0 comments on commit 946a261

Please sign in to comment.