From 74d01e508a643f5fe01575d8bc3dc775316d87eb Mon Sep 17 00:00:00 2001 From: YDZ Date: Thu, 8 Aug 2019 21:27:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=20541?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../541. Reverse String II.go | 17 ++++++ .../541. Reverse String II_test.go | 58 +++++++++++++++++++ Algorithms/0541. Reverse String II/README.md | 31 ++++++++++ 3 files changed, 106 insertions(+) create mode 100644 Algorithms/0541. Reverse String II/541. Reverse String II.go create mode 100644 Algorithms/0541. Reverse String II/541. Reverse String II_test.go create mode 100755 Algorithms/0541. Reverse String II/README.md diff --git a/Algorithms/0541. Reverse String II/541. Reverse String II.go b/Algorithms/0541. Reverse String II/541. Reverse String II.go new file mode 100644 index 00000000..fe300150 --- /dev/null +++ b/Algorithms/0541. Reverse String II/541. Reverse String II.go @@ -0,0 +1,17 @@ +package leetcode + +func reverseStr(s string, k int) string { + if k > len(s) { + k = len(s) + } + for i := 0; i < len(s); i = i + 2*k { + if len(s)-i >= k { + ss := revers(s[i : i+k]) + s = s[:i] + ss + s[i+k:] + } else { + ss := revers(s[i:]) + s = s[:i] + ss + } + } + return s +} diff --git a/Algorithms/0541. Reverse String II/541. Reverse String II_test.go b/Algorithms/0541. Reverse String II/541. Reverse String II_test.go new file mode 100644 index 00000000..358f0aa1 --- /dev/null +++ b/Algorithms/0541. Reverse String II/541. Reverse String II_test.go @@ -0,0 +1,58 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question541 struct { + para541 + ans541 +} + +// para 是参数 +// one 代表第一个参数 +type para541 struct { + s string + k int +} + +// ans 是答案 +// one 代表第一个答案 +type ans541 struct { + one string +} + +func Test_Problem541(t *testing.T) { + + qs := []question541{ + + question541{ + para541{"abcdefg", 2}, + ans541{"bacdfeg"}, + }, + + question541{ + para541{"abcdefg", 5}, + ans541{"edcbafg"}, + }, + + question541{ + para541{"abcd", 4}, + ans541{"dcba"}, + }, + + question541{ + para541{"", 100}, + ans541{""}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 541------------------------\n") + + for _, q := range qs { + _, p := q.ans541, q.para541 + fmt.Printf("【input】:%v 【output】:%v\n", p, reverseStr(p.s, p.k)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0541. Reverse String II/README.md b/Algorithms/0541. Reverse String II/README.md new file mode 100755 index 00000000..f368ba5f --- /dev/null +++ b/Algorithms/0541. Reverse String II/README.md @@ -0,0 +1,31 @@ +# [541. Reverse String II](https://leetcode.com/problems/reverse-string-ii/) + + +## 题目: + +Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original. + +**Example:** + + Input: s = "abcdefg", k = 2 + Output: "bacdfeg" + +**Restrictions:** + +1. The string consists of lower English letters only. +2. Length of the given string and k will in the range [1, 10000] + +## 题目大意 + +给定一个字符串和一个整数 k,你需要对从字符串开头算起的每个 2k 个字符的前k个字符进行反转。如果剩余少于 k 个字符,则将剩余的所有全部反转。如果有小于 2k 但大于或等于 k 个字符,则反转前 k 个字符,并将剩余的字符保持原样。 + +要求: + +- 该字符串只包含小写的英文字母。 +- 给定字符串的长度和 k 在[1, 10000]范围内。 + + +## 解题思路 + +- 要求按照一定规则反转字符串:每 `2 * K` 长度的字符串,反转前 `K` 个字符,后 `K` 个字符串保持不变;对于末尾不够 `2 * K` 的字符串,如果长度大于 `K`,那么反转前 `K` 个字符串,剩下的保持不变。如果长度小于 `K`,则把小于 `K` 的这部分字符串全部反转。 +- 这一题是简单题,按照题意反转字符串即可。