541. Reverse String II
static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
string reverseStr(string s, int k)
{
int len=s.length();
auto p=s.begin();
int i;
for(i=k;i<=len;i+=*k)
reverse(p+i-k,p+i);
i-=*k;
if(len==i)
return s;
else if(len-i>k)
reverse(p+i+k,s.end());
return s;
}
};
把数组截断,一部分一部分地逆置,以 i 为截断点,(2n-1)k处截断,截断点左侧k个元素要逆置。
在最后一截要分三类讨论,
第一类,i 刚好截到数组末尾,直接返回
第二类,i 左侧空缺元素个数小于k,即有不足k个要逆置的元素,逆置这部分元素即可
第三类,i 左侧空缺元素个数大于k,说明没有需要逆置的元素,直接返回即可
541. Reverse String II的更多相关文章
- [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 (反转字符串 II)
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
- [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 指定翻转前k个的字符串
[抄题]: Given a string and an integer k, you need to reverse the first k characters for every 2k chara ...
- 541 Reverse String II 反转字符串 II
给定一个字符串和一个整数 k,你需要对从字符串开头算起的每个 2k 个字符的前k个字符进行反转.如果剩余少于 k 个字符,则将剩余的所有全部反转.如果有小于 2k 但大于或等于 k 个字符,则反转前 ...
- [LC] 541. Reverse String II
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
随机推荐
- HTML5 Canvas ( 贝塞尔曲线, 一片星空加绿地 ) quadraticCurveTo, bezierCurveTo
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ASP.Net在web.config中设置上传文件的大小方法
修改Webcong文件:<system.web><httpRuntime maxRequestLength="40960" //即40MB,1KB=1024u ...
- leetcode171
public class Solution { private int ConvertToC(char c) { ; switch (c) { case 'A': case 'a': rnt = ; ...
- cookie和session的比较
cookie和session的比较 一.对于cookie: ①cookie是创建于服务器端 ②cookie保存在浏览器端 ③cookie的生命周期可以通过cookie.setMaxAge(2000); ...
- rem 响应 js函数
size();window.onresize = function(){ size();}function size(){ var htnl_o=document.getElementsByTagNa ...
- Haskell语言学习笔记(67)Gtk2Hs
Gtk2Hs $ brew cask install xquartz $ brew install glib cairo gtk gettext fontconfig freetype $ expor ...
- mysql 列转行
第一种方法:使用序列化表的方法实现列转行 第一种方法:使用UNION的方法实现列转行 第二种方法:使用序列化表的方法实现列转行
- 安装Anaconda以及jupyter的使用
1)下载https://www.anaconda.com/download/ 2)安装 3)终端查看(Anaconda Prompt) 4)升级所有的包 初次安装下的软件包版本一般都比较老旧,因此提前 ...
- linux下mongodb授权登录
mongodb版本为3.2(目前最新),演示的是linux下的mongodb授权认证 第一次登录不启动授权(mongo默认不启动) ./mongod --dbpath=/home/db/data -- ...
- global statement
[global statement] 在线程里,默认所有变量都是本线程局部变量,要想访问全局变量,则要先用global声明. 如全局有变量totalCount,线程中有语句 totalCount += ...