static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
string reverseStr(string s, int k)
{
int len=s.length();
auto p=s.begin();
int i;
for(i=k;i<=len;i+=*k)
reverse(p+i-k,p+i);
i-=*k;
if(len==i)
return s;
else if(len-i>k)
reverse(p+i+k,s.end());
return s;
}
};

把数组截断,一部分一部分地逆置,以 i 为截断点,(2n-1)k处截断,截断点左侧k个元素要逆置。

在最后一截要分三类讨论,

第一类,i 刚好截到数组末尾,直接返回

第二类,i 左侧空缺元素个数小于k,即有不足k个要逆置的元素,逆置这部分元素即可

第三类,i 左侧空缺元素个数大于k,说明没有需要逆置的元素,直接返回即可

541. Reverse String II的更多相关文章

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

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

  2. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  3. leadcode 541. Reverse String II

    package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ...

  4. 【leetcode_easy】541. Reverse String II

    problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ...

  5. LeetCode 541. Reverse String II (反转字符串 II)

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

  6. [LeetCode&Python] Problem 541. Reverse String II

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

  7. 541. Reverse String II 指定翻转前k个的字符串

    [抄题]: Given a string and an integer k, you need to reverse the first k characters for every 2k chara ...

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

    给定一个字符串和一个整数 k,你需要对从字符串开头算起的每个 2k 个字符的前k个字符进行反转.如果剩余少于 k 个字符,则将剩余的所有全部反转.如果有小于 2k 但大于或等于 k 个字符,则反转前 ...

  9. [LC] 541. Reverse String II

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

随机推荐

  1. leetcode447

    public class Solution { /// <summary> /// 计算两个点的距离 /// </summary> /// <param name=&qu ...

  2. 打印机 KX-MB788CN 佳能

    打印机 KX-MB788CN http://panasonic.cn/oa/help/download.asp?type=drivers&pid=1066 佳能打印机 腾彩 PIXMA MP2 ...

  3. c++ static成员

    static 成员通常不能在类的定义体重初始化 有一种例外,const static成员可以在定义体内初始化,并且可以用于构造函数 将函数声明为const表示该函数不能修改其所属的对象

  4. eclipse 注释字体不一致的问题

    eclipse中 1.解决注释的文字大小不一的情况 2.想让注释和代码大小不一样 3.win10系统下,设置Text Font时找不到Courier New字体 1.解决注释的文字大小不一的情况 打开 ...

  5. mongodb基础学习3-查询的复杂用法

    昨天看了一下查询,今天来说下查询的复杂用法,可以类比mysql的查询 $ne:不等于 $gt, $gte, $lt, $lte:大于,大于等于,小于,小于等于 $in $and $nor:相当于上面的 ...

  6. ibernate 配置数据库方言

          在开发hibernate的程序时,需要进行SessionFactory的配置,简单地说,也就是建立与数据库之间连接的配置,在hibernate中一般使用xml文件来进行配置,但是在该文件的 ...

  7. out对象以及网上答题系统

    out对象的主要功能是向客户输出响应信息,其主要方法为“print()”,可以输出任意类型的数据,HTML标记可以作为out输出的内容. 代码: 程序截图

  8. scikit Flow ,tensor flow 做ml模型

    [https://github.com/ilblackdragon/tf_examples/blob/master/titanic.py] [keras 高层tensorflow] https://k ...

  9. oracle表属性

    1. PCTFREE 要形容一个 BLOCK 的运作,我们可以把一个 BLOCK 想成一个水杯.侍者把水倒入放在我们面前的水杯,要多满呢,我们要求他倒 9 分满好了,这时候 PCTFREE 代表着设定 ...

  10. Windows自带的端口转发工具netsh使用方法_DOS/BAT

    Windows自带的端口转发工具netsh使用方法_DOS/BAT   作者:用户 来源:互联网 时间:2017-02-22 17:24:30 netsh 端口转发 摘要: 下面的代码在windows ...