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 79cc076

Browse files
author
Sandy
authored
Merge pull request #165 from openset/develop
Add: reverse_words_in_a_string_iii
2 parents 635e153 + 81b6c58 commit 79cc076

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
11
package reverse_words_in_a_string_iii
2+
3+
func reverseWords(s string) string {
4+
ss, pre, l := []byte(s+" "), 0, len(s)
5+
for cur, c := range ss {
6+
if c == ' ' {
7+
ws := ss[pre:cur]
8+
for i, j := 0, len(ws)-1; i < j; i, j = i+1, j-1 {
9+
ws[i], ws[j] = ws[j], ws[i]
10+
}
11+
pre = cur + 1
12+
}
13+
}
14+
return string(ss[:l])
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
11
package reverse_words_in_a_string_iii
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input string
7+
expected string
8+
}
9+
10+
func TestReverseWords(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: "Let's take LeetCode contest",
14+
expected: "s'teL ekat edoCteeL tsetnoc",
15+
},
16+
}
17+
for _, tc := range tests {
18+
output := reverseWords(tc.input)
19+
if output != tc.expected {
20+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
21+
}
22+
}
23+
}

0 commit comments

Comments
(0)

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