[53] (HashSet) Repeated numbers in the array | Search in a two-dimensional array

Repeated numbers in the array

Problem Description

Find the repeated numbers in the array.

All numbers in an array nums of length n are in the range of 0~n-1. Some numbers in the array are repeated, but I don't know how many numbers are repeated, nor do I know how many times each number is repeated. Please find any duplicate number in the array.

Problem-solving ideas

Add the elements in the array to the hash table, and judge whether there are corresponding elements in the table while adding them, and if so, return the element that is being added.

class Solution {
    
    
    public int findRepeatNumber(int[] nums) {
    
    
        Map<Integer,Integer> map = new HashMap<>();
        for(int num : nums){
    
    
            if(map.get(num) != null)
                return num;
            map.put(num,1);
        }
        return 0;
    }
}

Time complexity: O(n)
Space complexity: O(n)

The set container is used in the solution:

class Solution {
    
    
    public int findRepeatNumber(int[] nums) {
    
    
        Set<Integer> set = new HashSet<Integer>();
        int repeat = -1;
        for (int num : nums) {
    
    
            if (!set.add(num)) {
    
    //因为有重复的元素了,所有add失败
                repeat = num;
                break;
            }
        }
        return repeat;
    }
}

Small summary

The usage of Set in java:

The characteristics of the Set collection: the same elements cannot be stored.

And because it is an abstract interface: it cannot directly instantiate a set object. (Ie Set s = new Set() error)

This interface mainly inherits from the Collections interface, so it has some common methods of Collection:
Insert picture description here

The two longest used implementations of the Set interface: HashSet and TreeSet

TreeSet: The elements inside will be sorted by default.

Lookup in a two-dimensional array

Problem Description

In an n * m two-dimensional array, each row is sorted in increasing order from left to right, and each column is sorted in increasing order from top to bottom . Please complete an efficient function, input such a two-dimensional array and an integer to determine whether the array contains the integer.

example:
Insert picture description here

Problem-solving ideas

The idea comes from the solution of the problem .

Position the initial position to the last element in the first column. If the element is not equal to the target element and larger than the target element, go back one line, if it is smaller than the target element, add a column until the target element is found.

class Solution {
    
    
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
    
    
        int n = matrix.length;
        if(n==0) return false;
        int m = matrix[0].length;
        int i = n-1,j = 0;
        while(true){
    
    
            if(i < 0 || j >= m) return false;
            if(matrix[i][j] == target) return true;
            if(matrix[i][j] < target) 
                j++;
            else
                i--;
        }
    }
}

Time complexity: O(m+n)
Space complexity: O(1)

Experience

Learn to be good at using the characteristics of the given data to solve problems.

Guess you like

Origin blog.csdn.net/qq_43424037/article/details/114832579