[抄题]:

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"

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么写复杂的swap:原来是用的封装

[一句话思路]:

特殊情况比较重要:每2k翻转一次,n<k时直接就翻转n了

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

/**
* 0 k 2k 3k
* |-----------|-----------|-----------|---
* +--reverse--+ +--reverse--+
*/

[一刷]:

  1. 第n位是n - 1,不是i + n - 1,怎么会出这种错呢
  2. swap中有while循环,要一直进行,最好写成l r,这都不会说明基础太差

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

不知道怎么写复杂的swap:原来是用的封装

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

swap函数

 public void swap(char[] words, int i, int j) {
while (i < j) {
char temp = words[i];
words[i] = words[j];
words[j] = temp; i++;
j--;
}
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public String reverseStr(String s, int k) {
//corner case
if (s == null) {
return null;
}
if (k == 0) {
return s;
}
//convert
char[] words = s.toCharArray();
int i = 0;
int n = s.length();
//while (i < n)
while (i < n) {
//define j
int j = Math.min(i + k - 1, n - 1);
swap(words, i, j);
//i += 2k;
i += 2 * k;
}
//return
return new String(words);
} public void swap(char[] words, int i, int j) {
while (i < j) {
char temp = words[i];
words[i] = words[j];
words[j] = temp; i++;
j--;
}
}
}

541. Reverse String II 指定翻转前k个的字符串的更多相关文章

  1. 【leetcode_easy】541. Reverse String II

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

  2. [LeetCode] 344 Reverse String && 541 Reverse String II

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

  3. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  4. leadcode 541. Reverse String II

    package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ...

  5. 【LeetCode】541. Reverse String II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

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

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

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

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

  9. 541. Reverse String II

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

随机推荐

  1. 下ue节点

    #!/bin/bash action=$1 port=$2 file="/home/operation/workspace/renderingengine/engine/services.t ...

  2. hibernate规避SQL注入实例

    项目被检测出SQL注入,注入url如:http://127.0.0.1:8080/Test/wlf/getServiceInfo.html?province=%25E6%25B5%2599%25E6% ...

  3. 系统管理员都要知道的 30 个 Linux 系统监控工具

    1. top - 进程活动监控命令 top 命令会显示 Linux 的进程.它提供了一个运行中系统的实时动态视图,即实际的进程活动.默认情况下,它显示在服务器上运行的 CPU 占用率最高的任务,并且每 ...

  4. SpringBoot运行报Address already in use: bind

    java.net.BindException: Address already in use: bind at sun.nio.ch.Net.bind0(Native Method) at sun.n ...

  5. Java运算符 逻辑运算符 短路运算符

    &      与 两个运算数都为真时结果为真,只要有一个运算数为假结果就为假,否则就为真. true & true = true   true & false = false ...

  6. 架构-架构风格:RESTful

    ylbtech-架构-架构风格:RESTful 一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层 ...

  7. SQLSERVER出错提示:此上下文中不允许使用''。此处只允许使用常量、表达式或变量。不允许使用列名。

    在执行一段SQL语句时出现了这样的一段错误提示,在网上找了不少答案,都说的不是很详细,反复修改试验,最终解决了此问题.原SQl语句为: insert into shoufei(djbh,sflb,jk ...

  8. js获取页面名称和路径参数

    // 取当前页面名称(不带后缀名)function getPageName1(){    var a = location.href;    var b = a.split("/" ...

  9. Java int Integer

    http://www.cnblogs.com/haimingwey/archive/2012/04/16/2451813.html http://developer.51cto.com/art/200 ...

  10. linux中的常用压缩与解压缩命令

    linux中常用的压缩格式有   .zip   .gz   .bz2   .tar.gz   .tar.bz2 一..zip 1.命令格式 zip 压缩文件名 源文件名         (压缩文件到当 ...