[抄题]:

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. QT creator 调试问题

    问题:debug出现“ptrace:不允许的操作.” 解决办法: # may not be appropriate for developers or servers with only admin ...

  2. FOR XML PATH 灵活运用

    FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...

  3. Eclipse git插件使用

    1.Eclipse git插件使用 1)配置提交用户名和邮箱 2)在eclipse中选择Show View 搜索git 3)点击clone按钮 选择代码保存路径 4)导入项目 5)git插件功能介绍 ...

  4. oracle版本及字符集查询

    查询oracle版本:select * from v$version; 查询oracle字符集:select * from nls_database_parameters;

  5. 1、Java通过JDBC操作Hive

    0.概述 使用的都是CLI或者hive –e的方式仅允许使用HiveQL执行查询.更新等操作.然而Hive也提供客户端的实现,通过HiveServer或者HiveServer2,客户端可以在不启动CL ...

  6. proxool 连接池

    今天配置proxool 连接池,发现可配置属性非常多,以前也只是用,没总结过,今天查了下网上的资料,总结一下 方便你我.其实网上很多英文资料都很全,网上很多人就是考翻译老外的文章赚些流量,其实也没啥意 ...

  7. Jquery 监听浏览器前进后退

    jQuery(document).ready(function () { if (window.history && window.history.pushState) { $(win ...

  8. 20181122_C#中AOP初探_装饰器模式的AOP_Remoting实现AOP_Castle实现AOP

    一.   什么是AOP: a)         AOP是面向切面编程; 就像oop一样, 它也是一种编程思想; i.    Oop思想→一切皆对象, 对象交互组成功能, 功能叠加组成模块, 模块叠加组 ...

  9. 【洛谷】P1064 金明的预算方案(dp)

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今 ...

  10. Druid.io系列(八):部署

    介绍 前面几个章节对Druid的整体架构做了简单的说明,本文主要描述如何部署Druid的环境 Imply提供了一套完整的部署方式,包括依赖库,Druid,图形化的数据展示页面,SQL查询组件等.本文将 ...