Leetcode-541. Reverse String II
来源:互联网 发布:java开发安卓app 编辑:程序博客网 时间:2024/04/30 10:45
前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。这次比赛略无语,没想到前3题都可以用暴力解。
博客链接:mcf171的博客
——————————————————————————————
Example:
Input: s = "abcdefg", k = 2Output: "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 Solution { public String reverseStr(String s, int k) { StringBuilder sb = new StringBuilder(""); int start = 0; while(start <= s.length()){ int i = 0,j = 0; if(s.length() - start <= k){ i = s.length() - 1; j = s.length(); }else{ i = start + k - 1; j = start + k; } for( ;i >= start; i--) sb.append(s.charAt(i)); for(; j < s.length() && j < start + 2*k; j ++)sb.append(s.charAt(j)); start += 2*k; } return sb.toString(); }}
1 0
- leetcode 541. Reverse String II
- Leetcode-541. Reverse String II
- 【Leetcode】541. Reverse String II
- 541. Reverse String II LeetCode
- [LeetCode]541. Reverse String II
- LeetCode 541. Reverse String II
- LeetCode 541. Reverse String II
- leetcode-541. Reverse String II
- [leetcode]541. Reverse String II
- [leetcode]: 541. Reverse String II
- LeetCode:541. Reverse String II
- LeetCode 541. Reverse String II
- leetcode 541. Reverse String II
- leetcode#541. Reverse String II
- leetcode 541. Reverse String II
- LeetCode-541. Reverse String II
- [LeetCode] 541. Reverse String II
- LeetCode 541. Reverse String II
- Android的Activity的四种启动模式
- Linux操作系统哪个版本最好用?
- Fragment详解之一——概述
- CCF201409(1)相邻数对
- EditText详解与自定义
- Leetcode-541. Reverse String II
- 在Linux服务器上部署个人网站(一)
- 1008 选数 (深搜 判断素数)
- 数据结构实验之链表一:顺序建立链表 (sdut oj)
- 【Codeforces 780 D Innokenty and a Football League 】+ 模拟 + 贪心
- ScrollView内容适配及RelativeLayout 水平居中等分
- IDEA连接数据库
- centos7虚拟机中安装nginx+php+mysql
- 平衡树splay