[leetcode-541-Reverse String II]
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]
思路:
以2*k 为一组,每一组前k个字符翻转,然后处理剩余字符。
写出来感觉好啰嗦。。另外简洁的代码看到大神有用stl的reverse的,回头试一下,省不少事儿。
string reverseStr(string s, int k)
{
int group = s.size()/(*k);
int i = ;
for (; i < group;i++)
{
for (int j = ; j < k/;j++)
{
swap(s[i * * k + j], s[i * * k + k-j-]);
}
}
int remain = s.size() % ( * k);
int end = (remain >= k) ? k : remain ;
for (int j = ; j < end/;j++)
{
swap(s[i * * k + j], s[i * * k + end - j - ]);
}
return s;
}
[leetcode-541-Reverse String II]的更多相关文章
- 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 ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- leadcode 541. Reverse String II
package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ...
- 【leetcode_easy】541. Reverse String II
problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ...
- 【LeetCode】541. Reverse String II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [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 ...
- 541 Reverse String II 反转字符串 II
给定一个字符串和一个整数 k,你需要对从字符串开头算起的每个 2k 个字符的前k个字符进行反转.如果剩余少于 k 个字符,则将剩余的所有全部反转.如果有小于 2k 但大于或等于 k 个字符,则反转前 ...
- 541. Reverse String II
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 541. Reverse String II 指定翻转前k个的字符串
[抄题]: Given a string and an integer k, you need to reverse the first k characters for every 2k chara ...
随机推荐
- JS中this到底指向谁?
关于this的指向,是一个令人很头疼的问题.但是,你运气好,碰到了我.老夫这儿有本祖传秘籍,看懂这个,妈妈再也不用担心你的this指向不对啦! 归根结底,this指向就一句话:谁最终调用函数,this ...
- 单片机IAP学习
1.IAP是什么--简介 IAP是In Application Programming的首字母缩写,IAP是用户自己的程序在运行过程中对User Flash的部分区域进行烧写,目的是为了在产品发布后可 ...
- SSH抛出org.apache.ibatis.exceptions.PersistenceException: 异常
抛出的异常类容如下 如果遇到这个异常,那么肯定是你在配置事物切面时出错,或者是你的写的事物的方法名称没有和这里的配置对应: 你需要注意如下几点: 1.你的名称必须是以英文开头 2.在你用着事物方法的名 ...
- Git版本控制,rsync同步文件,完成线上部署
之前项目开发完成,测试阶段,借着此时,由于公司暂时用两台aliyun ecs 做业务层,所以每次都需要同步线上文件,进而想着搞一搞服务器端(小公司,新项目,先小搞一把),搭建一套小的版本控制上线的 ...
- Git Flow Note
近期困惑于Git代码版本控制,集中两天时间研究,其中基础知识来源于<Git权威指南>,分支思想则来源于一篇博文<A successful Git branching model> ...
- cuda学习1-初始庐山真面目
cuda作为gpu计算中的代表,拥有着超级高的计算效率,其原因是gpu实际相当与一台超级并行机组,使用过MPI做并行计算的人们可能知道,所谓的并行计算,简单讲就是用多个U(计算单元)来完成一个U的计算 ...
- JQuery中参数e,event
与Flex类似,JavaScript中的事件也同样存在,捕获--触发--冒泡 三个节点.比较常见的情况是,在子DIV触发事件时,如果父DIV也监听同类事件,那么也会一起触发,并向上冒泡 jQuery对 ...
- ionic 使用mobisscrolls,实现日期选择的插件
废话不多说,直接说用法: 1,先下载mobisscrolls的破解版,下载地址,链接:http://pan.baidu.com/s/1boSKf51 密码:5dft 当然你也可以去官网下载,不过官网的 ...
- NodeJS在线聊天室(NodeJS & SocketIO & Express & EJS & MongoDB & Gulp)
项目背景 这个项目主要是为了玩玩NodeJS,项目的方向大概是做出类似QQ的在线聊天系统.想要在线体验可以点击在线演示. 项目使用PM2进行部署和管理,功能在不断的迭代开发中.如果你觉得这个项目比较有 ...
- js添加多个样式属性cssText
document.getElementById("box").style.cssText += ";color:red;font-size:20px"; 代码分 ...