Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6a8cdad

Browse files
committed
q557
1 parent 615877e commit 6a8cdad

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

‎double-pointer/src/q557.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
class Solution(object):
22

3-
4-
53
def reverseWords(self, s):
64

75
return " ".join(x[::-1] for x in s.split(" "))

‎double-pointer/src/q557.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
struct Solution ();
2+
impl Solution {
3+
pub fn reverse_words(s: String) -> String {
4+
5+
let mut v = s.into_bytes();
6+
let (mut left, mut right) = (0, 0);
7+
8+
while right < v.len() {
9+
while right < v.len() && v[right] != ' ' as u8 {
10+
right += 1
11+
}
12+
13+
let next = right + 1;
14+
15+
while left < right {
16+
unsafe {
17+
std::ptr::swap(&mut v[right -1], &mut v[left]);
18+
}
19+
left += 1;
20+
right -= 1;
21+
}
22+
23+
left = next;
24+
right = next;
25+
}
26+
27+
String::from_utf8(v).unwrap()
28+
}
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use super::*;
34+
35+
#[test] fn test_1() {
36+
37+
38+
39+
assert_eq!(
40+
Solution::reverse_words(String::from("Let's take LeetCode contest")),
41+
String::from("s'teL ekat edoCteeL tsetnoc"))
42+
}
43+
44+
45+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /