C#LeetCode刷题之#541-反转字符串 II(Reverse String II)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3951 访问。
给定一个字符串和一个整数 k,你需要对从字符串开头算起的每个 2k 个字符的前k个字符进行反转。如果剩余少于 k 个字符,则将剩余的所有全部反转。如果有小于 2k 但大于或等于 k 个字符,则反转前 k 个字符,并将剩余的字符保持原样。
输入: s = "abcdefg", k = 2
输出: "bacdfeg"
要求:
该字符串只包含小写的英文字母。
给定字符串的长度和 k 在[1, 10000]范围内。
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.
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]
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3951 访问。
public class Program {
public static void Main(string[] args) {
var s = "abcdefg";
var k = 2;
var res = ReverseStr(s, k);
Console.WriteLine(res);
s = "abcdefg";
k = 3;
res = ReverseStr2(s, k);
Console.WriteLine(res);
s = "abcdefg";
k = 8;
res = ReverseStr3(s, k);
Console.WriteLine(res);
Console.ReadKey();
}
private static string ReverseStr(string s, int k) {
if(k > s.Length) return new string(s.Reverse().ToArray());
var nums = Math.Ceiling(s.Length / (double)k);
var sb = new StringBuilder(s);
for(var i = 0; i < nums; i += 2) {
var start = i * k;
var end = Math.Min(s.Length - 1, start + k - 1);
while(start < end) {
var swap = sb[start];
sb[start] = sb[end];
sb[end] = swap;
start++;
end--;
}
}
return sb.ToString();
}
private static string ReverseStr2(string s, int k) {
var chars = s.ToCharArray();
var offset = 2 * k;
for(var i = 0; i < chars.Length; i += offset) {
if(i + k > chars.Length) Array.Reverse(chars, i, chars.Length - i);
else Array.Reverse(chars, i, k);
}
return new string(chars);
}
private static string ReverseStr3(string s, int k) {
var list = new List<string>();
for(var i = 0; i < s.Length; i += k) {
if(i + k < s.Length) list.Add(s.Substring(i, k));
else list.Add(s.Substring(i, s.Length - i));
}
return string.Join("", list.Select((v, i) => i % 2 == 0 ? new string(v.Reverse().ToArray()) : v));
}
}
以上给出3种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3951 访问。
bacdfeg
cbadefg
gfedcba
分析:
显而易见,以上3种算法的时间复杂度均为 。
C#LeetCode刷题之#541-反转字符串 II(Reverse String II)的更多相关文章
- C#LeetCode刷题之#344-反转字符串(Reverse String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3933 访问. 编写一个函数,其作用是将输入的字符串反转过来. 输 ...
- C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3770 访问. 给定两个字符串 s 和 t,判断它们是否是同构的. ...
- LeetCode 541. 反转字符串 II(Reverse String II)
541. 反转字符串 II 541. Reverse String II
- C#LeetCode刷题之#557-反转字符串中的单词 III(Reverse Words in a String III)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3955 访问. 给定一个字符串,你需要反转字符串中每个单词的字符顺 ...
- C#LeetCode刷题之#345-反转字符串中的元音字母(Reverse Vowels of a String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...
- C#LeetCode刷题之#859-亲密字符串(Buddy Strings)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3973 访问. 给定两个由小写字母构成的字符串 A 和 B ,只要 ...
- 说了你可能不信leetcode刷题局部链表反转D92存在bug,你看了就知道了
一.题目描述 找出数组中重复的数字 > 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...
- leetcode刷题七<整数反转>
给出一个 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 : 输入: 输出: 示例 : 输入: - 输出: - 示例 : 输入: 输出: 假设我们的环境只能存储得下 32 位的有符号整 ...
- C#LeetCode刷题之#443-压缩字符串(String Compression)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3943 访问. 给定一组字符,使用原地算法将其压缩. 压缩后的长度 ...
随机推荐
- python利用selenium(webdriver chrome)模拟登陆获取cookie
(我是在windows下进行实验的) 准备工作: 1.安装python环境. 2.python安装selenium插件(执行以下命令就行). pip install selenium 3.Wind ...
- [Antd-vue] Warning: You cannot set a form field before rendering a field associated with the value.
在用ant-design-vue的框架中,使用到了这种场景,就是点击编辑按钮,弹出modal模态框,渲染modal模态框中的form表单页面,并给表单赋值,但是在给表单赋值的时候,总是会报错. 错误提 ...
- react实战 : react 与 svg
有一个需求是这样的. 一个组件里若干个区块.区块数量不定. 区块里面是一个波浪效果组件,而这个一般用 SVG 做. 所以就变成了在 react 中使用 SVG 的问题. 首先是波浪效果需要的样式. . ...
- 3.新手建站教程系列之认识WordPress和第一篇文章
上一期咱已经把本地环境和wp网站给搭建出来了,接下来就是来认识这个程序了.进入网站后台,地址为你的网址/wp-admin 后台名字叫做仪表盘,首页是一个信息合集区域,上面会显示有多少文章,多少页面以及 ...
- 因为喜欢所以升级,MyStaging-3.0 继续
我为什么维护MyStaging 目前该项目只有我一个人在维护,权当学习交流.为什么要继续维护呢,说一千道一万,还是因为喜欢,由于他的简单易用,从而促使我决定对 MyStaging 进行升级,目前 3. ...
- 题解 CF785E 【Anton and Permutation】
考虑用分块解决这个题,一次交换对当前逆序对个数的影响是,加上两倍的在区间\([l+1,r-1]\)中比\(a_r\)小的元素个数,减去两倍的在区间\([l+1,r-1]\)中比\(a_l\)小的元素个 ...
- Zookeeper ----- ZAB算法
介绍 Zookeeper没有使用Paxos实现,而是使用ZAB(Zookeeper原子消息广播协议)作为数据一致性的核心算法. ZAB是一种专为Zookeeper设计的支持崩溃恢复的原子广播协议. Z ...
- Problem C: 计算机类
Description 定义一个Computer类,有两个属性: 1. 字符串属性name,用于表示计算机的名字. 2. 静态整型属性cnt,用于记录产生的计算机对象的个数. 至少有如下成员函数: 1 ...
- 前端学习(十二):CSS排版
进击のpython ***** 前端学习--CSS排版 本节主要介绍网页排版中主要格式化元素属性 帮助开发者把css技术与网页排版紧密联系到一起,来更好的实现网页设计效果 字体属性 字体 在日常工作中 ...
- R语言基本绘图-plot参数:标题,坐标轴和颜色
标题 plot(c(1:2,2:4),main = "这是主标题",sub = "这是副标题",xlab = "这是x轴", ylab = ...