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]
import java.util.LinkedList; /** * @author zhangyu * @version V1.0 * @ClassName: ReverseString2Test * @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. * @date 2018/10/14 15:34 **/ public class ReverseString2Test { public static void main(String[] args) { String s = "abcdefg"; int key = 2; String newString = getReverseString2(s, 2); System.out.println(newString); } private static String getReverseString2(String s, int k) { //当s字符串的长度小于k的值 if (s.length() <= k) { return new StringBuffer(s).reverse().toString(); } //当s字符串的长度介于 k和2k之间 if (s.length() > k && s.length() <= 2 * k) { String subString = s.substring(0, k); subString = new StringBuffer(subString).reverse().toString(); return subString + s.substring(k, s.length()); } //当k的字符串的长度大于2k if (s.length() > 2 * k) { LinkedList<String> list = new LinkedList<>(); StringBuffer sb = new StringBuffer(); int number = s.length() / k; for (int i = 0; i < number; i++) { list.add(s.substring(i * k, i * k + k)); } if (number * k < s.length()) { list.add(s.substring(number * k)); } for (int index = 0; index < list.size(); index++) { if (index % 2 == 0) { String temp = new StringBuffer(list.get(index)).reverse().toString(); sb.append(temp); } else { sb.append(list.get(index)); } } return sb.toString(); } return null; } }
上面这种计算效率高,但是为了简洁,把这个代码编程下面这种:
class Solution { public String reverseStr(String s, int k) { LinkedList<String> list = new LinkedList<>(); StringBuffer sb = new StringBuffer(); int number = s.length() / k; for (int i = 0; i < number; i++) { list.add(s.substring(i * k, i * k + k)); } if (number * k < s.length()) { list.add(s.substring(number * k)); } for (int index = 0; index < list.size(); index++) { if (index % 2 == 0) { String temp = new StringBuffer(list.get(index)).reverse().toString(); sb.append(temp); } else { sb.append(list.get(index)); } } return sb.toString(); } }
这种的效率没有上面那种效率高,但是方法更加的简洁;
-
还有一种方法另外正则表达式去切分字符串,切分字符串的方法如下所示:
private static ArrayList<String> getStringList(String str, int n) { ArrayList<String> list = new ArrayList<String>(); // 正则表达式规则 String regEx = "\\w{" + n + "}"; // 编译正则表达式 Pattern pattern = Pattern.compile(regEx); // 忽略大小写的写法 Matcher matcher = pattern.matcher(str); // 查找字符串中是否有匹配正则表达式的字符/字符串 while (matcher.find()) { list.add(matcher.group()); } // 截取字符串后面一位 if (n * (list.size()) < str.length()) { list.add(str.substring(n * list.size())); } return list; }
这种切割字符串方式利用正则表达式,效率上来说肯定比上面两种速度慢。