[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 ...
随机推荐
- [笔记]A*寻路算法初探
写在开始之前 最近突然对各路游戏的寻路算法很感兴趣,于是去学习了下游戏里的AI们是如何寻路的.网上相关内容很多,但同时有些说法也不一,制作自己的A* 算法时也有因不同的说法而困惑.整理多方资料并自己实 ...
- 使用Cmder的几个问题
Cmder 全尺寸版本 [101022] 新版本的 Cmder Full 版本,安装包目录的 config 目录下,已经没有 aliases 文件,在 vendor 下的 init.bat 下也没有了 ...
- asp.net Socket的简单Web Server
1.首先初始化socket,包含对端点以及对连接队列长度的初始化 IPAddress address = IPAddress.Loopback; IPEndPoint endPoint = ); So ...
- 【原创】JQWidgets-TreeGrid 2、初探源码
已知JQWidgets的TreeGrid组件依赖于jqxcore.js.jqxtreegrid.js,实际上它还依赖于jqxdatatable.js.我们先通过一个例子,来探索本次的话题. 需求: 图 ...
- spring boot入门(一)自己动手搭建spring boot
spring boot官方文档 http://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/html/index.html 此篇文章 ...
- 如何用php实现简单的文件上传功能?(带图解)
如图所示:点击浏览出现选择文件的对话框,将所选文件上传到保存文件的文件. 关键点:文件上传的图解: 代码: <!DOCTYPE html> <html> <head&g ...
- 模板不存在:./xx 错误位置 FILE: LINE:110 (thinkphp上传至服务器后模板无法解析原因)
thinkphp上传至服务器后模板无法解析原因 前几日做好的响应式静态页面上传至虚拟空间,打开网址地址出现: 模板不存在:./App/Admin/View/Config/customerService ...
- Java中的局部变量表及使用jclasslib进行查看
直接上下载地址 jclasslib是一个独立的工具,不是包含在JDK中的工具,需要自己进行下载,下载地址如下: http://downfile.downcc.com/down/JClassLib_wi ...
- MySQL锁(MyISAM和InnoDB)
MySQL有三种级别的锁: 1. 页级别 BDB 2. 表级别 MyISAM 3. 行级别 InnoDB 就 总体而言MyISAM表的读和写是串行的.在一定条件下,MyISAM表也支持查询和插入操作的 ...
- Hibernate与Jpa的关系(1)
[转自:http://freewind.me/blog/20111129/588.html ] 我知道Jpa是一种规范,而Hibernate是它的一种实现.除了Hibernate,还有EclipseL ...