1 暴力遍历
取模,再大都不怕
public String LeftRotateString(String str,int n) { if(str==null || str.length() ==0){ return null; } char [] chars=str.toCharArray(); int len=chars.length char [] resChars=new char[len]; for(int i=0;i<len;i++){ int j=i-k%len if(j<0){ j=len-j; } resChars[j]=chars[i]; } StringBuilder sb=new StringBuilder(); for(char c : resChars){ sb.append(c); } return sb.toString(); }
2 根据特带你的优化
public String LeftRotateString(String str,int n) { if(str==null || str.length() ==0){ return ""; } int len=str.length(); int tmp=n%len; String str1=str.substring(0,tmp); String str2=str.substring(tmp,len); return str2+str1; }