public class Solution { public String LeftRotateString(String str,int n) { if(n>=str.length())return str; char[] ch = str.toCharArray(); int len = ch.length-1; reverse(ch, 0, n-1); reverse(ch, n, len); reverse(ch, 0, len); return new String(ch); } public void swap(char[] ch, int i, int j) { char temp = ch[i]; ch[i] = ch[j]; ch[j] = temp; } public void reverse(char[] ch, int i, int j) { while(i<j) swap(ch, i++, j--); } }
class Solution { public: string LeftRotateString(string str, int n) { int len = str.length(); if(len == 0) return ""; n = n % len; str += str; return str.substr(n, len); } };
原文:https://www.cnblogs.com/yihangZhou/p/10496974.html