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. JavaScript 2019.3.15

    方法名.call(对象)可以切换方法调用的对象 参数数量 基本数据类型 typeof无法更细致的区分引用类型(全是object) =

  2. 4)栈和队列-->受限线性表

    栈和队列叫  受限线性表  只不过他们插入和删除的位置  相对于之前的线性表有了限制   所以叫受限线性表 1)栈-->就是先进后出 2)队列-->先进先出 3)循环链表框图: 4)队列

  3. java8+tomcate8仅支持TLSv1.2

    1.编辑$tomcat_home/conf/server.xml <Connector protocol="org.apache.coyote.http11.Http11NioProt ...

  4. 简单模拟B1011

    #include<iostream> using namespace std; int main() { int n; long long a,b,c; cin >> n; ; ...

  5. 深入分析Java反射(五)-类实例化和类加载

    前提 其实在前面写过的<深入分析Java反射(一)-核心类库和方法>已经介绍过通过类名或者java.lang.Class实例去实例化一个对象,在<浅析Java中的资源加载>中也 ...

  6. CSS 之pseudo-classes 与pseudo-element的异同

    从W3School找到相关资料如下: 伪类: 伪类存在的意义是为了通过选择器找到那些不存在与DOM树中的信息以及不能被常规CSS选择器获取到的信息. 伪类由一个冒号:开头,冒号后面是伪类的名称和包含在 ...

  7. ZOJ 3735 dp

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3735 好久没做DP题了,一开始没理解题目里的C(M,3)是干什么,原来就是 ...

  8. SSM到Spring Boot-校园商铺平台:第01章 开发准备

    第01章 开发准备 环境准备 创建一个Maven项目作为开始 添加一个 Server Runtime 添加maven的java编译插件 <build> <finalName>$ ...

  9. PAT A1133 Splitting A Linked List (25) [链表]

    题目 Given a singly linked list, you are supposed to rearrange its elements so that all the negative v ...

  10. PAT Basic 1043 输出PATest (20分)[Hash散列]

    题目 给定⼀个⻓度不超过10000的.仅由英⽂字⺟构成的字符串.请将字符重新调整顺序,按"PATestPATest-."这样的顺序输出,并忽略其它字符.当然,六种字符的个数不⼀定是 ...