[LeetCode]541. Reverse String II

限时加码!20+主流AI编程工具免费用 购周边加赠Coding Plan Lite,Claude Code、Cursor等即刻畅享,学习进阶更高效! 阅读详情

[LeetCode]541. Reverse String II

题目描述

这里写图片描述

思路

条件判断,符合条件逆转即可
(看答案有一行解决的)

代码

#include <iostream>
#include <string>

using namespace std;;

class Solution {
public:
    string reverseStr(string s, int k) {
        if (s.size() <= k)
            reverse(s.begin(), s.end());
        else if (s.size() > k && s.size() < k * 2) {
            for (int j = 0; j < k / 2; ++j)
                swap(s[j], s[k - j - 1]);
        }
        else {
            for (int i = 0; i < s.size(); ++i) {
                if (i % (k * 2) == 0 && i + k - 1 < s.size()) {
                    for (int j = 0; j < k / 2; ++j)
                        swap(s[i + j], s[i + k - j - 1]);
                }
                else if (i % (k * 2) == 0) {
                    int len = s.size() - i;
                    for (int j = 0; j < len / 2; ++j)
                        swap(s[i + j], s[i + len - j - 1]);
                }
            }

        }

        return s;
    }

};

int main() {
    Solution s;
    cout << s.reverseStr("hyzqyljrnigxvdtneasepfahmtyhlohwxmkqcdfehybknvdmfrfvtbsovjbdhevlfxpdaovjgunjqlimjkfnqcqnajmebeddqsgl", 39) << endl;
    system("pause");
}
LeetCode //C - 541. Reverse String II 【代码】LeetCode //C - 541. Reverse String II 阅读详情

相关推荐

[LeetCode] 344 Reverse String && 541 Reverse String II

原题地址: 344Reverse String: https://leetcode.com/problems/reverse-string/description/ 541Reverse String II: https://leetcode.com/problems/reverse-string-ii/description/ 题目&&解法: 1....

aixian8156的博客 143

Leetcode-541. Reverse String II

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。这次比赛略无语,没想到前3题都可以用暴力解。 博客链接:mcf171的博客 —————————————————————————————— Given a st

mcf171的专栏 2128

Leetcode541. Reverse String II

541. Reverse String II Easy 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 characte...

蒋志碧的博客 300

leetcode 541. Reverse String II

很基础的一道题: class Solution { public String reverseStr(String s, int k) { String str = ""; int count = s.length()%k==0 ? s.length()/k : s.length()/k+1; for(int i=1;i<=count;...

xiaobailaji的博客 192

[LeetCode] 541.Reverse String II

[LeetCode] 541.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 a

user_name_is_so_long的博客 296

leetcode 541 Reverse String II C++

这题没什么难的,读懂题就好了,注意k比字符串长和最后剩下的比k小的情况。 string reverseStr(string s, int k) { int pos = 0; while(pos < s.size()) { if (pos + k > s.size()) { k = s.size() -

wcxdell的专栏 1244

[leetcode]541. 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...

Brett 198

LeetCode - 541. Reverse String II

链接 541. Reverse String II 题意 翻转字符串II 给定一个字符串和k,k表示要翻转前k个字母,然后跳过k个字母,再翻转k个字母...以此类推。当字符个数少于2k但大于等于k个时,翻转前k个字符并且剩余的保持不变。 思路 循环调用翻转字符串的方法即可。 代码 public class Solution { public String reverseStr(S...

103

LeetCode 541. Reverse String II

这道题一开始看错了题目,以为只是把前2K个的k个字符反转就可以,后来wa了才发现是要把每一个2k个字符的前k个字符反转,改正了代码。class Solution { public: void inv(string &s, int t, int k){ int med = (k-t)/2 + t; for(int i = t; i <= med; i++)

jessir的专栏 907

Leetcode 541.反转字符串 IIReverse String II

Leetcode 541.反转字符串 II 1 题目描述(Leetcode题目链接)   给定一个字符串 s 和一个整数 k,你需要对从字符串开头算起的每隔 2k 个字符的前 k 个字符进行反转。 如果剩余字符少于 k 个,则将剩余字符全部反转。 如果剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样。 输入: s = "abcdefg", k = 2 输出: "bacdfeg" 提示: 该字符串只包含小写英文字母。 给定字符串的长度和 k 在 [1, 10000] 范围内

qq_39378221的博客 274

LeetCode 541 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 th

几百个测试用例一致通过 275

leetcode 541. Reverse String II 简单题也得多刷刷

leetcode  541. Reverse String II class Solution(object): def reverseStr(self, s, k): """ :type s: str :type k: int :rtype: str """ return s

独钓寒江雪 376

Leetcode 541. Reverse String II

文章作者:Tyan 博客:noahsnail.com &nbsp;|&nbsp; CSDN &nbsp;|&nbsp; 简书 1. Description 2. Solution class Solution { public: string reverseStr(string s, int k) { int index = 0; int range = ...

SnailTyan 328

leetcode 541. Reverse String II 反转字符串 II

该题目要求对字符串进行每隔2k个字符反转前k个字符的处理。解决方案使用双指针法,将字符串分割为多个2k长度的区间,对每个区间的前k个字符进行反转。通过维护左右指针l和r来划定处理范围,判断剩余字符数是否足够k个来决定反转范围。最后将处理后的子字符串拼接得到最终结果。时间复杂度O(n),空间复杂度O(n)。

邹九的个人博客 394
上一篇: [LeetCode]217. Contains Duplicate
下一篇: [LeetCode]149. Max Points on a Line
charon____
博客等级 码龄11年 3粉丝 202原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值