字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。 示例 1: 输入: s = "abcdefg", k = 2 输出: "cdefgab" 示例 2: 输入: s = "lrloseumgh", k = 6 输出: "umghlrlose" 限制: 1 <= k < s.length <= 10000
subString()
方法将字符串阶段后拼接;package inteview; /** * Created with IntelliJ IDEA. * Version : 1.0 * Author : cunyu * Email : cunyu1024@foxmail.com * Website : https://cunyu1943.github.io * Date : 2020/3/29 21:44 * Project : LeetCode * Package : inteview * Class : FiveEight * Desc : 面试题58 - II. 左旋转字符串 */ public class FiveEight { public static void main(String[] args) throws Exception { FiveEight fiveEight = new FiveEight(); String s = "abcdefg"; int n = 2; System.out.println(fiveEight.reverseLeftWords(s, n)); } public String reverseLeftWords(String s, int n) { int size = s.length(); return s.substring(n, size) + s.substring(0, n); } }
[1]
面试题58 - II. 左旋转字符串: https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/
本文分享自微信公众号 - 村雨遥(cunyu1943)
原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。
原始发表时间:2020-04-09
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句