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. JQuery 遍历 操作数组 map、grep、filter 的区别

    filter() 方法将匹配元素集合缩减为匹配指定选择器的元素.例如:改变所有 div 的颜色,然后向类名为 "middle" 的类添加边框:$("div"). ...

  2. java- 泛型类到底是类还是接口,<T extends Comparable>的写法中为什么没有用implements

    java-core P533 public static <T extands Comparable> T min(T[] a ) 这里产生了一个疑问,就是 泛型类的英文是generic ...

  3. Java锁--非公平锁

    转载请注明出处:http://www.cnblogs.com/skywang12345/p/3496651.html 参考代码 下面给出Java1.7.0_40版本中,ReentrantLock和AQ ...

  4. django2为什么要用序列化

    https://www.jianshu.com/p/1e404604d5c3 结论:如果没用序列化,Django默认将数据进行多次转换才能提取我们所需数据,出现关联时我们还需进一步对数据进行查询,操作 ...

  5. linux mint 19 打开 Windows 下制作的 TXT 文件时‘乱码’

    因为 Linux 采用的是 UTF-8 编码,Windows 的中文编码是 GB18030. 解决的办法:让 Linux 的文本编辑器支持 GB18030 1.我们安装一个小软件"Dconf ...

  6. P4568 [JLOI2011]飞行路线 分层图最短路

    思路:裸的分层图最短路 提交:1次 题解: 如思路 代码: #include<cstdio> #include<iostream> #include<cstring> ...

  7. leetcode解题报告(8):Remove Element

    描述 Given an array and a value, remove all instances of that value in place and return the new length ...

  8. JXOJ 9.7 NOIP 放松模拟赛 总结

    比赛链接 T1 数数 题意:有a个红球,b个黄球,c个蓝球,d个绿球排成一列,求任意相邻不同色的排列的数目 ​ 1 <= a , b, c, d <= 30 答案对1e9 + 7 取膜 用 ...

  9. Noip2012 提高组 Day1 T1 Vigenère 密码

    题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简单易用,且破译难度比较高,曾在美国南 ...

  10. webstorm双击鼠标出现光标块

    将Vim 选项对勾去掉