mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 557
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func reverseWords(s string) string {
|
||||
ss := strings.Split(s, " ")
|
||||
for i, s := range ss {
|
||||
ss[i] = revers(s)
|
||||
}
|
||||
return strings.Join(ss, " ")
|
||||
}
|
||||
|
||||
func revers(s string) string {
|
||||
bytes := []byte(s)
|
||||
i, j := 0, len(bytes)-1
|
||||
for i < j {
|
||||
bytes[i], bytes[j] = bytes[j], bytes[i]
|
||||
i++
|
||||
j--
|
||||
}
|
||||
return string(bytes)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question557 struct {
|
||||
para557
|
||||
ans557
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para557 struct {
|
||||
s string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans557 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
func Test_Problem557(t *testing.T) {
|
||||
|
||||
qs := []question557{
|
||||
|
||||
question557{
|
||||
para557{"Let's take LeetCode contest"},
|
||||
ans557{"s'teL ekat edoCteeL tsetnoc"},
|
||||
},
|
||||
|
||||
question557{
|
||||
para557{""},
|
||||
ans557{""},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 557------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans557, q.para557
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, reverseWords(p.s))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
25
Algorithms/0557. Reverse Words in a String III/README.md
Executable file
25
Algorithms/0557. Reverse Words in a String III/README.md
Executable file
@@ -0,0 +1,25 @@
|
||||
# [557. Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/)
|
||||
|
||||
|
||||
## 题目:
|
||||
|
||||
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Input: "Let's take LeetCode contest"
|
||||
Output: "s'teL ekat edoCteeL tsetnoc"
|
||||
|
||||
**Note:** In the string, each word is separated by single space and there will not be any extra space in the string.
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
|
||||
- 反转字符串,要求按照空格隔开的小字符串为单位反转。
|
||||
- 这是一道简单题。按照题意反转每个空格隔开的单词即可。
|
||||
Reference in New Issue
Block a user