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. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.

Example:

Input: s = "abcdefg", k = 2
Output: "bacdfeg"

Restrictions:

  1. The string consists of lower English letters only.
  2. Length of the given string and k will in the range [1, 10000]

这道题是之前那道题Reverse String的拓展,同样是翻转字符串,但是这里是每隔k隔字符,翻转k个字符,最后如果不够k个了的话,剩几个就翻转几个。比较直接的方法就是先用n/k算出来原字符串s能分成几个长度为k的字符串,然后开始遍历这些字符串,遇到2的倍数就翻转,翻转的时候注意考虑下是否已经到s末尾了,参见代码如下:

解法一:

class Solution {
public:
string reverseStr(string s, int k) {
int n = s.size(), 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;
}
};

在论坛里又发现了写法更为简洁的方法,就是每2k个字符来遍历原字符串s,然后进行翻转,翻转的结尾位置是取i+k和末尾位置之间的较小值,感觉很叼,参见代码如下:

解法二:

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;
}
};

类似题目:

Reverse String

参考资料:

https://discuss.leetcode.com/topic/82652/one-line-c/2

https://discuss.leetcode.com/topic/82626/java-concise-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Reverse String II 翻转字符串之二的更多相关文章

  1. [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

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

  3. LeetCode Reverse String II

    原题链接在这里:https://leetcode.com/problems/reverse-string-ii/#/description 题目: Given a string and an inte ...

  4. [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  5. [LeetCode] Flip Game II 翻转游戏之二

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

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

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

  7. [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  8. [LeetCode] Reverse String 翻转字符串

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

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

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

随机推荐

  1. 复习C#

    (1)public共有访问.该修饰符可用于类和结构的成员,可用于命名空间下直接定义的类型,对于类和结构成员,如果声明为共有的,那么除自身的成员,外部成员也可以访问 (2)private限制为私有访问. ...

  2. CentOS7搭建solr7.2

    solr介绍 一.Solr它是一种开放源码的.基于 Lucene Java 的搜索服务器,易于加入到 Web 应用程序中. 二.Solr 提供了层面搜索(就是统计).命中醒目显示并且支持多种输出格式( ...

  3. 计算1-1/3+1/5-1/7+···的前n项和

    这图1为书里的教材,图二为自己打的程序 (1)二者相比,自己写的代码显得更短,听说代码写的越精简越好,但是自己的较难分析,他人看来可能会较难理解一点:(自己在第一次运行时将for()中的第二个表达式写 ...

  4. 基于scrapy爬虫的天气数据采集(python)

    基于scrapy爬虫的天气数据采集(python) 一.实验介绍 1.1. 知识点 本节实验中将学习和实践以下知识点: Python基本语法 Scrapy框架 爬虫的概念 二.实验效果 三.项目实战 ...

  5. 201621123031 《Java程序设计》第2周学习总结

    Week02-Java基本语法与类库 1. 本周学习总结 本周讲了Java的基本数据类型,主要分为八类(byte,short,int,long,double,float,char,boolean),其 ...

  6. slf4j 与 log4j2 基本用法

    简单的说 log4j2 是log4j2的升级版,解决了部分性能问题和部分死锁问题,其使用方式与使用配置与log4j相同. 建议使用maven依赖直接使用log4j2 <dependency> ...

  7. JAVA_SE基础——63.String类的常用方法

    获取方法int length()  获取字符串的长度char charAt(int index) 获取特定位置的字符 (角标越界)int indexOf(String str) 查找子串第一次出现的索 ...

  8. Linq 等式运算符:SequenceEqual

    检查元素的数量,每个元素的值及两个集合中元素的顺序是否相等,3个方面都相等则为true,否则为false IList<string> strList1 = new List<stri ...

  9. Python内置函数(55)——globals

    英文文档: globals() Return a dictionary representing the current global symbol table. This is always the ...

  10. 如何排查CPU飙升的Java问题

    1. JPS 查看jvm进程 2. 显示线程列表 ps -mp pid -o THREAD,tid,time 找到了耗时最高的线程tid 3. tid转换成16进制 printf "%x\n ...