题目: 将一个交错数据合并为一个一维数组
输入: strJaggedArray[][], 由多个一维数组(长度不定,个数不定)组成的交错数组
输出: strArray[], 由strJaggedArray[r][c]中的元素以"&"为分隔符拼合而成, 是strJaggedArray中数组元素的无重复组合(不考虑顺序)
举例:
输入: strJaggedArray[0]=new string[] {"we","are","student"};
strJaggedArray[1]=new string[] {"say","what"};
输出: strArray[]={"we&say","we&what","are&say","are&what","student&say","student&what"}
输入: strJaggedArray[0]=new string[] {"cs","app"};
strJaggedArray[1]=new string[] {"good","cool","dev"};
strJaggedArray[2]=new string[] {"king","of","the","world"};
输出: strArray[]={"cs&good&king","cs&good&of","cs&good&the","cs&good&world","cs&cool&king","cs&cool&of","cs&cool&the","cs&cool&world",...}
好久没玩玩TopCoder了,工作时间不允许,看到这样的题目多多少少有种熟悉的感觉....,如果现在再打TopCoder的话,因该比去年进步了很多了吧........
本来是C#的题,不过最近学JAVA又没书看,只好找题做了...
public
class
ExchangeArray
...
{
/** *//**
* @Auth Red_angelX
* @param args
*/
public static void main(String[] args) ...{
// TODO Auto-generated method stub
String[][] strJaggedArray = new String[3][];
strJaggedArray[0] = new String[] ...{"cs","app"};
strJaggedArray[1] = new String[] ...{"good","cool","dev"};
strJaggedArray[2] = new String[] ...{"king","of","the","world"};
String[] strArray = Exchange(strJaggedArray);
for(int i=0; i< strArray.length ;i++)
...{
System.out.println(strArray[i]);
}
}
/** *//**
* 函数
* @param strJaggedArray
* @return
*/
public static String[] Exchange(String[][] strJaggedArray)
...{
String[][] temp = DoExchange(strJaggedArray);
return temp[0];
}
/** *//**
* 递归
* @param strJaggedArray
* @return
*/
private static String[][] DoExchange(String[][] strJaggedArray)
...{
int len = strJaggedArray.length;
if(len >= 2)
...{
int len1 = strJaggedArray[0].length;
int len2 = strJaggedArray[1].length;
int newlen = len1*len2;
String[] temp = new String[newlen];
int Index = 0;
for(int i=0;i<len1;i++)
...{
for(int j=0;j<len2;j++)
...{
temp[Index] = strJaggedArray[0][i] + "&"+strJaggedArray[1][j];
Index ++;
}
}
String[][] newArray = new String[len-1][];
for(int i=2;i<len;i++)
...{
newArray[i-1] = strJaggedArray[i];
}
newArray[0] = temp;
return DoExchange(newArray);
}
else
...{
return strJaggedArray;
}
}
}