problem

541. Reverse String II

题意:

给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符。

solution1:

class Solution {
public:
string reverseStr(string s, int k) {
int n = s.size();
int cnt = n / k;
for(int i=; i<=cnt; i++)
{
if(i%==)
{
if(i*k+k<n) reverse(s.begin()+i*k, s.begin()+i*k+k);
else reverse(s.begin()+i*k, s.end());
}
}
return s;
}
};

solution2: 简洁版

就是每2k个字符来遍历原字符串s,然后进行翻转,翻转的结尾位置是取i+k和末尾位置之间的较小值,非常666。

class Solution {
public:
string reverseStr(string s, int k) {
for(int i=; i<s.size(); i+=*k)
{
reverse(s.begin()+i, min(s.begin()+i+k, s.end()));
}
return s;
}
};

参考

1. Leetcode_easy_541. Reverse String II;

2. Grandyang;

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

  1. 【LeetCode】541. Reverse String II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

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

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

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

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

  4. leadcode 541. Reverse String II

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

  5. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

  6. 【leetcode】344. Reverse String

    problem 344. Reverse String solution: class Solution { public: void reverseString(vector<char> ...

  7. 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 ...

  8. 【leetcode_easy】557. Reverse Words in a String III

    problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...

  9. [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 ...

随机推荐

  1. Cairo

    Cairo 英[ˈkaɪrəʊ] 美[ˈkaɪroʊ] n. 开罗(埃及首都); [例句]From Cairo came expressions of regret at the attac

  2. 操作系统-chapter1

    课程:https://mooc.study.163.com/learn/1000002004?tid=2402971010&_trace_c_p_k2_=f79694c7fc04429bb9b ...

  3. Maven 设置阿里镜像

    Maven 默认的中央仓库速度慢,可以考虑换成阿里的镜像.修改方式主要有两种. 1.针对所有项目修改中央仓库Maven 提供了全局配置文件 settings.xml 针对所有项目有效,位置是在安装目录 ...

  4. char 类型的数组转换到CSting

    首先,字符串“abc”在CString的保存格式是‘a’,'\0','b','\0','c','\0','\0','\0'; 从中可以看出它是以‘\0’,'\0',结束的. 当char ch[6]: ...

  5. 虚拟机Linux无法查看本地ip地址 解决办法

    解决方案: 1.虚拟机与本机采用的连接方式为:Host-only模式,其中几种连接模式的区别我不做介绍,自己百度.如果之前连接方式不为Host-only,更改之后需要重新启动虚拟机. 2.将本机的两块 ...

  6. Appium获取toast消息

    Android获取toast,需要在参数里设置automationName:Uiautomator2 设置设备的信息 desired_caps = { 'platformName': 'Android ...

  7. AcWing:244. 谜一样的牛(树状数组 + 二分)

    有n头奶牛,已知它们的身高为 1~n 且各不相同,但不知道每头奶牛的具体身高. 现在这n头奶牛站成一列,已知第i头牛前面有AiAi头牛比它低,求每头奶牛的身高. 输入格式 第1行:输入整数n. 第2. ...

  8. Git生成本机SSH Key并添加到GitHub中

    1.检查电脑里是否有SSH Key 打开git Bash客户端 cd ~/.ssh ls 如果有就会输出下面内容 config id_rsa id_rsa.pub known_hosts 2.创建 邮 ...

  9. iptables 查看对应规则及端口号

    iptables -L -n --line-number

  10. sql注入笔记-mysql

    整理下sql相关知识,查漏补缺(长期更新) 1 常用语句及知识 information_schema包含了大量有用的信息,例如下图 mysql.user下有所有的用户信息,其中authenticati ...