私はアルゴリズムを実践しており、再帰は常に私の弱点です。この問題は、ネストされた配列を単一の配列にまとめることを要求します。これは、O(n^3)[与えられた等しい大きさの3dアレイ]解を与えるループを使用するならば、簡単であろう。Java-再帰を使用して配列を平滑化
しかし、再帰で私は数時間苦労しています。これは私が持っているものです、私は別のソリューションを試して私のコードで手抜きしていることに注意してください、これは私があなたに投稿するにそれを残すことを決めたものです。
私が欲しいのは、現在のコードを修正して正しい出力を得ることができますか、再帰を使用してこのコードを書くのが簡単で難解な方法です。
ボーナス質問ネストされた配列のサイズがわからない場合は、どのように再帰を使用してこの問題について説明しますか? [OK]を
EDIT ので、いくつかのハード(私はしたくないもの)をコードした後、私はこの仕事を得ることができました。しかし、コードは現在ハードコード化されており、非常に面倒です。コードをクリーンアップするか、再帰を使用してこれを解決する簡単な方法に行くのですか?私がREDO-INGのヘルパーメソッドの再帰を使用してこの問題をしようとしています
EDIT2 。私はこのスタイルを使用して、より良い運を持っている場合、私は表示されます
import java.io. * ;
import java.util. * ;
class Solution {
// static int oneLen = 0;
//static int twoLen = 0;
//static int threeLen = 0;
static int oneCnt = 0;
static int twoCnt = 0;
static int threeCnt = 0;
static ArrayList <Integer> result = new ArrayList <Integer>();
public static ArrayList <Integer> flatten(int [][][] arr){
if (oneCnt < arr[threeCnt][twoCnt].length && !(oneCnt == 2 && twoCnt == 2 && threeCnt == 2))
{
if (oneCnt == 0 && twoCnt == 0 && threeCnt == 0){
result.add(arr[threeCnt][twoCnt][oneCnt]);
oneCnt++;
result.add(arr[threeCnt][twoCnt][oneCnt]);
System.out.println("Line One");
System.out.println("Count1: " + oneCnt);
System.out.println("Count2: " + twoCnt);
System.out.println("Count3: " + threeCnt);
}
oneCnt++;
if (oneCnt != 3){
result.add(arr[threeCnt][twoCnt][oneCnt]); }
System.out.println("Line One");
System.out.println("Count1: " + oneCnt);
System.out.println("Count2: " + twoCnt);
System.out.println("Count3: " + threeCnt);
flatten(arr);
} else if (oneCnt == arr[threeCnt][twoCnt].length && twoCnt < arr[threeCnt].length - 1){
//oneLen = 0;
oneCnt = 0;
// twoLen++;
twoCnt++;
result.add(arr[threeCnt][twoCnt][oneCnt]);
System.out.println("Line Two");
System.out.println("Count:1 " + oneCnt);
System.out.println("Count:2 " + twoCnt);
System.out.println("Count:3 " + threeCnt);
flatten(arr);
}
else if (oneCnt == arr[threeCnt][twoCnt].length && twoCnt == arr[threeCnt].length - 1 && threeCnt < arr.length - 1){
oneCnt = 0;
twoCnt = 0;
threeCnt++;
result.add(arr[threeCnt][twoCnt][oneCnt]);
System.out.println("Line Three");
System.out.println("Count:1 " + oneCnt);
System.out.println("Count:2 " + twoCnt);
System.out.println("Count:3 " + threeCnt);
flatten(arr);
}
return result;
}
public static void main(String[] args) {
int[][][] array =
{ { {1, 2, 3}, { 4, 5, 6}, { 7, 8, 9} },
{ {10, 11, 12}, {13, 14, 15}, {16, 17, 18} },
{ {19, 20, 21}, {22, 23, 24}, {25, 26, 27} } };
flatten(array);
for (int i = 0; i < result.size(); i++){
System.out.print(result.get(i) + ",");
}
}
}
出力:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 、16,17,18,19,20,21,22,23,24,25,26,27、
EDIT3 私はほとんどの答えを持っていますが、最後の要素文句を言わないヘルパー再帰を使用した後arraylistに追加します。
import java.io. * ;
import java.util. * ;
class Solution {
static ArrayList <Integer> result = new ArrayList <Integer>();
public static void flatten(int [][][] arr){
int oneLen = 0;
int twoLen = 0;
int threeLen = 0;
flattenHelper(arr, oneLen, twoLen, threeLen);
}
public static void flattenHelper(int [][][] arr, int oneLen, int twoLen, int threeLen){
if (oneLen < arr[threeLen][twoLen].length - 1){
System.out.println("Line One");
System.out.println("Count:1 " + oneLen);
System.out.println("Count:2 " + twoLen);
System.out.println("Count:3 " + threeLen);
result.add(arr[threeLen][twoLen][oneLen]);
flattenHelper(arr, oneLen + 1, twoLen, threeLen);
}
else if (twoLen < arr[threeLen].length - 1){
System.out.println("Line Two");
System.out.println("Count:1 " + oneLen);
System.out.println("Count:2 " + twoLen);
System.out.println("Count:3 " + threeLen);
result.add(arr[threeLen][twoLen][oneLen]);
flattenHelper(arr, oneLen = 0, twoLen + 1, threeLen);
} else if (threeLen < arr.length - 1){
System.out.println("Line Two");
System.out.println("Count:1 " + oneLen);
System.out.println("Count:2 " + twoLen);
System.out.println("Count:3 " + threeLen);
result.add(arr[threeLen][twoLen][oneLen]);
flattenHelper(arr, oneLen = 0, twoLen = 0, threeLen + 1);
}
}
public static void main(String[] args) {
int[][][] array =
{ { {1, 2, 3}, { 4, 5, 6}, { 7, 8, 9} },
{ {10, 11, 12}, {13, 14, 15}, {16, 17, 18} },
{ {19, 20, 21}, {22, 23, 24}, {25, 26, 27} } };
flatten(array);
for (int i = 0; i < result.size(); i++){
System.out.print(result.get(i) + ",");
}
}
}
出力:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21 、22,23,24,25,26,
ありがとう!これはまさに私が持っていたボーナスの質問に答えるという点で私が探していたものです。 – user3051442
リフレクションライブラリの機能と、このソリューションとの関連性を尋ねることはできますか?それに関する文書を探してみて、ありがとう。 – user3051442
ここには良いスタートがあります: https://docs.oracle.com/javase/tutorial/reflect/ これは基本的にJavaを少し「剛性の低い」ものにし、Pythonなどの言語でより一般的に見られるいくつかの方法をもたらしますとRuby。 コンパイル時にすべてを知ることができず、ランタイムを待つ必要があることがあります。このオブジェクトは配列のように見えますが、int [] []かArrayListかもしれませんが、すべての要素を繰り返し処理したいだけです。 –