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的更多相关文章

  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. 【leetcode_easy】541. Reverse String II

    problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ...

  4. 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 ...

  5. [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 ...

  6. 541. Reverse String II

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  7. 541. Reverse String II 指定翻转前k个的字符串

    [抄题]: Given a string and an integer k, you need to reverse the first k characters for every 2k chara ...

  8. 541 Reverse String II 反转字符串 II

    给定一个字符串和一个整数 k,你需要对从字符串开头算起的每个 2k 个字符的前k个字符进行反转.如果剩余少于 k 个字符,则将剩余的所有全部反转.如果有小于 2k 但大于或等于 k 个字符,则反转前 ...

  9. [LC] 541. Reverse String II

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

随机推荐

  1. python跳一跳辅助学习

    微信跳一跳辅助工具 准备工具 adb驱动 安卓手机 打开手机的调试模式 usb接好手机和电脑 PyCharm:全宇宙唯一一款专门用于Python开发IDE工具 实现原理: 获取手机的实时的截图 点击起 ...

  2. hdu1331(记忆化搜索)

    #include<iostream> #include<stdio.h> #include<string.h> using namespace std; typed ...

  3. Linux下HTTP Server

    想在Linux下实现一个简单的web Server并不难.一个最简单的HTTP Server不过是一个高级的文件服务器,不断地接收客户端(浏览器)发送的HTTP请求,解析请求,处理请求,然后像客户端回 ...

  4. PHP——分页显示数据库内容

    test.php <?php header("Content-Type:text/html;charset=utf-8"); //加载分页类 include "pa ...

  5. 已知问题汇总 (2013-11-30) - QQ空间, EXTJS

    目前发现两个已知问题暂时无法得到解决: 1. QQ空间问题. 打开页面 http://user.qzone.qq.com/822994792/311, 点击 "xxx人赞" 这个链 ...

  6. grid-tooltip扩展方法

    调用:$('#dg').datagrid('doCellTip', { 'max-width': '100px' }); /** * 扩展两个方法 */$.extend($.fn.datagrid.m ...

  7. java----内部类与匿名内部类的各种注意事项与知识点

    Java 内部类分四种:成员内部类.局部内部类.静态内部类和匿名内部类.1.成员内部类: 即作为外部类的一个成员存在,与外部类的属性.方法并列.注意:成员内部类中不能定义静态变量,但可以访问外部类的所 ...

  8. 《敏捷软件开发-原则、方法与实践》-Robert C. Martin读书笔记(转)

    Review of Agile Software Development: Principles, Patterns, and Practices 本书主要包含4部分内容,这些内容对于今天的软件工程师 ...

  9. MFC通过button控制编辑框是否显示系统时间(动态显示)

    1.在dlg.h中public bool flag; static UINT time(void *param); 2.在构造函数中 flag=false; 3.在button的生成函数中 if(fl ...

  10. 去掉if

    修改前 namespace CleanCSharp.Methods.Dirty {     class BooleanSwitchingArgumentsExample     {         p ...