Lc58_面试题58 - II. 左旋转字符串
package com.example.demo;
/**
* 面试题58 - II. 左旋转字符串
* 字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
* <p>
* <p>
* <p>
* 示例 1:
* <p>
* 输入: s = "abcdefg", k = 2
* 输出: "cdefgab"
* 示例 2:
* <p>
* 输入: s = "lrloseumgh", k = 6
* 输出: "umghlrlose"
* <p>
* <p>
* 限制:
* <p>
* 1 <= k < s.length <= 10000
*/
public class Lc58_interview {
public static String reverseLeftWords(String s, int n) {
char[] c = s.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0 ; i < n; i++){
sb.append(c[i]);
}
StringBuffer sb1 = new StringBuffer();
for (int i = n ; i < s.length(); i++){
sb1.append(c[i]);
}
return sb1.toString()+sb.toString();
}
public static void main(String[] args) {
String s = "abcdefg";
int n = 2;
System.out.println(reverseLeftWords(s,n));
}
}
不恋尘世浮华,不写红尘纷扰