541. Reverse String II 指定翻转前k个的字符串
[抄题]:
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--+
*/
[一刷]:
- 第n位是n - 1,不是i + n - 1,怎么会出这种错呢
- 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个的字符串的更多相关文章
- 【leetcode_easy】541. Reverse String II
problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- leadcode 541. Reverse String II
package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ...
- 【LeetCode】541. Reverse String II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 541 Reverse String II 反转字符串 II
给定一个字符串和一个整数 k,你需要对从字符串开头算起的每个 2k 个字符的前k个字符进行反转.如果剩余少于 k 个字符,则将剩余的所有全部反转.如果有小于 2k 但大于或等于 k 个字符,则反转前 ...
- 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 ...
- [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 ...
- 541. Reverse String II
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
随机推荐
- [LeetCode系列] 二叉树最大深度求解问题(C++递归解法)
问: 给定二叉树, 如何计算二叉树最大深度? 算法描述如下: 如果当前节点为空, 返回0(代表此节点下方最大节点数为0) 如果当前节点不为空, 返回(其左子树和右子树下方最大节点数中的最大值+1) 上 ...
- spring整合xfire出现Document root element "beans", must match DOCTYPE root "null"错误解决方案
fire自带的包下有个spring1.2.6的jar包,而工程的jar包是2.0的. 解决方案: 1.将原配置文件的头schema方式换为DOCTYPE方式,原配置文件如下(非maven) <? ...
- J2EE项目在weblogic下的改动
1.struts所有配置文件放到classes根目录下 2〉java.lang.ClassCastException:weblogic.xml.jaxp.RegistryDocumentBuilder ...
- Docker的一些常用
日常使用的一些命令 1234567891011121314 docker pull mysql:tags // 拉mysql的tag版本 docker run -it -p(端口映射-主机端口:容器端 ...
- Xbox360游戏收藏
xbox360游戏下载地址 http://dl.3dmgame.com/SoftList_221.html XBLA游戏总结.http://tieba.baidu.com/p/3174478602 ...
- vc中edit控件使用总结
通过类向导可以生成两种类成员变量,一种是cstring类型,一种是cedit类型.在程序中使用时如果只是简单的获取 edit控件内容,或设置简单的内容建议使用cstring类型成员变量.示例:CStr ...
- windows10 vs2015编译 带nginx-rtmp-module 模块的32位nginx
1 下载必要软件 从 http://xhmikosr.1f0.de/tools/msys/下载msys:http://xhmikosr.1f0.de/tools/msys/MSYS_MinGW-w6 ...
- Java-Runoob-高级教程:Java 泛型
ylbtech-Java-Runoob-高级教程:Java 泛型 1.返回顶部 1. Java 泛型 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检 ...
- windows python文件拷贝到linux上执行问题-换行符问题/r/n
之前在Windows下写好了一个Python脚本,运行没问题,今天在Linux下,脚本开头的注释行已经指明了解释器的路径,也用chmod给了执行权限,但就是不能直接运行脚本. 1 问题1: 报错:: ...
- CentOS7 系统菜单中添加快捷方式
一,在桌面新建一个文件 文件名随意,但必须带有.desktop的后缀名, 以Eclipse为例 vi /usr/share/applications/eclipse.desktop 二,在文件中写入如 ...