【发布时间】:2013-04-25 23:31:56
【问题描述】:
创建并返回一个新数组,该数组是作为参数传递的数组的反向副本。
我的代码如下。我被算法困住了?这是一道考试题,但现在考试已经结束了。
import java.util.*;
public class Q4_ArrayReverse
{
public static String[] reverse( String[] in )
{
String[] out = new String[ in.length ];
reverse( in, out, 0 );
return out;
}
///////////////////////////////////////////////////////////
//
// Given an input array, an output array and an index,
// copy the element at the index offset of the input array
// to the appropriate reversed position in the output array.
// i.e. in[ 0 ] -> out[ n - 1 ]
// in[ 1 ] => out[ n - 2 ]
// etc.
// After doing the copy for index, recurse for index + 1
// Be sure to quit when you get to the end of the array.
//
//////////////////////////////////////////////////////////
static void reverse( String[] in, String[] out, int index )
{
}
【问题讨论】:
-
好吧,把它分解成它的组件。在递归中,总是需要有一个终止条件。
-
Q4_ArrayReverse是一个讨厌的类名,不符合 Java 命名约定。