diff --git a/leetcode/1301-1400/1324.Print-Words-Vertically/README.md b/leetcode/1301-1400/1324.Print-Words-Vertically/README.md index e9b977756..c61429d26 100644 --- a/leetcode/1301-1400/1324.Print-Words-Vertically/README.md +++ b/leetcode/1301-1400/1324.Print-Words-Vertically/README.md @@ -1,28 +1,38 @@ # [1324.Print Words Vertically][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`. Return all the words vertically in the same order in which they appear in `s`. +Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed). +Each word would be put on only one column and that in one column there will be only one word. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "HOW ARE YOU" +Output: ["HAY","ORO","WEU"] +Explanation: Each word is printed vertically. + "HAY" + "ORO" + "WEU" ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Print Words Vertically -```go +``` +Input: s = "TO BE OR NOT TO BE" +Output: ["TBONTB","OEROOE"," T"] +Explanation: Trailing spaces is not allowed. +"TBONTB" +"OEROOE" +" T" ``` +**Example 3:** + +``` +Input: s = "CONTEST IS COMING" +Output: ["CIC","OSO","N M","T I","E N","S G","T"] +``` ## 结语 diff --git a/leetcode/1301-1400/1324.Print-Words-Vertically/Solution.go b/leetcode/1301-1400/1324.Print-Words-Vertically/Solution.go index d115ccf5e..3eac95afb 100644 --- a/leetcode/1301-1400/1324.Print-Words-Vertically/Solution.go +++ b/leetcode/1301-1400/1324.Print-Words-Vertically/Solution.go @@ -1,5 +1,31 @@ package Solution -func Solution(x bool) bool { - return x +import "strings" + +func Solution(s string) []string { + words := strings.Split(s, " ") + ml := 0 + for _, w := range words { + ml = max(ml, len(w)) + } + ans := make([]string, 0) + buf := strings.Builder{} + for index := 0; index < ml; index++ { + tailSpaces := 0 + for _, w := range words { + if index < len(w) { + buf.WriteByte(w[index]) + tailSpaces = 0 + continue + } + buf.WriteByte(' ') + tailSpaces++ + } + cur := buf.String() + l := len(cur) - tailSpaces + cur = cur[:l] + ans = append(ans, cur) + buf.Reset() + } + return ans } diff --git a/leetcode/1301-1400/1324.Print-Words-Vertically/Solution_test.go b/leetcode/1301-1400/1324.Print-Words-Vertically/Solution_test.go index 14ff50eb4..4dde447db 100644 --- a/leetcode/1301-1400/1324.Print-Words-Vertically/Solution_test.go +++ b/leetcode/1301-1400/1324.Print-Words-Vertically/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", "HOW ARE YOU", []string{"HAY", "ORO", "WEU"}}, + {"TestCase2", "TO BE OR NOT TO BE", []string{"TBONTB", "OEROOE", " T"}}, + {"TestCase3", "CONTEST IS COMING", []string{"CIC", "OSO", "N M", "T I", "E N", "S G", "T"}}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }