列表遍历拼接:
c++
class Solution {
public:
string reverseLeftWords(string s, int n) {
string res;
int l = s.size();
for(int i=n; i<l; i++){
res += s[i];
}
for(int i=0; i<n; i++){
res += s[i];
}
return res;
}
};
python
class Solution:
def reverseLeftWords(self, s: str, n: int) -> str:
res = ""
l = len(s)
for i in range(n,l):
res += s[i]
for i in range(0,n):
res += s[i]
return res
python 切片
class Solution:
def reverseLeftWords(self, s: str, n: int) -> str:
return s[n:] + s[:n]