题目:
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:
The string consists of lower English letters only.
Length of the given string and k will in the range [1, 10000]

思路:
每2k个字符是一组,进行处理。
若剩下的字符小于k个,则逆序所有字符,直接返回s;
若剩下的字符大于等于k且小于2k个,则逆序前k个字符,直接返回s;
若剩下的字符大于2k个,则逆序前k个字符后,再将索引i往后移动2*k位。
循环结束后,如果没有返回,这种情况是字符串长度刚好是2k的倍数,则再返回s。

代码:

class Solution {
public:string reverseStr(string s, int k) {int len=s.length();if(len<=2){//如果s的长度小于等于2,直接返回sreturn s;}int i=0;//设置索引iwhile(i<len){//如果索引i小于len时if(i+k-1>=len){//如果剩下的字符小于k个reverse(s.begin()+i,s.end());//逆序所有剩下字符return s;//直接返回s}else if(i+k-1<=len&&i+2*k-1>=len){//剩下的字符大于等于k且小于2k个reverse(s.begin()+i,s.begin()+i+k);//则逆序前k个字符return s;//直接返回s} else{//其实是如果剩下的字符大于2k个reverse(s.begin()+i,s.begin()+i+k);//则逆序前k个字符i+=2*k;//索引i往后移动2*k位}}return s;//这种情况是字符串长度刚好是2k的倍数}
};

**输出结果:**9ms

LeetCode 541. Reverse String II相关推荐

  1. LeetCode之Reverse String II

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

  2. C#LeetCode刷题之#541-反转字符串 II(Reverse String II)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3951 访问. 给定一个字符串和一个整数 k,你需要对从字符串开头 ...

  3. 定间隔字符翻转(Reverse String II)

    一.学习要点: 1.主要是对剩余字符长度的考虑,大于k的时候,对前k个进行翻转:小于k的时候,对实际个数进行全翻转:本程序中翻转个数用变量x来表示: 二.代码: #include<stdlib. ...

  4. LeetCode之Reverse String

    1.题目: Write a function that takes a string as input and returns the string reversed. Example: Given ...

  5. LeetCode 541. 反转字符串 II

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

  6. LeetCode 344.Reverse String

    题意: Write a function that takes a string as input and returns the string reversed. Example: Given s ...

  7. LeetCode: 344. Reverse String

    051102 题目 Write a function that reverses a string. The input string is given as an array of characte ...

  8. LeetCode 344. Reverse String

    题目: Write a function that takes a string as input and returns the string reversed. Example: Given s ...

  9. leetcode 344. 反转字符串 541. 反转字符串 II 双指针解

    目录 leetcode 344.反转字符串 1.题目 2.思考 leetcode 541. 反转字符串 II 1.题目 2.思考 leetcode 344.反转字符串 1.题目 2.思考 典型的双指针 ...

最新文章

  1. [网络应用]Flash中鼠标手势及Ctrl+T问题{Firefox}
  2. 某程序员吐槽:妈妈到处炫耀自己年薪三十万,老家亲戚纷纷向自己借钱,已借出十几万!怎么办?网友:反借回去!...
  3. 安装Ubuntu 出现 SQUASFS error / sd 8:0:0:0 :[sdb] assuming drive cache:write through
  4. VS2015 MFC属性页孙鑫笔记
  5. 什么是Cortex、ARMv8、arm架构、ARM指令集、soc
  6. 哈希表 - (代码、分析 )
  7. spring security 2.x HttpSessionEventPublisher 以及listener配置
  8. Activiti 流程实例、任务、执行对象及相关的表
  9. Unicode字符集下UTF-8 、UTF-16编码和GBK(GB2312)字符集
  10. 网络工程项目标书模板
  11. 怎么通过SPSS的神经网络模型预测结果
  12. Activiti7讲义
  13. phpmyadmin 4.8.1 Remote File Inclusion Vulnerability (CVE-2018-12613)漏洞复现
  14. 很牛的几篇圈内爆料——影视圈
  15. [CF1603D]Artistic Partition
  16. 量子通信与计算机网络,虽然量子计算机仍然是个梦想,但是量子通信时代已经到来...
  17. matlab对ADC动态性能进行分析
  18. 免费馅饼 (DP动态规划问题详细解析)
  19. 计算机科学专刊的区别,EI和SCI期刊的正刊和专刊有区别吗
  20. ValueError: Length mismatch: Expected axis has 4 elements, new values have 3 elements

热门文章

  1. rip协议中周期性广播路由信息的报文_距离矢量路由协议-RIP
  2. 数字电路实验怎么接线视频讲解_利达:气体灭火接线示意图
  3. C/C++:各类型变量占用字节
  4. linux内核那些事之物理内存模型之DISCONTIGMEM(2)
  5. jQuery-事件和应用
  6. docker 学习笔记一(教程) 快速上手
  7. WordPress简约响应式自媒体资讯博客主题Qui-Pure v5.25
  8. python一行输出多个数据_Python高效数据分析的8个技巧
  9. 区块链JAVA数字交易所官方商业版开发级全套三端纯源码
  10. 超好看的自适应蜘蛛池官网首页源码