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. Cannot proceed with delivery: an existing transporter instance is currently uploading this package

    当使用Xcode的Application Loader上传spa到AppStore的过程中,如果临时中断,当你再次进行上传的过程时,就发发现如下现象: Cannot proceed with deli ...

  2. [shell]Linux脚本开头#!/bin/bash和#!/bin/sh是什么意思以及区别

    一直以为在shell脚本中#都是代表着注释功能,同样在脚本开始的#!/bin/sh也只是告诉用户这是一个shell脚本,而最近顺手查了下,才发现不是这个意思,分享下面的文章. 转自:http://ww ...

  3. UCOS2系统内核讲述_总体描述

    Ⅰ.写在前面 学习本文之前可以参考我前面基于STM32硬件平台移植UCOS2的几篇文章,我将其汇总在一起: UCOS2_STM32F1移植详细过程(汇总文章) 要想学习,或使用系统配套的资源(如:信号 ...

  4. CSS(五):背景、列表、超链接伪类、鼠标形状控制属性

    一.背景属性 1.背景属性用来设置页面元素的背景样式. 2.常见背景属性 属性 描述 background-color 用来设置页面的背景色,取值如red,#ff0000 background-ima ...

  5. Spark Streaming:大规模流式数据处理的新贵

    转自:http://www.csdn.net/article/2014-01-28/2818282-Spark-Streaming-big-data 提到Spark Streaming,我们不得不说一 ...

  6. 【剑指offer】翻转单词顺序

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/27372033 题目描写叙述: JOBDU近期来了一个新员工Fish,每天早晨总是会拿着一本 ...

  7. MyEclipse------如何添加jspsmartupload.jar,用于文件上传

    方法: 右键“Web”工程->properties->Libraries->Add External JARs...->找到“jspsmartupload.jar”,添加进去 ...

  8. UE4关于编译配置的参考(Debug,DebugGame,Development,Shipping,Test等)

    https://docs.unrealengine.com/latest/CHN/Programming/Development/BuildConfigurations/index.html 编译配置 ...

  9. 如何通过Keil将程序正确的下载进flash中

    前面介绍了一些创建工程和调试的基本步骤,在这里准备介绍一下如何正确的将Keil程序在仿真调试中下载到flash.这里再次涉及到了debug的窗口.   工具/原料   Keil uVision 4/5 ...

  10. .net Session延长过期时间

    一.全局网站(即服务器)级 IIS-网站-属性-Asp.net-编辑配置-状态管理-会话超时(分钟)-设置为120,即为2小时,即120分钟后如果当前用户没有操作,那么Session就会自动过期. 二 ...