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. Spring中的控制反转和依赖注入

    Spring中的控制反转和依赖注入 原文链接:https://www.cnblogs.com/xxzhuang/p/5948902.html 我们回顾一下计算机的发展史,从最初第一台计算机的占地面积达 ...

  2. UML-架构分析-步骤

    1.识别->因素表 2.解决->技术备忘录 1).可靠性 2).法律问题 3).可适应性

  3. CodeForces-1100C NN and the Optical Illusion 简单数学

    题目链接:https://vjudge.net/problem/CodeForces-1100C 题意: 题目给出外部圆的数目n和内部圆的半径r,要求求出外部圆的半径以满足图片要求. 显然这是一道数学 ...

  4. spring hystrix和内置tomcat组件的参数调优解析

    1. springboot内置tomcat容器的参数配置 server: port: 12021 # server端的socket超时间(毫秒),使用值-1表示没有(即无限)超时,默认值为60000( ...

  5. PAT Basic 1075 链表元素分类(25) [链表]

    题目 给定⼀个单链表,请编写程序将链表元素进⾏分类排列,使得所有负值元素都排在⾮负值元素的前⾯,⽽[0, K]区间内的元素都排在⼤于K的元素前⾯.但每⼀类内部元素的顺序是不能改变的.例如:给定链表为 ...

  6. pandas读取和写入excel多个sheet表单

    一.读取单个表单 import pandas as pd excel_reader=pd.ExcelFile('文件.xlsx') # 指定文件 sheet_names = excel_reader. ...

  7. Cover letter

    Cover letter意义和新意可以写的夸张一点没关系,写因存在竞争关系的不建议作为审稿人.不要推荐熟悉人(导师,导师的导师,有关系的人,co-authors)作为推荐审稿人,不要推荐非该领域内大牛 ...

  8. 洛谷 P5017 摆渡车

    题目传送门 解题思路: 个人感觉DP这东西,只可意会,不可言传 AC代码: #include<iostream> #include<cstdio> #include<cs ...

  9. Ubuntu目錄

    /         (这就是著名的根)├── bin         (你在终端运行的大多数程序,比如cp.mv...)├── boot         (内核放在这里,这个目录也经常被作为某个独立分 ...

  10. Please select an empty folder to install Android Studio

    原因 当前安装的Android Studio的文件夹不是空的 解决 把路径改成一个空文件夹即可