3

何よりもまず、私は Java 初心者です。リストやハッシュテーブルを含まないこの問題への簡潔なアプローチを探していましたが、まだ見つかりませんでした:

**これは宿題ではありませんが、「Building Java Programs」の第 7 章の演習 14 です。

整数の 2 つの配列をパラメーターとして受け取り、2 番目の配列の要素が最初の配列に現れるかどうかを示すブール値を返す、contains というメソッドを作成します。

例:

Integer[] list1 = {1,6,2,1,4,1,2,1,8};

Integer[] list2 = {1,2,1};

を呼び出すcontains(list1, list2)と、 が返されtrueます。配列を反復できるネストされた for ループのアイデアは得られますが、明確な解決策がわかりません。

public static Boolean contains(Integer[] listOfNumbers1, Integer[] listOfNumbers2){

    for(int i = 0 ; i < listOfNumbers2.length; i++){

        for(int j = 0 ; j < listOfNumbers1.length; j++){

        }
    }

    return true;
}
4

3 に答える 3

2

(重複を考慮する必要があるかどうかは実際には指定しません。例から、array1にarray2のすべての要素が順番にあるかどうかを確認しようとしているように見えます)

考慮すべきいくつかの異なるケースがあります。

1. array2 is longer than array1:
       if this is the case the result must be false because array1 can't 
       possibly have the same elements in order since array2 is longer

2. array2 is the same size as array1: 
       for this step you can use a single loop and compare the elements in order, 
       if you find a  mismatch then array1 does not contain array2

       for i from 0 to length do
           if array1[i] != array2[i]
              // false
           end
       end
       // true

3. array2 is shorter than array1:
       for this case you need to examine every array2.length elements from array1 
       and see if they match array2

       matches = 0
       for i from 0 to array1.length do
           for j from 0 to array2.length do
               if array1[i] == array2[j] 
                  // increment i
                  // increment matches
               end
           end
           if matches == array2.length
               // true
           else
               // false
           end
           matches = 0  // reset matches
       end
于 2012-12-05T04:33:16.487 に答える
1

したがって、基本的には、検索配列の各位置を反復処理し ( listOfNumbers1)、探しているシーケンスの開始点であるかどうかを確認します ( listOfNumbers2) 。

// Loops through the search array
for( int i = 0; i < listOfNumbers1.length; i++ )
{
    boolean found = true;
    for(int j = 0; j < listOfNumbers2.length; j++ )
    {
        /* Check if the following conditions hold
           - Not going to cause an ArrayIndexOutOfBoundsException
           - Values do **not** match => set found to false */
        if( i+j < listOfNumbers1.length && listOfNumbers1[i + j] != listOfNumbers2[j] )
        {
            // Didn't find the sequence here
            found = false;
        }
    }

    // If found is still true, we have found the sequence and can exit
    if( found ) return true;
}

return false;
于 2012-12-05T04:19:52.210 に答える
0
class contains{
    public static void main(String[] args){
        int[] list1 = {1,6,2,1,4,1,2,1,8};
        int[] list2 = {1,2,1};

        if(contains(list1, list2)){
            System.out.println("list2 found in list1");
        }
        else {
            System.out.println("list2 not found in list1");
        }
    }

    public static boolean contains(int[] listOfNumbers1, int[] listOfNumbers2){
        if(listOfNumbers2.length > listOfNumbers1.length){
            return false;
        }
        for(int j = 0 ; j < listOfNumbers1.length - listOfNumbers2.length + 1; j++){
            for(int i = 0 ; i < listOfNumbers2.length; i++){
                if(listOfNumbers1[j+i] != listOfNumbers2[i]){
                    break;
                }
                else {
                    if(i == listOfNumbers2.length-1){
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
于 2012-12-05T04:32:31.337 に答える