汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列 S ,请你把其循环左移 K 位后的序列输出。例如,字符序列 S = ”abcXYZdef” , 要求输出循环左移 3 位后的结果,即 “XYZdefabc”
数据范围:输入的字符串长度满足 ,
进阶:空间复杂度 ,时间复杂度
"abcXYZdef",3
"XYZdefabc"
"aab",10
"aba"
public class Solution { public String LeftRotateString(String str,int n) { char[] chars = str.toCharArray(); if(chars.length < n) return ""; reverse(chars, 0, n-1); reverse(chars, n, chars.length-1); reverse(chars, 0, chars.length-1); StringBuilder sb = new StringBuilder(chars.length); for(char c:chars){ sb.append(c); } return sb.toString(); } public void reverse(char[] chars,int low,int high){ char temp; while(low<high){ temp = chars[low]; chars[low] = chars[high]; chars[high] = temp; low++; high--; } } }
/**
* 题目描述
* 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,
* 就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,
* 请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,
* 要求输出循环左移3位后的结果,即“XYZdefabc”。
* 是不是很简单?OK,搞定它!
*
* @author shijiacheng
*/
public class LeftRotateStringSolution {
public String LeftRotateString(String str, int n) {
char[] chars = str.toCharArray();
if (chars.length < n) {
return "";
}
reverse(chars, 0, n - 1);
reverse(chars, n, chars.length - 1);
reverse(chars, 0, chars.length - 1);
return new String(chars);
}
public void reverse(char[] chars, int start, int end) {
while (start < end) {
char temp = chars[start];
chars[start] = chars[end];
chars[end] = temp;
start++;
end--;
}
}
}
class Solution { public: string LeftRotateString(string str, int n) { int len=str.length(); if(len<=0||n<=0) return str; //假设输入str为abcXYZdef,n=3 Reverse(str,0,n-1); //反转前n个字符,得到cbaXYZdef Reverse(str,n,len-1); //反转第n个字符后面所有的字符cbafedZYX Reverse(str,0,len-1); //反转整个字符串XYZdefabc return str; } void Reverse(string &str,int begin,int end) { int temp; while(begin<end) { temp=str[begin]; str[begin]=str[end]; str[end]=temp; begin++,end--; } } };
//C++ /*解法一:在原字符串上修改 “abcdef”循环左移9位(3位) 前3位逆序,后3位逆序,整体再逆序。 “cbafed”-> “defabc” */ class Solution { public: string LeftRotateString(string str, int n) { int len = str.size(); if(len <= 0) return ""; n = n%len; if(n==0) return str; reverseStr(str,0,n-1); reverseStr(str,n,len-1); reverseStr(str,0,len-1); return str; } void reverseStr(string &str, int left, int right) { for(int i=left,r=right;i<=left+(right-left)/2;++i){ swap(str[i],str[r--]); } } }; /*解法二: 借助n个大小的额外空间 */class Solution {public:string LeftRotateString(string str, intn) {string ret;intlen = str.size();if(len <= 0)return"";n = n%len;ret = str+str.substr(0,n);ret = ret.substr(n,str.size());returnret;}};
# -*- coding:utf-8 -*- class Solution: def LeftRotateString(self, s, n): # write code here res, length = list(s), len(s) if n > length : return "" for i in range(int(n/2)): res[i], res[n-1-i] = res[n-1-i], res[i] for i in range(n, int((n+length)/2)): res[i], res[length-1-i+n] = res[length-1-i+n], res[i] for i in range(int(length/2)): res[i], res[length-1-i] = res[length-1-i], res[i] return "".join(res)
public class Solution { /** * 思路: * 1.先翻转前半部分 * 2.再翻转后半部分 * 3.再对字符串整个进行翻转 * * 考点:不使用库对字符串进行灵活的翻转 */ public String LeftRotateString(String str,int n) { if (str == null || str.length() == 0 || n <= 0) { return str; } if (n >= str.length()) { n = n % str.length(); } char[] ch = str.toCharArray(); reverseString(ch, 0, n - 1); reverseString(ch, n, str.length() - 1); reverseString(ch, 0, str.length() - 1); return new String(ch); } /** * 对字符数组 ch 的 start 到 end 范围内的字符进行翻转 */ public void reverseString(char[] ch, int start, int end) { while (start < end) { char temp = ch[start]; ch[start] = ch[end]; ch[end] = temp; start++; end--; } } }
package String;
/**
* 左旋转字符串
* 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。
* 对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。
* 例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!
*/
public class Solution31 {
public static void main(String[] args) {
Solution31 solution31 = new Solution31();
String str = "abcXYZdef";
System.out.println(solution31.LeftRotateString(str, 3));
}
public String LeftRotateString(String str, int n) {
if (str == null || str.length() == 0) return "";
StringBuilder sb1 = new StringBuilder(str.substring(0, n));
StringBuilder sb2 = new StringBuilder(str.substring(n, str.length()));
sb2.append(sb1);
return sb2.toString();
}
}
char *LeftRotateString(char *str, int n) { char *tmp; tmp = (char *)malloc((n + 1)*sizeof(char)); if (!tmp) { /* code */ printf("allocate memory failed!.\n"); exit(1); } memcpy(tmp, str, n); *(tmp + n) = '\0'; memcpy(str, str + n, strlen(str) - n); *(str + strlen(str) - n) = '\0'; strcat(str, tmp); free(tmp); return str; }
class Solution: def LeftRotateString(self, s, n): # write code here res = [] tmp = [] c = ['']*len(s) i = 0 if n >=len(s):return s for j in s: c[i]=j i+=1 for _ in range(n): a = c.pop(0) tmp.append(a) for k in c: res.append(k) for f in tmp: res.append(f) return ''.join(res) print(Solution().LeftRotateString('abcXYZdef', 4))
# -*- coding:utf-8 -*- class Solution: def LeftRotateString1(self, s, n): if not s: return "" if n<0: return k = n%len(s) return s[k:]+s[:k] def LeftRotateString(self, s, n): if not s: return "" if n<0: return k = n%len(s) #c = (s[:k][::-1]+s[k::][::-1]) #return c[::-1] return (s[:k][::-1]+s[k::][::-1])[::-1]
/*1、这一题使用队列就可以解决,没从头部移出一个放到一个变量中 然后再将变量中的值再次放入到队列中。 队列满足先进先出 2、使用StringBuffer进行字符串操作 */ import java.util.*; public class Solution { public String LeftRotateString(String str,int n) { if(str.length()==0 || str.equals("")){ return ""; } Queue<String> queue = new LinkedList<String>(); char[] s = str.toCharArray(); for(int i=0;i<s.length;i++){ queue.add(s[i]+""); } //开始移位 for(int j=0;j<n;j++){ String tmp = queue.remove(); queue.add(tmp); } //将字符数组再次变成字符串 StringBuffer sb = new StringBuffer(); for(int k=0;k<s.length;k++){ sb.append(queue.remove()); } return sb.toString(); } }
//和翻转单词顺序是一样的原理,分为两段各做一次翻转再做一次总的翻转。主要注意输入为空串的情况和越界的问题,一定要注意这些细节 class Solution { public: void Reverse(string&str,int start,int end) { if(start>=end) return; while(start<end) { char c = str[start]; str[start]=str[end]; str[end] = c; start++; end--; } } string LeftRotateString(string str, int n) { int length = str.size(); if(length<=0) return str; if(length>0&& n>0&&n<length) { int pFirstStart = 0; int pFirstEnd = 0+n-1; int pSecondStart = 0+n; int pSecondEnd = 0+length-1; Reverse(str,pFirstStart,pFirstEnd); Reverse(str,pSecondStart,pSecondEnd); Reverse(str,pFirstStart,pSecondEnd); } return str; } };