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. Python时间问题

    获取当前的时间,time只能精确到秒,而datetime可以精确到毫秒,所以使用格式化的时候要注意. nowTime=time.localtime((time.time())) t=time.strf ...

  2. JAVAEE 和项目开发(第一课:浏览器和服务器的交互模式和HTTP协议的概念和介绍)

    互联网的发展非常迅速,但是万变不离其宗.学习 web 开发,需要我们对互 联的交互机制有一定的了解.为了更好的理解并掌握 Servlet,在正式学习 Servlet之前需要对 web 开发中客户端和服 ...

  3. NSDictionary和NSMaptable, NSArray,NSSet,NSOrderedSet和NSHashTable的区别

    NSSet, NSDictionary, NSArray是Foundation框架关于集合操作的常用类, 和其他标准的集合操作库不同, 他们的实现方法对开发者进行隐藏, 只允许开发者写一些简单的代码, ...

  4. findbugs报OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE的修改实例

    先看出问题的一段代码 public void encode(String xxxPath, String thumbTmpPath, String imageType) { LOGGER.info(& ...

  5. 树上问题&图论模板整理

    去除过水的模板,包括但不限于dijkstra(甚至堆优化都被过滤了).SPFA.kruskal.拓扑排序等. 欧拉回路:http://uoj.ac/problem/117 #include<bi ...

  6. java内部类(构造spring中的接收返回数据的实体类)

    说起内部类这个词,想必很多人都不陌生,但是又会觉得不熟悉.原因是平时编写代码时可能用到的场景不多,用得最多的是在有事件监听的情况下,并且即使用到也很少去总结内部类的用法.今天我们就来一探究竟. 原文链 ...

  7. python模块——datetime

    datetime模块是python自带对时间的操作,其常用的四大类分别是date.time.datetime.timedelta.下面分别讲解下这四大类中常用的方法及其属性. date类 date类的 ...

  8. Python 学习笔记:Python 连接 SQL Server 报错(20009, b'DB-Lib error message 20009, severity 9)

    问题及场景: 最近需要使用 Python 将数据写到 SQL Server 数据库,但是在进行数据库连接操作时却报以下错误:(20009, b'DB-Lib error message 20009, ...

  9. jest 测试入门(一)

    说实话,作为前端来说,单元测试,并不是一种必须的技能,但是确实一种可以让你加法的技能 之前我一个库添加了单元测试,加完之后感悟颇深,所以写下这篇文章来记录 环境搭建 一般来说,普通的库,如果没有添加 ...

  10. 吴裕雄--天生自然 JAVA开发学习:多态

    Parent p = new Child(); public class Test { public static void main(String[] args) { show(new Cat()) ...