Repeated numbers in arrays, find and replace spaces in two-dimensional arrays, and merge of two ordered arrays A1A2

1. Repeated numbers in an array

  • Solution 1: Sort the array quickly, and sort an array of length n takes O(nlogn) time.
  • Solution 2: Use HashMap to record the number of occurrences of each number, and the first one greater than 2 can be returned. Time replication is O(n), and a hash table of size O(n) is required as a cost.
  • Solution three: In fact, it is also a general idea. The condition of the title is "In an array of length n, all numbers are in [0,n-1]". Then we can complete this problem in O(n) time complexity without opening up new storage space. That is to traverse from the beginning, the purpose is to store the corresponding value in each position, for example, a[0] stores 0, a[3] stores 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. Search in a two-dimensional array

  • Solution 1: First select the number in the upper right corner of the array. If the number is equal to the number to be searched, the search ends. If the number is greater than the number to be searched, the column where the number is located is eliminated; if the number is less than the number to be searched, the row where the number is located is eliminated.
  • Solution 2: Binary search method.
/**
 * @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. Replace spaces

  • Solution 1: Scan the string from beginning to end and replace each time a space character is encountered. Since 1 character is replaced with 3 characters, we must move all the characters behind the space by 2 bytes, otherwise, two characters will be overwritten. The time efficiency is O(n^2).
  • Solution 2: Move characters on the original string. (Move from back to forward. First traverse the number of spaces, set the length of the string, and traverse the string from back to forward). The time efficiency is 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 Merging of two ordered arrays 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);
        }
    }
}

Guess you like

Origin blog.csdn.net/Zmd_23/article/details/108697539