问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. DEP(Data Execution Prevention) 数据执行保护

    1.原理 数据执行保护,简称“DEP”,英文全称为“Data Execution Prevention”,是一组在存储器上运行额外检查的硬件和软件技术,有助于防止恶意程序码在系统上运行. 此技术由Mi ...

  2. GPO - General GPO Settings(3)

    WMI filtering Setting - Differentiating Installation Between Operations and Architecture. WMI SQL Ge ...

  3. Python Ethical Hacking - ARPSpoof_Detector

    ARPSPOOF_DETECTOR Watch value for gateway mac in the arp table Nice and simple, but will not detect ...

  4. python numpy库np.percentile用法说明

    在python中计算一个多维数组的任意百分比分位数,此处的百分位是从小到大排列,只需用np.percentile即可…… a = range(1,101) #求取a数列第90%分位的数值 np.per ...

  5. 程序员每日一乐:html动态烟花设计 3D

    3D版烟花 效果图:file:///C:/Users/QianXin/Desktop/3D%E7%83%9F%E8%8A%B1.html 经过一天的的工作或者学习是否感到枯燥乏味?现在的你是否想找些乐 ...

  6. java io流根据url读取图片

    //获取图片大小 public void readFileSize(String url,HttpServletRequest request){ //根路径 File file = new File ...

  7. 萌新学渗透系列之Hack The Box_Lame

    我将我的walkthrough过程用视频解说的形式记载 视频地址https://www.bilibili.com/video/BV1Mv411z75c 一是因为看我视频的后来者应该都是刚入门的新手,视 ...

  8. 下载数据到csv中(乱码),使用numpy , pandas读取失败 解决方案

    读取数据,下载数据到 csv 文件中 allUniv 列表类型[[...],[...]] 字符集编码使用 utf-8-sig with open('文件名.csv','w',newline='',en ...

  9. PHP restore_error_handler() 函数

    定义和用法 restore_error_handler() 函数恢复之前的错误处理程序. 该函数用于在通过 set_error_handler() 函数改变后恢复之前的错误处理程序. 该函数总是返回 ...

  10. 5.19 省选模拟赛 T1 小B的棋盘 双指针 性质

    LINK:小B的棋盘 考试的时候没有认真的思考 导致没做出来. 容易发现 当k>=n的时候存在无限解 其余都存在有限解 对于30分 容易想到暴力枚举 对称中心 然后 n^2判断. 对于前者 容易 ...