配列内の繰り返し数、2次元配列内のスペースの検索と置換、および2つの順序付けられた配列A1A2のマージ

1.配列内の繰り返し数

  • 解決策1:配列をすばやく並べ替え、長さnの配列を並べ替えると、O(nlogn)時間かかります。
  • 解決策2:HashMapを使用して各数値の出現回数を記録します。2より大きい最初の数値を返すことができます。時間レプリケーションはO(n)であり、コストとしてサイズO(n)のハッシュテーブルが必要です。
  • 解決策3:実際にはそれも一般的な考え方であり、タイトルの条件は「長さnの配列では、すべての数値は[0、n-1]にある」です。次に、新しいストレージスペースを開くことなく、O(n)時間の複雑さでこの問題を完了することができます。つまり、最初からトラバースするために、目的は各位置に対応する値を格納することです。たとえば、a [0]は0を格納し、a [3]は3を格納します。
/**
 * @author bro
 * @date 2020/9/17 20:53
 * @desc 数组中重复的数字
 */
public class Duplicate {
    
    
    public static boolean dup(int number[], int length, int[] duplication) {
    
    
        if (number == null || length <= 0) return false;
        for (int i = 0; i < length; i++) {
    
    
            if (number[i] < 0 || number[i] > length - 1) return false;
        }
        for (int i = 0; i < length - 1; i++) {
    
    
            while (number[i] != i) {
    
    
                if (number[i] == number[number[i]]) {
    
    
                    duplication[0] = number[i];
                    return true;
                }
                int temp = number[i];
                number[i] = number[temp];
                number[temp] = temp;
            }
        }
        return false;
    }

    public static void main(String[] args) {
    
    
        int[] number = {
    
    2, 3, 1, 0, 4, 5, 3};
        int[] duplication = new int[1];
        System.out.println(dup(number, number.length, duplication));
    }
}

2. 2次元配列で検索する

  • 解決策1:まず、アレイの右上隅にある番号を選択します。番号が検索対象の番号と等しい場合、検索は終了します。番号が検索対象の番号よりも大きい場合、番号が配置されている列が削除され、番号が検索対象の番号よりも小さい場合、番号が配置されている行が削除されます。
  • 解決策2:バイナリ検索方法。
/**
 * @author bro
 * @date 2020/9/20 9:37
 * @desc 二维数组找数字
 */
public class Find {
    
    
    public static boolean findTo(int target, int[][] array) {
    
    
        boolean flag = false;
        int rows = array.length;
        int columns = array[0].length;
        if (array != null && rows > 0 && columns > 0) {
    
    
            int row = 0;
            int column = columns - 1;
            while (row < rows && column >= 0){
    
    
                if (array[row][column] == target){
    
    
                    flag = true;
                    break;
                }
                else if (array[row][column] > target){
    
    
                    column --;
                }
                else row ++;
            }
        }
        return flag;
    }

    public static void main(String[] args) {
    
    
        int[][] array = {
    
    {
    
    1,2,8,9},{
    
    2,4,9,12},{
    
    4,7,10,13},{
    
    6,8,11,15}};
        System.out.println(findTo(5, array));
    }
}
public class Solution {
    
    
    public static boolean Find(int target, int[][] array) {
    
    
        for (int i = 0; i < array.length; i++) {
    
    
            int low = 0;
            int high = array[i].length - 1;
            while (low <= high) {
    
    
                int mid = (low + high) / 2;
                if (array[i][mid] > target) {
    
    
                    high = mid - 1;
                }
                if (array[i][mid] < target) {
    
    
                    low = mid + 1;
                } else return true;
            }
        }
        return false;
    }
}

 public static void main(String[] args) {
    
    
        int[][] array = {
    
    {
    
    1, 2, 8, 9}, {
    
    2, 4, 9, 12}, {
    
    4, 7, 10, 13}, {
    
    6, 8, 11, 15}};
        System.out.println(Find(7, array));
    }

3.スペースを置き換える

  • 解決策1:文字列を最初から最後までスキャンし、スペース文字が見つかるたびに置き換えます。1文字が3文字に置き換えられるため、スペースの後ろのすべての文字を2バイト移動する必要があります。移動しないと、2文字が上書きされます。時間効率はO(n ^ 2)です。
  • 解決策2:元の文字列の文字を移動します。(後ろから前に移動します。最初にスペースの数を移動し、文字列の長さを設定し、文字列を後ろから前に移動します)。時間効率はO(n)です。
/**
 * @author bro
 * @date 2020/9/20 16:02
 * @desc 替换空格
 */
public class ReplaceBlank {
    
    
    public static String Replace(StringBuffer s) {
    
    
        if (s.length() <= 0 && s == null) return "";
        int numberOfBlack = 0;
        int originalLength = s.length() - 1;
        for (int i = 0; i < originalLength; i++) {
    
    
            if (s.charAt(i) == ' ') {
    
    
                numberOfBlack++;
            }
        }
        int newLength = s.length() + numberOfBlack * 2;
        int i = newLength -1;
        s.setLength(newLength);
        while (originalLength >= 0 && i > originalLength) {
    
    
            if (s.charAt(originalLength) == ' ') {
    
    
                s.setCharAt(i--, '0');
                s.setCharAt(i--, '2');
                s.setCharAt(i--, '%');
            } else s.setCharAt(i--,s.charAt(originalLength));
            originalLength--;
        }
        return s.toString();
    }

    public static void main(String[] args) {
    
    
        StringBuffer str = new StringBuffer("We are happy");
        System.out.println(Replace(str));
    }
}

3.1順序付けられた2つの配列A1 A2のマージ

/**
 * @author bro
 * @date 2020/9/20 17:15
 * @desc 两个有序数组 A1 A2 的合并
 */
public class OrderArrayMerge {
    
    
    public static int[] merge(int[] A1, int[] A2) {
    
    
        if (A1 == null && A2 == null) return null;
        int A1A2Length = A1.length - 1;
        int A2Length = A2.length - 1;
        int A1Length = A1.length - A2.length - 1;
        if (A2.length <= 0) return A1;
        while (A2Length >= 0 && A1Length >= 0) {
    
    
            if (A2[A2Length] >= A1[A1Length]) {
    
    
                A1[A1A2Length] = A2[A2Length--];
            } else A1[A1A2Length] = A1[A1Length--];
            A1A2Length--;
        }
        return A1;
    }

    public static void main(String[] args) {
    
    
        int[] A1 = {
    
    1, 5, 7, 8, 9, 12, 20, 39, 0, 0, 0, 0, 0, 0, 0};
        int[] A2 = {
    
    3, 5, 7, 13, 15, 23, 45};
        for (int num : merge(A1, A2)) {
    
    
            System.out.println(num);
        }
    }
}

おすすめ

転載: blog.csdn.net/Zmd_23/article/details/108697539