c# leetcode 面試題58 - II. 左旋轉字符串 (字符串)

字符串的左旋轉操作是把字符串前面的若干個字符轉移到字符串的尾部。請定義一個函數實現字符串左旋轉操作的功能。比如,輸入字符串"abcdefg"和數字2,該函數將返回左旋轉兩位得到的結果"cdefgab"。

示例 1:

輸入: s = "abcdefg", k = 2
輸出: "cdefgab"

示例 2:

輸入: s = "lrloseumgh", k = 6
輸出: "umghlrlose"

限制:

  • 1 <= k < s.length <= 10000

我的答案:

字符串截取+拼接,沒什麼技術含量

public class Solution {
    public string ReverseLeftWords(string s, int n) {
         
            if (s.Length <= n)
                return s;
            string s1= s.Substring(0, n);
            string res = "";
            for (int i = n; i < s.Length; i++)
            {
               res += s[i];
            }
            return res+s1;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章