🚀 打造你的开发者个人IP
掌握算法是成功的基石,而全方位展示你的才华则是获得垂青的关键。
我的另一个项目 leader.me —— 专为程序员打造的“全能型”个人品牌展示平台。
三位一体(All-In-One)的职场利器:
- 📄 简历 + 作品集 + 博客: 将你的 GitHub 项目、技术心得与职场经历完美融合。
- 🌐 永久免费自定义域名: 支持绑定你自己的独立域名,且该功能永久免费。
- ✨ 顶级行业子域名: 提供
name.leader.me,极具职业含金量的专属域名。
访问原文链接:541. 反转字符串 II - LeetCode Python/Java/C++/JS/C#/Go/Ruby 题解,体验更佳!
力扣链接:541. 反转字符串 II, 难度等级:简单。
给定一个字符串 s 和一个整数 k,从字符串开头算起,每计数至 2k 个字符,就反转这 2k 字符中的前 k 个字符。
- 如果剩余字符少于
k个,则将剩余字符全部反转。 - 如果剩余字符小于
2k但大于或等于k个,则反转前k个字符,其余字符保持原样。
输入: s = "abcdefg", k = 2
输出: bacdfeg
输入: s = "abcd", k = 2
输出: bacd
1 <= s.length <= 10000sconsists of only lowercase English letters.1 <= k <= 10000
- 题目没有要求
原地反转,所以用一个新的字符串result作为返回值,操作起来容易些。 - 在循环中,步进值用
k比2k更方便,因为如果用2k,还是要再用k做判断。 - 要求只反转
2k字符中前k个字符,所以需要一个布尔类型变量should_reverse作为是否要反转判断条件。
-
用一个新的字符串
result作为返回值。在循环中,步进值用k。result = '' index = 0 while index < s.size k_chars = s[index...index + k] result += k_chars index += k end return result
-
用布尔类型变量
should_reverse作为是否要反转判断条件,并只反转2k字符中前k个字符。result = '' should_reverse = true # 1 index = 0 while index < s.size k_chars = s[index...index + k] if should_reverse # 2 result += k_chars.reverse # 3 else # 4 result += k_chars end index += k should_reverse = !should_reverse # 5 end return result
- 时间复杂度:
O(N). - 空间复杂度:
O(N).
class Solution:
def reverseStr(self, s: str, k: int) -> str:
result = ''
should_reverse = True
index = 0
while index < len(s):
k_chars = s[index:index + k]
if should_reverse:
result += k_chars[::-1]
else:
result += k_chars
index += k
should_reverse = not should_reverse
return resultclass Solution {
public String reverseStr(String s, int k) {
var result = new StringBuffer();
var shouldReverse = true;
var index = 0;
while (index < s.length()) {
var kChars = s.substring(index, Math.min(index + k, s.length()));
if (shouldReverse) {
result.append(new StringBuffer(kChars).reverse());
} else {
result.append(kChars);
}
index += k;
shouldReverse = !shouldReverse;
}
return result.toString();
}
}var reverseStr = function (s, k) {
let result = ''
let shouldReverse = true
let index = 0
while (index < s.length) {
const kChars = s.substr(index, k)
if (shouldReverse) {
result += [...kChars].reverse().join('')
} else {
result += kChars
}
index += k
shouldReverse = !shouldReverse
}
return result
};public class Solution
{
public string ReverseStr(string s, int k)
{
string result = "";
bool shouldReverse = true;
int index = 0;
while (index < s.Length)
{
string kChars = s[index..Math.Min(index + k, s.Length)];
if (shouldReverse)
{
result += new string(kChars.Reverse().ToArray());
}
else
{
result += kChars;
}
index += k;
shouldReverse = !shouldReverse;
}
return result;
}
}def reverse_str(s, k)
result = ''
should_reverse = true
index = 0
while index < s.size
k_chars = s[index...index + k]
if should_reverse
result += k_chars.reverse
else
result += k_chars
end
index += k
should_reverse = !should_reverse
end
result
endclass Solution {
public:
string reverseStr(string s, int k) {
string result = "";
bool shouldReverse = true;
int index = 0;
while (index < s.length()) {
auto kChars = s.substr(index, k);
if (shouldReverse) {
reverse(kChars.begin(), kChars.end());
}
result += kChars;
index += k;
shouldReverse = !shouldReverse;
}
return result;
}
};func reverseStr(s string, k int) string {
var result []rune
shouldReverse := true
index := 0
for index < len(s) {
end := index + k
if end > len(s) {
end = len(s)
}
kChars := []rune(s[index:end])
if shouldReverse {
for i, j := 0, len(kChars) - 1; i < j; i, j = i + 1, j - 1 {
kChars[i], kChars[j] = kChars[j], kChars[i]
}
}
result = append(result, kChars...)
index += k
shouldReverse = !shouldReverse
}
return string(result)
}// Welcome to create a PR to complete the code of this language, thanks!🚀 打造你的开发者个人IP
掌握算法是成功的基石,而全方位展示你的才华则是获得垂青的关键。
我的另一个项目 leader.me —— 专为程序员打造的“全能型”个人品牌展示平台。
三位一体(All-In-One)的职场利器:
- 📄 简历 + 作品集 + 博客: 将你的 GitHub 项目、技术心得与职场经历完美融合。
- 🌐 永久免费自定义域名: 支持绑定你自己的独立域名,且该功能永久免费。
- ✨ 顶级行业子域名: 提供
name.leader.me,极具职业含金量的专属域名。
访问原文链接:541. 反转字符串 II - LeetCode Python/Java/C++/JS/C#/Go/Ruby 题解,体验更佳!
GitHub 仓库: leetcode-python-java.