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 ...
随机推荐
- docker 带参数启动 配合springboot profile
dockerfile FROM frolvlad/alpine-oraclejdk8:slim VOLUME /tmp ADD test-push-service--SNAPSHOT.jar app. ...
- HTML5 Canvas ( 文字的度量 ) measureText
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- AS3 - 数组元素乱序方法以及效率比较
http://www.hangge.com/blog/cache/detail_453.html
- as3 string split方法一些注意
split () 方法 AS3 function split(delimiter:*, limit:Number = 0x7fffffff):Array 如果指定 limit 参数,返回的数组中具有的 ...
- 18 subprocess模块(跟操作系统交互)
1.基本概念介绍 我们经常需要通过Python去执行一条系统命令或脚本,系统的shell命令是独立于你的python进程之外的, 每执行一条命令,就是发起一个新进程,通过python调用系统命令或脚本 ...
- C++Primer笔记-----day08
==========================================================================day08===================== ...
- Enum学习中的compareTo方法分析
今天看工厂模式的时候里面用了枚举定义各种可能的实例类型,就看了一下枚举,发现里面有一个compareTo(E o)方法 通过Object的getClass()方法比较两个两个比校对象类型是否一致,如果 ...
- map模块使用方法
map指令使用ngx_http_map_module模块提供的.默认情况下,nginx有加载这个模块,除非人为的 --without-http_map_module.ngx_http_map_modu ...
- htop工具使用
1 防止进程重复显示 在使用htop查看进程信息的时候,经常会出现很多个进程重复的情况,如下情况: 经研究发现,htop会把一个进程里的线程当做一个进程来显示出来,上图中的java进程有多个线程,所以 ...
- Virtualbox [The headers for the current running kernel were not found] (操作过程后还是失败,显示相同问题)
在笔记本安装Ubuntu11.04增强功能失败 引用 fuliang@fuliang-VirtualBox:~$ sudo /etc/init.d/vboxadd setup Removing exi ...