LeetCode Reverse String II
原题链接在这里:https://leetcode.com/problems/reverse-string-ii/#/description
题目:
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:
- The string consists of lower English letters only.
- Length of the given string and k will in the range [1, 10000]
题解:
是Reverse String的进阶题目.
每2k个char中前k个char swap.
Time Complexity: O(n), n = s.length(). Space: O(n).
AC Java:
public class Solution {
public String reverseStr(String s, int k) {
int len = s.length();
char [] charArr = s.toCharArray();
int i = 0;
while(i < len){
int j = Math.min(i+k-1, len-1);
swap(charArr, i, j);
i += 2*k;
}
return new String(charArr);
}
private void swap(char [] s, int i, int j){
while(i<j){
char temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
}
跟上Reverse Words in a String III.
LeetCode Reverse String II的更多相关文章
- [LeetCode] Reverse String II 翻转字符串之二
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- LeetCode 541. 反转字符串 II(Reverse String II)
541. 反转字符串 II 541. Reverse String II
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- LeetCode Reverse String
原题链接在这里:https://leetcode.com/problems/reverse-string/ 题目: Write a function that takes a string as in ...
- leadcode 541. Reverse String II
package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ...
- LeetCode——Reverse String
LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...
- 【leetcode_easy】541. Reverse String II
problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ...
随机推荐
- css position absolute 浮动特性
absolute的元素不会占据未浮动的元素的空间<html> <head> <style type="text/css"> .flipbox{ ...
- java的服务端与客户端通信(2)
一.Socket连接与HTTP连接 1.1Socket套接字 套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元.它是网络通信过程中端点的抽象表示,包含进行网络通信 ...
- uCOS-II的学习笔记(共九期)和例子(共六个)
源:uCOS-II的学习笔记(共九期)和例子(共六个) 第一篇 :学习UCOS前的准备工作http://blog.sina.com.cn/s/blog_98ee3a930100w0eu.html 第二 ...
- STM32F4XX高效驱动篇2 I2C
说到I2C很多用过STMF10X硬件I2C方式的工程师,都感觉有点头痛.大部分还是使用软件模拟的方式,I2C由于一般的工作频率是400,100KHz.所以在平凡读取,或所读数据量大时,使用这模拟的方式 ...
- 登陆weblogic后页面控制台卡主
输入http://localhost:7001/console进入控制页面,能登陆进去,但是登陆进去后页面就马上卡死,可以看到页面头部,其余都显示不出来. 重启后启动访问,能够正常进入,关闭weblo ...
- Xamarin简介与Xamarin支持MVC设计模式
Create Native iOS, Android,Mac and Windows apps in C#. 官方网站:http://xamarin.com/ 使用武器 Run a C# app, g ...
- transition失效问题
关于transition,css教程中有一个很简单的例子: <!DOCTYPE html> <html> <head> <meta charset=" ...
- 20145239《网络对抗》- 逆向及Bof基础实践
1 逆向及Bof基础实践说明 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件.该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串.该程序同 ...
- 20145230《java程序设计》第6周学习总结
20145230 <Java程序设计>第6周学习总结 教材学习内容 串流设计的概念 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象.如果要将数据从来源取出,可以 ...
- QFile操作文件
1.构造QFile对象 QFile file("C:\a.txt"); 或者 QFile *file = new QFile("C:\a.txt"); 2.设置 ...