題目
字符串的左旋轉操作是把字符串前面的若干個字符轉移到字符串的尾部。請定義一個函數實現字符串左旋轉操作的功能。比如,輸入字符串"abcdefg"和數字2,該函數將返回左旋轉兩位得到的結果"cdefgab"。
示例 1:
輸入: s = “abcdefg”, k = 2
輸出: “cdefgab”
示例 2:
輸入: s = “lrloseumgh”, k = 6
輸出: “umghlrlose”
限制:
1 <= k < s.length <= 10000
思路
定義一個字符串,先將s中的第n位到最后一位放入字符串,然后將第0位到第n-1位存入字符串
代碼
#include <vector>
#include <iostream>
#include <string>
using namespace std;class Solution {
public:string reverseLeftWords(string s, int n) {string res;for (int i = n; i < s.size(); i++) {res.push_back(s[i]);}for (int i = 0; i < n; i++) {res.push_back(s[i]);}return res;}
};int main()
{Solution test;std::string input &#