问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章

  1. C#LeetCode刷题之#344-反转字符串​​​​​​​(Reverse String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3933 访问. 编写一个函数,其作用是将输入的字符串反转过来. 输 ...

  2. C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3770 访问. 给定两个字符串 s 和 t,判断它们是否是同构的. ...

  3. LeetCode 541. 反转字符串 II(Reverse String II)

    541. 反转字符串 II 541. Reverse String II

  4. C#LeetCode刷题之#557-反转字符串中的单词 III(Reverse Words in a String III)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3955 访问. 给定一个字符串,你需要反转字符串中每个单词的字符顺 ...

  5. C#LeetCode刷题之#345-反转字符串中的元音字母​​​​​​​(Reverse Vowels of a String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...

  6. C#LeetCode刷题之#859-亲密字符串​​​​​​​​​​​​​​(Buddy Strings)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3973 访问. 给定两个由小写字母构成的字符串 A 和 B ,只要 ...

  7. 说了你可能不信leetcode刷题局部链表反转D92存在bug,你看了就知道了

    一.题目描述 找出数组中重复的数字 > 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...

  8. leetcode刷题七<整数反转>

    给出一个 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 : 输入: 输出: 示例 : 输入: - 输出: - 示例 : 输入: 输出: 假设我们的环境只能存储得下 32 位的有符号整 ...

  9. C#LeetCode刷题之#443-压缩字符串​​​​​​​(String Compression)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3943 访问. 给定一组字符,使用原地算法将其压缩. 压缩后的长度 ...

随机推荐

  1. Guava的两种本地缓存策略

    Guava的两种缓存策略 缓存在很多场景下都需要使用,如果电商网站的商品类别的查询,订单查询,用户基本信息的查询等等,针对这种读多写少的业务,都可以考虑使用到缓存.在一般的缓存系统中,除了分布式缓存, ...

  2. C++语法小记---运算符重载

    运算符重载 运算符重载的本质也是对已有功能的扩展 运算符重载的本质就是函数重载,只是函数变成了 operator + 运算符 当成员函数和全局函数对运算符进行重载时,优先调用成员函数 运算符重载为成员 ...

  3. .Net Core缓存组件(MemoryCache)【缓存篇(二)】

    一.前言 .Net Core缓存源码 1.上篇.NET Core ResponseCache[缓存篇(一)]中我们提到了使用客户端缓存.和服务端缓存.本文我们介绍MemoryCache缓存组件,说到服 ...

  4. springboot+redis做事件过期通知业务

    springboot+redis做事件过期通知 博主也是初次体验,不足之处多多指教 我的业务场景 系统管理员要给维护员分配巡查路口设施的工作,由于路口比较多,管理员不知道哪些路口已经被分配了,况且过了 ...

  5. jsp课堂笔记4 javabean

    Javabean是一个可重复使用的软件组件,实际上是一种java类 实现代码重复利用 易编写易维护易使用 jsp页面的主要任务是显示页面,不负责数据的逻辑业务处理 将数据处理过程中指派一个或多个bea ...

  6. 数据库(十一):Navicat可视化工具

    进击のpython ***** 数据库--Navicat可视化工具 那命令行敲了那么久,难免影响开发效率 所以说就出现了一款可视化开发工具Navicat 下载位置:https://pan.baidu. ...

  7. PHP设计模式之----简单工厂模式

    定义个抽象的类(或接口),让子类去继承(实现)它 abstract class Operation { abstract public function getValue($num1, $num2); ...

  8. Jquery日历编写小练习

    日历练习 总体效果展示: 代码展示: 源代码部分 <body> <!-- 日历--> <div class="div_sty"> <tab ...

  9. 给我半首歌的时间,给你说明白Immutable List

    先看再点赞,给自己一点思考的时间,微信搜索[沉默王二]关注这个靠才华苟且的程序员.本文 GitHub github.com/itwanger 已收录,里面还有一线大厂整理的面试题,以及我的系列文章. ...

  10. LQB201803乘积尾零

    果然是练思维呀!!要是我的话估计就能挨个算一算呜呜呜 分解成 2和5相乘的式子 #include <iostream> using namespace std; //快速幂运算 int m ...