採樣輸入# 「左移」 1鑑於字符作爲輸入的一個陣列,返回一個陣列,其中元件已經由一個
shift({'a','b','c','d','e'})
樣本輸出#1
{'b','c','d','e','a'}
public class ShiftElements {
static char[] testcase1 = {'a', 'b', 'd', 'c', 'b', 'd', 'c'};
public static void main(String args[]) {
ShiftElements testInstance = new ShiftElements();
char[] result = testInstance.shift(testcase1);
System.out.println(result);
}
public char[] shift(char[] elements) {
if (elements.length >= 2) {
int temp = elements[0];
for (int i = 0; i < elements.length - 1; i++)
elements[i] = elements[i + 1];
temp = elements[elements.length - 1];
}
return elements;
}
當我試圖運行測試用例時,它失敗了我的輸入{'b','c','d','e','a'}'
。我的輸出{'c','d','e','a','a'}
正確輸出{'c','d','e','a','b'}
。該怎麼辦?
供參考:因爲你就地改變了數組,所以不需要返回數組。只需打印'testcase1'。 – Andreas