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.

Example:

Input: s = "abcdefg", k = 2
Output: "bacdfeg"

Restrictions:

  1. The string consists of lower English letters only.
  2. Length of the given string and k will in the range [1, 10000]
class Solution {
public String reverseStr(String s, int k) {
int i = 0;
char[] charArr = s.toCharArray();
while (i < charArr.length) {
int j = Math.min(i + k - 1, charArr.length - 1);
swap(i, j, charArr);
i += 2 * k;
}
return new String(charArr);
} private void swap(int i, int j, char[] charArr) {
while (i < j) {
char temp = charArr[i];
charArr[i] = charArr[j];
charArr[j] = temp;
i += 1;
j -= 1;
}
}
}

[LC] 541. Reverse String II的更多相关文章

  1. [LeetCode] 344 Reverse String && 541 Reverse String II

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

  2. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  3. leadcode 541. Reverse String II

    package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ...

  4. 【leetcode_easy】541. Reverse String II

    problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ...

  5. 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 ...

  6. 541. Reverse String II 指定翻转前k个的字符串

    [抄题]: Given a string and an integer k, you need to reverse the first k characters for every 2k chara ...

  7. [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 ...

  8. 541. Reverse String II

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

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

    给定一个字符串和一个整数 k,你需要对从字符串开头算起的每个 2k 个字符的前k个字符进行反转.如果剩余少于 k 个字符,则将剩余的所有全部反转.如果有小于 2k 但大于或等于 k 个字符,则反转前 ...

随机推荐

  1. mybatis连接mysql(jdbc)常见问题

    问题1:java.io.IOException: Could not find resource com/xxx/xxxMapper.xml IDEA是不会编译src的java目录的xml文件,所以在 ...

  2. 运行roscore出现unable to contact my own server无法启动小海龟的部分故障问题解决

    运行roscore后,出现下图这种情况(unable to contact my own server) 原因是找不到http://后面那些,ping不到域名或IP. 参考http://www.ros ...

  3. 最小二乘拟合(scipy实现)

    Scipy库在numpy库基础上增加了众多数学,科学及工程计算中常用库函数.如线性代数,常微分方程数值求解,信号处理,图像处理,稀疏矩阵等. 如下理解通过Scipy进行最小二乘法拟合运算 最小二乘拟合 ...

  4. Mac系统的SVN客户端:Snail SVN 精简版

    Mac系统的SVN客户端:Snail SVN 精简版 前言 本人在公司中,使用的是windows操作系统,svn客户端自然也就使用tortoise svn.但自从男朋友给我买了台macbook pro ...

  5. Ubuntu的奇技淫巧

    sudo apt-get install cmatrix 输入密码,安装后,按F11把terminal全屏,输入cmatrix -b sudo apt-get install sl 安装后执行sl,屏 ...

  6. vscode显示当前文件完整路径信息

    Code->Preferences->Settings 搜索window.title 原本是activeEditorShort,修改 activeEditorShort => act ...

  7. 吴裕雄--天生自然 JAVA开发学习:日期时间

    import java.util.Date; public class DateDemo { public static void main(String args[]) { // 初始化 Date ...

  8. JQuery 点击子控件事件,不会触发父控件的事件

     $('.order-delete').on('tap', function (e) {                  console.log('删除1');                  c ...

  9. ZJNU 2206 - 染色

    开纵横两个结构体数组,记录连续涂了一整行或者一整列的情况 再开一个map,记录涂点 #include<iostream> #include<algorithm> #includ ...

  10. ZJNU 2201 - 挖矿谷物语

    在dfs过程中加上栈记录当次dfs走过的路径 如果当次dfs到了一个之前的dfs已经经过的点 又因为只对没有访问过的点开始dfs 所以这种情况就说明接下来不可能返回到当次dfs开始的点 将栈内元素取出 ...