mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
规范格式
This commit is contained in:
@ -0,0 +1,17 @@
|
||||
package leetcode
|
||||
|
||||
import "strings"
|
||||
|
||||
func reverseWords151(s string) string {
|
||||
ss := strings.Fields(s)
|
||||
reverse151(&ss, 0, len(ss)-1)
|
||||
return strings.Join(ss, " ")
|
||||
}
|
||||
|
||||
func reverse151(m *[]string, i int, j int) {
|
||||
for i <= j {
|
||||
(*m)[i], (*m)[j] = (*m)[j], (*m)[i]
|
||||
i++
|
||||
j--
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question151 struct {
|
||||
para151
|
||||
ans151
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para151 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans151 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
func Test_Problem151(t *testing.T) {
|
||||
|
||||
qs := []question151{
|
||||
|
||||
question151{
|
||||
para151{"the sky is blue"},
|
||||
ans151{"blue is sky the"},
|
||||
},
|
||||
|
||||
question151{
|
||||
para151{" hello world! "},
|
||||
ans151{"world! hello"},
|
||||
},
|
||||
question151{
|
||||
para151{"a good example"},
|
||||
ans151{"example good a"},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 151------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans151, q.para151
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, reverseWords151(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
57
leetcode/0151.Reverse-Words-in-a-String/README.md
Executable file
57
leetcode/0151.Reverse-Words-in-a-String/README.md
Executable file
@ -0,0 +1,57 @@
|
||||
# [151. Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/)
|
||||
|
||||
|
||||
|
||||
## 题目:
|
||||
|
||||
Given an input string, reverse the string word by word.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Input: "the sky is blue"
|
||||
Output: "blue is sky the"
|
||||
|
||||
**Example 2:**
|
||||
|
||||
Input: " hello world! "
|
||||
Output: "world! hello"
|
||||
Explanation: Your reversed string should not contain leading or trailing spaces.
|
||||
|
||||
**Example 3:**
|
||||
|
||||
Input: "a good example"
|
||||
Output: "example good a"
|
||||
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
|
||||
|
||||
**Note:**
|
||||
|
||||
- A word is defined as a sequence of non-space characters.
|
||||
- Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
|
||||
- You need to reduce multiple spaces between two words to a single space in the reversed string.
|
||||
|
||||
**Follow up:**
|
||||
|
||||
For C programmers, try to solve it *in-place* in *O*(1) extra space.
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个字符串,逐个翻转字符串中的每个单词。
|
||||
|
||||
说明:
|
||||
|
||||
- 无空格字符构成一个单词。
|
||||
- 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
|
||||
- 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
|
||||
|
||||
|
||||
进阶:
|
||||
|
||||
- 请选用 C 语言的用户尝试使用 O(1) 额外空间复杂度的原地解法。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
|
||||
- 给出一个中间有空格分隔的字符串,要求把这个字符串按照单词的维度前后翻转。
|
||||
- 依照题意,先把字符串按照空格分隔成每个小单词,然后把单词前后翻转,最后再把每个单词中间添加空格。
|
Reference in New Issue
Block a user