汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列 S ,请你把其循环左移 K 位后的序列输出。例如,字符序列 S = ”abcXYZdef” , 要求输出循环左移 3 位后的结果,即 “XYZdefabc”
数据范围:输入的字符串长度满足 ,
进阶:空间复杂度 ,时间复杂度
"abcXYZdef",3
"XYZdefabc"
"aab",10
"aba"
public class Solution { public String LeftRotateString(String str, int n) { String result = ""; if(str == null || str.length() == 0){ return result; } if (str.length() > n) { result = str.substring(n) + str.substring(0, n); } else if (str.length() < n) { for (int i = 0; i < n / str.length() + 2; i++) { result += str; } result = result.substring(n, n + str.length()); } else { result = str; } return result; } }
public class Solution { public String LeftRotateString(String str, int n) { if(str.length() == 0) return ""; char[] cstr = str.toCharArray(); int len = cstr.length; char[] ans = new char[len]; int pers = n % len; for (int i = 0; i < len; i++) { if (i < pers) ans[i] = cstr[i]; if (i + pers < len) cstr[i] = cstr[i + pers]; } for (int i = 0; i < pers; i++) { cstr[i + len - pers] = ans[i]; } return new String(cstr); } }
import java.util.Deque; import java.util.LinkedList; public class Solution { public String LeftRotateString(String str,int n) { if(str==null||str.equals("")) return ""; char[] chars=str.toCharArray(); Deque<Character> deque=new LinkedList(); for(char c:chars) deque.offer(c); for(int i=0;i<n;i++){ char c=deque.poll(); deque.offer(c); } StringBuilder builder=new StringBuilder(); while(!deque.isEmpty()) builder.append(deque.poll()); return builder.toString(); } }
public String LeftRotateString(String str, int n) { if (str.isEmpty()) return ""; int len = str.length(); n = n % len; StringBuilder sb = new StringBuilder(str); sb.append(sb.substring(n)); sb.append(sb.subSequence(0, n)); sb.delete(0, len); return sb.toString(); }
static class JZLeftRotateString { public String LeftRotateString(String str, int n) { if (str.isEmpty()) return ""; int len = str.length(); n = n % len; char[] strs = str.toCharArray(); reverse(strs, 0, len - 1); reverse(strs, 0, len - n - 1); reverse(strs, len - n, len - 1); return new String(strs); } public void reverse(char[] strs, int left, int right) { while (left < right) { char tmp = strs[left]; strs[left++] = strs[right]; strs[right--] = tmp; } } }
public class Solution { public String LeftRotateString(String str,int n) { if(str.length()==0) return str; int len=str.length(); n=n%len; return str.substring(n,len)+str.substring(0,n); } }
public class Solution { public String LeftRotateString(String str, int n) { if (str.isEmpty()) { return ""; } int m = str.length(); // n有可能大于m 取模 n = n % m; char[] s = str.toCharArray(); reverse(s, 0, m - 1); reverse(s, 0, m - n - 1); reverse(s, m - n, m - 1); return new String(s); } public void reverse(char[] s, int start, int end) { while (start < end) { swap(s, start++, end--); } } public void swap(char[] s, int a, int b) { char temp = s[a]; s[a] = s[b]; s[b] = temp; } }
import java.util.*; public class Solution { public String LeftRotateString(String str,int n) { if (str.length() <= 1){ return str; } StringBuilder ans = new StringBuilder(str); while (n > 0){ String s = ans.substring(0,1); int end = ans.length(); ans = new StringBuilder(ans.substring(1,end)); ans.append(s); n--; } return ans.toString(); } }
public class Solution { public String LeftRotateString(String str, int n) { if (str == null || str.equals("")) { return ""; } int length = str.length(); int trueN = n % length; return new StringBuilder().append(str.substring(trueN)).append(str.substring(0, trueN)).toString(); } }
public class Solution { public void revser(char[]chars, int i, int j) { while (i < j) { char ch = chars[i]; chars[i] = chars[j]; chars[j] = ch; i++; j--; } } public String LeftRotateString(String str, int n) { char[]chars = str.toCharArray(); if (str.length() == 0) return ""; n = n % str.length(); revser(chars, 0, n - 1); revser(chars, n, str.length() - 1); revser(chars, 0, str.length() - 1); return new String(chars); } }
楼里答案好多感觉都不全啊
public class Solution {
public String LeftRotateString(String str,int n) { if(str.length()==0){ return str; } if(n>str.length()){ n=n%str.length(); } String s=str.substring(0,n); String s1=str.substring(n,str.length()); return s1+s; }
}
public class Solution { private void reverse(char[] c, int i, int j) { // 给下标从i到j的这一段字符都首尾两两交换,这也就起到了反转的效果 while (i < j) { swap2(c, i++, j--); } } private void swap2(char[] c, int i, int j) { // 交换字符数组下标i和j的两个字符 char temp = c[i]; c[i] = c[j]; c[j] = temp; } public String LeftRotateString(String str, int n) { if (str.length() == 0 || n == 0) { return str; } if (n >= str.length()) { n %= str.length(); } char[] chars = str.toCharArray(); // 第 n 位置,下标自然是 n - 1 // 先将 "abc" reverse(chars, 0, n - 1); // 再将剩下的反转 reverse(chars, n, chars.length - 1); // 最后把整体反转 reverse(chars, 0, chars.length - 1); return new String(chars); } }
public class Solution { public String LeftRotateString(String str,int n) { // 解法1优化:n取模来避免无用遍历 // 边界 if(str.length()==0||str==null){ return ""; } // 取模 n %= str.length(); // 创建sb StringBuilder sb = new StringBuilder(str); // 循环n次,每次左移一位 while(n>0){ // 取第一位 char temp = sb.charAt(0); // 删除第一位 sb.deleteCharAt(0); // 添加至最后一位 sb.append(temp); n--; } return sb.toString(); } }
import java.util.*; public class Solution { public String LeftRotateString(String str,int n) { LinkedList<Character> linkedlist = new LinkedList<>(); ArrayList<Character> tempList = new ArrayList<>(); String res = ""; if (str.length() == 0) return res; if (str.length() == n) return str; int num = 0; if (n < str.length()){ num = n; }else{ num = n % str.length(); } for (int i = 0; i < str.length(); i++){ linkedlist.add(str.charAt(i)); } for (int j = 0; j < num; j++){ tempList.add(linkedlist.get(j)); } for (int k = 0; k < num; k++){ linkedlist.removeFirst(); linkedlist.add(tempList.get(k)); } for (int i = 0; i < linkedlist.size(); i++){ res = res + linkedlist.get(i) + ""; } return res; } }
public class Solution { public String LeftRotateString(String str,int n) { if(str.length()==0||str==null) return ""; StringBuilder sb=new StringBuilder(str); for(int i=0;i<n;i++){ sb.append(String.valueOf(str.charAt(i%str.length()))); } return sb.toString().substring(n,sb.length()); } }