leadcode 541. Reverse String II
package leadcode; /**
* 541. Reverse String II
* Easy
* 199
* 575
*
*
* 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]
*/
public class L541 {
public String reverseStr(String s, int k) {
StringBuilder sb = new StringBuilder();
int len = s.length();
int count = len/(2*k)-1;
int remaining = len%(2*k);
if (remaining!=0){
count = count+1;
}
for (int i=0;i<=count;i++){
if(remaining!=0 && i==count){
int cout2k = 2*k*(i+1);
int coutk = 2*k*i+k;
if(len<coutk){
StringBuilder sb3 = reverse(s,2*k*i,len);
sb.append(sb3);
}else if(len<cout2k){
StringBuilder sb3 = reverse(s,2*k*i,coutk);
sb.append(sb3);
StringBuilder sb4 = retain(s,coutk,len);
sb.append(sb4);
}
}else {
StringBuilder sb1 = reverse(s,2*k*i,2*k*i+k);
sb.append(sb1);
StringBuilder sb2 = retain(s,2*k*i+k,2*k*(i+1));
sb.append(sb2);
}
}
return new String(sb);
} private StringBuilder reverse(String s,int begin,int end){
return new StringBuilder(s.substring(begin,end)).reverse();
} private StringBuilder retain(String s,int begin,int end){
return new StringBuilder(s.substring(begin,end));
} }
class Solution {
public String reverseStr(String s, int k) {
char[] str=s.toCharArray();
int i;
int len=str.length;
for(i=0;i<len;i+=2*k){
if(i+k-1 >= len || i+(2*k)-1 >= len)
break;
reverse(str,i,i+k-1);
}
// if < 2k
if(i+k-1 < len)
reverse(str,i,i+k-1);
// if < k
else if(i< len && i+k-1 >= len)
reverse(str,i,len-1);
return new String(str);
}
public void reverse(char[] str, int i, int j){
while(i<j){
char temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
}
}
class Solution {
public String reverseStr(String s, int k) {
char[] a = s.toCharArray();
for (int start = 0; start < a.length; start += 2 * k) {
int i = start, j = Math.min(start + k - 1, a.length - 1);
while (i < j) {
char tmp = a[i];
a[i++] = a[j];
a[j--] = tmp;
}
}
return new String(a);
}
}
leadcode 541. Reverse String II的更多相关文章
- [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_easy】541. Reverse String II
problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ...
- 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 ...
- [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 ...
- 541. Reverse String II
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 541. Reverse String II 指定翻转前k个的字符串
[抄题]: Given a string and an integer k, you need to reverse the first k characters for every 2k chara ...
- 541 Reverse String II 反转字符串 II
给定一个字符串和一个整数 k,你需要对从字符串开头算起的每个 2k 个字符的前k个字符进行反转.如果剩余少于 k 个字符,则将剩余的所有全部反转.如果有小于 2k 但大于或等于 k 个字符,则反转前 ...
- [LC] 541. Reverse String II
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
随机推荐
- lua字符串对齐函数
最近要用到字符串对齐,开始只是一部分字符串,就直接加空格了,后来发现有很多, 于是写了个字符串对齐的函数. --功能:分割字符串 --参数:带分割字符串,分隔符 --返回:字符串表 function ...
- C# Dictionary 复制
Dictionary<string, int> dictionary = new Dictionary<string, int>(); dictionary.Add(" ...
- Maven基础命令
Maven 参数 -D 传入属性参数 -P 使用pom中指定的配置 -e 显示maven运行出错的信息 -o 离线执行命令,即不去远程仓库更新包 -X 显示maven允许的debug信息 -U 强制去 ...
- 翻译:Laravel-4-Generators 使用自己定义代码生成工具高速进行Laravel开发
使用自己定义代码生成工具高速进行Laravel开发 这个Laravle包提供了一种代码生成器,使得你能够加速你的开发进程.这些生成器包含: generate:model – 模型生成器 generat ...
- 树莓派teamviewer远程 windows远程桌面
https://mirror.tuna.tsinghua.edu.cn/help/raspbian/ 用这个源后,再安装 apt-get update https://download.teamvie ...
- [android] AndroidManifest.xml - 【 manifest -> 其他次要配置】
<uses-sdk> 作用:使应用程序的兼容性更好,指明应用程序需要的最小API,编译API以及最大支持的API.值都是整数 <uses-sdk android:minSdkVers ...
- 第二百七十二节,Tornado框架-iframe标签框架伪造ajax
Tornado框架-iframe标签框架伪造ajax html <!DOCTYPE html> <html> <head lang="en"> ...
- Thymeleaf 3.0.9.RELEASE is the current stable version. It requires Java SE 6 or newer.
Thymeleaf 3.0.9.RELEASE is the current stable version. It requires Java SE 6 or newer. Release date: ...
- 文件 I/O 问题
文件 I/O 问题:(1)对不存在的或者错误的文件进行操作吗?(2)文件以不正确的方式打开吗?(3)文件结束判断不正确吗?(4)没有正确地关闭文件吗? #include <iostream> ...
- 【BZOJ】1058: [ZJOI2007]报表统计(splay+set)
http://www.lydsy.com/JudgeOnline/problem.php?id=1058 当复习一下splay.... 做法很简单..... 观察得知每一次插入一个点只需要维护前后的绝 ...