LeetCode Reverse String II
原题链接在这里:https://leetcode.com/problems/reverse-string-ii/#/description
题目:
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]
题解:
是Reverse String的进阶题目.
每2k个char中前k个char swap.
Time Complexity: O(n), n = s.length(). Space: O(n).
AC Java:
 public class Solution {
     public String reverseStr(String s, int k) {
         int len = s.length();
         char [] charArr = s.toCharArray();
         int i = 0;
         while(i < len){
             int j = Math.min(i+k-1, len-1);
             swap(charArr, i, j);
             i += 2*k;
         }
         return new String(charArr);
     }
     private void swap(char [] s, int i, int j){
         while(i<j){
             char temp = s[i];
             s[i] = s[j];
             s[j] = temp;
             i++;
             j--;
         }
     }
 }
跟上Reverse Words in a String III.
LeetCode Reverse String II的更多相关文章
- [LeetCode] Reverse String II 翻转字符串之二
		Given a string and an integer k, you need to reverse the first k characters for every 2k characters ... 
- [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) ... 
- LeetCode 541. 反转字符串 II(Reverse String II)
		541. 反转字符串 II 541. Reverse String II 
- [LeetCode] Reverse String 翻转字符串
		Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ... 
- LeetCode Reverse String
		原题链接在这里:https://leetcode.com/problems/reverse-string/ 题目: Write a function that takes a string as in ... 
- leadcode 541. Reverse String II
		package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ... 
- LeetCode——Reverse String
		LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ... 
- 【leetcode_easy】541. Reverse String II
		problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ... 
随机推荐
- git 分支合并处理
			Git 分支 - 分支的新建与合并 https://git-scm.com/book/zh/v2/%E8%B5%B7%E6%AD%A5-%E5%85%B3%E4%BA%8E%E7%89%88%E6%9 ... 
- 方法——<37>
			1,返回url参数 /* * 返回参数值 * @method getUrlPara * @papram {string},url中参数名 * @return {string},url中参数值 * */ ... 
- HTTP学习笔记05-首部
			首部和方法配合工作共同决定了客户端和服务器能做些什么事情. 首部可以出现在请求和响应报文中,大致来分的话,可以分为那么5种: 通用首部: request和response报文都可以使用的首部. 比如 ... 
- INSPIRED启示录 读书笔记 - 第10章 管理上司
			十条经验 1.为项目波动做好准备:用项目波动代指让你心烦意乱的各种返工.计划变更.不要企图消灭项目波动,但是可以尽量降低其负面影响.方法是提高警惕,记录工作进度,掌握项目波动的规律,寻找对策.制订项目 ... 
- Java 内部类、静态类内部类
			问: 什么是内部类? 答: 内部类(Inner Class)就是在一个类的内部再定义一个类,与之对应包含内部类的类被称为外部类. 问: 为什么要将一个类定义在另外一个类内部呢? 答: 内部类主要作用如 ... 
- Kubernetes List-Watch
			list-watch,作为k8s系统中统一的异步消息传递方式,对系统的性能.数据一致性起到关键性的作用. list-watch操作需要做这么几件事: 由组件向apiserver而不是etcd发起wat ... 
- 搭建配置cacti,采集信息监控
			安装cactilamp环境[iyunv@Cacti ~]#service iptables stop //关闭防火墙服务[iyunv@Cacti ~]#chkconfig iptables off / ... 
- 1.mysql导论
			虽然之前用过mysql一年多,但大多只是会用,深入了解的不多.所以想利用平时时间 系统的总结总结. 一.什么是数据库:(数据库软件) 1).什么是数据库(软件):数据库(DB:DataBase ... 
- 【P2361】yyy棋(博弈论+贪心+模拟)
			这个题看上去本来不好处理,然而善意的题面已经基本告诉你做法了,小时候玩的那个游戏就是代码的核心.动动脑子想想,如果长和宽的积是奇数,那么一定要先手,如果是偶数,那么后手就会获胜. 好了,那么怎么处理对 ... 
- java基础10(IO流)-字节流
			IO流 输入与输出[参照物是程序] 如果从键盘.文件.网络甚至是另一个进程(程序或系统)将数据读入到程序或系统中,称为输入 如果是将程序或系统中的数据写到屏幕.硬件上的文件.网络上的另一端或者是一个进 ... 
