541.Reverse String II(String-Easy)
来源:互联网 发布:app应用市场源码 编辑:程序博客网 时间:2024/05/16 02:07
转载请注明作者和出处: http://blog.csdn.net/c406495762
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]
题目:根据k,反转字符串。
/** * 0 k 2k 3k * |-----------|-----------|-----------|--- * +--reverse--+ +--reverse--+ */
每2k个字符,反转前面的k个字符。如果少于k个字符,反转所有字符。如果大于等于k个字符小于2k个字符,那么反转前面的k个字符,剩余的字符保留不动。
思路:使用reverse函数进行反转。代码依旧很简单。
Language : cpp
class Solution {public: string reverseStr(string s, int k) { for(int i = 0; i < s.size(); i += 2*k){ reverse(s.begin()+i, min(s.begin()+i+k, s.end())); } return s; }};
0 0
- 541.Reverse String II(String-Easy)
- leetcode 541. Reverse String II(easy)
- String:541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- MySQL 触发器
- iw工具安卓移植
- EOS Governor各类配置信息以及具体使用场景和含义
- 研究Bitmap
- allegro 16.6 出gerber时drill文件出问题及解决
- 541.Reverse String II(String-Easy)
- 解决Unable toconnect to a repository at URL 禁止访问(forbidden)
- CentOS 6.5 下载地址
- docker应用-5(使用overlay 网络进行容器间跨物理主机通信)
- 链栈的基本操作
- 【C#】txt的读写
- 银行操作系统简单交易
- Intel80x86内存寻址
- git学习-- -fatal: 不是一个有效的对象名:'master'。