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. MySQL 找回密码

    Windows: 1.关闭正在运行的MySQL. 2.打开DOS窗口,转到mysql\bin目录. 3.输入mysqld --skip-grant-tables回车.如果没有出现提示信息,那就对了. ...

  2. CSS——div居中,window.open(0

    margin:0 auto 表示什么意思?? margin后面如果只有两个参数的话,第一个表示top和bottom,第二个表示left和right因为0 auto,表示上下边界为0,左右则根据宽度自适 ...

  3. 实现cell显示一个删除button

    假设想实现滑动cell时,cell右边就能显示一个删除button,则要实现tableview 下边方法: - (void)tableView:(UITableView *)tableView com ...

  4. datagrid鼠标悬浮提示

    /** * 扩展两个方法 */ $.extend($.fn.datagrid.methods, { /** * 开打提示功能 * @param {} jq * @param {} params 提示消 ...

  5. Applet是java的自动执行方式(这是它的优势,主要用于HTML)

    进度条:ProgressBar. JcomboBox:下拉菜单:在AWT中同类组件是choice. JlistPanel:选择列表 BorderPanel:设置边框 JsplitPanel:可将容器分 ...

  6. 【BZOJ】1622: [Usaco2008 Open]Word Power 名字的能量(dp/-模拟)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1622 这题我搜的题解是dp,我也觉得是dp,但是好像比模拟慢啊!!!! 1400ms不科学! 设f[ ...

  7. js调绝对定位的top

    $("ggg div").each(function () {                this.style.top = (parseFloat(this.style.top ...

  8. 我学cocos2d-x (一) 游戏基本概念:坐标系与Anchor Point

    坐标系: 游戏开发中.全部物体都有自己的位置,而我们须要一个參考系来描写叙述物体的位置.使用cocos2d-x开发的时候.有几个比較重要坐标系须要掌握:屏幕坐标系和Cocos2d坐标系 屏幕坐标系: ...

  9. [转]Linux动态库的种种要点

    linux下使用动态库,基本用起来还是很容易.但如果我们的程序中大量使用动态库来实现各种框架/插件,那么就会遇到一些坑,掌握这些坑才有利于程序更稳健地运行. 本篇先谈谈动态库符号方面的问题. 测试代码 ...

  10. Spring_day01--Spring的bean管理(xml方式)_属性注入介绍

    Spring的bean管理(xml方式) Bean实例化的方式 1 在spring里面通过配置文件 创建对象 2 bean实例化(创建对象)三种方式实现 第一种 使用类的无参数构造创建(重点) Use ...