左旋转字符串
题目描述:
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
示例 1:
输入: s = "abcdefg", k = 2
输出: "cdefgab"
示例 2:
输入: s = "lrloseumgh", k = 6
输出: "umghlrlose"
限制:
1 <= k < s.length <= 10000
解题思路:
在介绍这题的解题思路之前,首先先向大家复习一下java的两种方法:
Java substring() 方法
substring() 方法返回字符串的子字符串。
语法
public String substring(int beginIndex)
或
public String substring(int beginIndex, int endIndex)
参数
- beginIndex – 起始索引(包括), 索引从 0 开始。
- endIndex – 结束索引(不包括)。
实例
public class Test {
public static void main(String args[]) {
String Str = new String("www.runoob.com");
System.out.print("返回值 :" );
System.out.println(Str.substring(4) );
System.out.print("返回值 :" );
System.out.println(Str.substring(4, 10) );
}
}
以上程序执行结果为:
返回值 :runoob.com
返回值 :r