diff --git a/leetcode/901-1000/0917.Reverse-Only-Letters/README.md b/leetcode/901-1000/0917.Reverse-Only-Letters/README.md index 4be05757a..eba615bd7 100644 --- a/leetcode/901-1000/0917.Reverse-Only-Letters/README.md +++ b/leetcode/901-1000/0917.Reverse-Only-Letters/README.md @@ -1,28 +1,33 @@ # [917.Reverse Only Letters][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +Given a string `s`, reverse the string according to the following rules: + +- All the characters that are not English letters remain in the same position. +- All the English letters (lowercase or uppercase) should be reversed. + +Return `s` after reversing it. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "ab-cd" +Output: "dc-ba" ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Reverse Only Letters -```go ``` +Input: s = "a-bC-dEf-ghIj" +Output: "j-Ih-gfE-dCba" +``` + +**Example 3:** +``` +Input: s = "Test1ng-Leet=code-Q!" +Output: "Qedo1ct-eeLg=ntse-T!" +``` ## 结语 diff --git a/leetcode/901-1000/0917.Reverse-Only-Letters/Solution.go b/leetcode/901-1000/0917.Reverse-Only-Letters/Solution.go index d115ccf5e..ea9f5a7e0 100644 --- a/leetcode/901-1000/0917.Reverse-Only-Letters/Solution.go +++ b/leetcode/901-1000/0917.Reverse-Only-Letters/Solution.go @@ -1,5 +1,20 @@ package Solution -func Solution(x bool) bool { - return x +func ok(b byte) bool { + return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') +} + +func Solution(s string) string { + bs := []byte(s) + l, r := 0, len(bs)-1 + for l < r { + for ; l < r && !ok(bs[l]); l++ { + + } + for ; r > l && !ok(bs[r]); r-- { + } + bs[l], bs[r] = bs[r], bs[l] + l, r = l+1, r-1 + } + return string(bs) } diff --git a/leetcode/901-1000/0917.Reverse-Only-Letters/Solution_test.go b/leetcode/901-1000/0917.Reverse-Only-Letters/Solution_test.go index 14ff50eb4..c4355d8c1 100644 --- a/leetcode/901-1000/0917.Reverse-Only-Letters/Solution_test.go +++ b/leetcode/901-1000/0917.Reverse-Only-Letters/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs string + expect string }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "ab-cd", "dc-ba"}, + {"TestCase2", "a-bC-dEf-ghIj", "j-Ih-gfE-dCba"}, + {"TestCase3", "Test1ng-Leet=code-Q!", "Qedo1ct-eeLg=ntse-T!"}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }