返回信息流Given a string and an offset, rotate string by offset. (rotate from left to right)
Example
Given "abcdefg".
offset=0 => "abcdefg"
offset=1 => "gabcdef"
offset=2 => "fgabcde"
offset=3 => "efgabcd"
错误代码:
```JAVA
public class Solution {
/**
* @param str: an array of char
* @param offset: an integer
* @return: nothing
*/
public void rotateString(char[] str, int offset) {
// write your code here
char[] c = str;
int n = str.length;
offset = offset % n;
for (int i = offset; i < n; i++){
str[i] = c[i - offset];
};
for (int i = 0; i < offset; i++){
str[i] = c[i + n - offset];
}
}
}
```
Wrong Answer:
Your input
"abcdefg", 3
Your output
"efgefge"
Expected
"efgabcd"
这是一条镜像帖。来源:北邮人论坛 / java / #54271同步于 2016/12/8
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
【问题】Rotate String 给定偏移量由左向右旋转字符串
buwenyuwu
2016/12/8镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
不是??把str的数据保存到c中,
【 在 nuanyangyang 的大作中提到: 】
: char[] c = str;
: 你是想说
: char[] c = new char[str.length];
: 吗?
:
发自「贵邮」
【 在 nuanyangyang 的大作中提到: 】
: Java里,数组是对象,如果你直接说c=str,意思是变量c指向和str同一个数组,而不是拷贝一份。
: 其实这道题有原地的算法
改成System.arraycopy(str,0,c,0,str.length)后可以了!谢谢暖神(这个没搞懂就还没去看原地的解法……)