Test Development Preparation for Autumn Recruitment Interview 11-Hash

After working hard for so many years, looking back, it is almost all long setbacks and sufferings. For most people's life, smooth sailing is only occasional, and frustration, unbearable, anxiety and confusion are the main theme. We take the stage we did not choose, play the script we did not choose. Keep going!

Table of contents

1. The sum of two numbers

2. Numbers that appear more than half of the time in the array

3. Two numbers that appear only once in the array

4. The missing first positive integer

5. The sum of three numbers


1. The sum of two numbers

Topic Link: Sum of Two Numbers_Niuke题盘_Niuke.com
Idea: Use hashmap to store, O(n) time complexity.
Java version:

import java.util.*;


public class Solution {
    /**
     * 
     * @param numbers int整型一维数组 
     * @param target int整型 
     * @return int整型一维数组
     */
    public int[] twoSum (int[] numbers, int target) {
        // write code here
        HashMap<Integer, Integer> map = new HashMap<>() ;
        for(int i=0; i<numbers.length; i++){
            if(map.containsKey(target - numbers[i])){
                return new int [] {map.get(target-numbers[i])+1, i+1} ;
            }
            map.put(numbers[i], i) ;
        }
        return new int []{} ;
    }
}

2. Numbers that appear more than half of the time in the array

Topic link: Numbers that appear more than half of the time in the array_Niuke题目霸_Niuke.com
Idea: Map stores the number of times that a number appears, and finds the number that appears more than half of the time.
Java version:

import java.util.* ;

public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
       Map<Integer, Integer> map = new HashMap<>() ;
       int i = 0 ;
       for(; i<array.length; i++){
       map.put(array[i],map.getOrDefault(array[i],  0)+1) ;
        if(map.get(array[i]) > array.length / 2){
            break ;
        }
       }  
       return array[i] ;
    }
}

3. Two numbers that appear only once in the array

Topic link: Two numbers that appear only once in an array_Niu Ke Topic_Niu Ke Net

Idea: use hashmap to store, appear and store in order, and delete twice.

Java version:

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param array int整型一维数组 
     * @return int整型一维数组
     */
    public int[] FindNumsAppearOnce (int[] array) {
        // write code here
        Map<Integer, Object> map = new HashMap<>() ;
        for(int i=0; i<array.length; i++){
            if(map.containsKey(array[i])){
                map.remove(array[i], null) ;
            }else{
                map.put(array[i], null) ;
            }
        }
        int [] ans = new int [map.size()] ;
        int i = 0 ;
        for(Integer res : map.keySet()){
            ans[i++] = res ;
        }
        return ans ;
    }
}

4. The missing first positive integer

Topic Link: The Missing First Positive Integer

Idea: Guarantee the time complexity of O(n), store it with map, and then judge whether it exists in the map from 1.
Java version:

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param nums int整型一维数组 
     * @return int整型
     */
    public int minNumberDisappeared (int[] nums) {
        // write code here
        Map<Integer, Integer> map = new HashMap<>() ;
        for(int i=0; i<nums.length; i++){
            map.put(nums[i], 1);
        }
        int j = 1 ;
        for(j=1; ; j++){
            if(!map.containsKey(j)){
                break ;
            }
        }

        return j ;
    }
}

5. The sum of three numbers

Topic Link: Sum of Three Numbers_Niuke Question Master_Niuke.com

Idea: violent method, three-layer loop, time complexity O(n3).

Java version:
 

import java.util.*;
public class Solution {
    public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
        ArrayList<ArrayList<Integer>> list = new ArrayList<>() ;
        Arrays.sort(num) ;
        for(int i=0; i<num.length; i++){
            for(int j=i+1; j<num.length; j++){
                for(int k=j+1; k<num.length; k++){
                    ArrayList<Integer> lst = new ArrayList<>() ;
                    lst.add(num[i]) ;
                    lst.add(num[j]) ;
                    lst.add(num[k]) ;
                    if(num[i] + num[j] + num[k] == 0 && !list.contains(lst)){
                        list.add(lst) ;
                    }
                }
            }
        }
         return list ;
    }
}

Idea 2: The double-pointer method can reduce the time complexity to O(n2), but the pointers are all on the right, and the test cases are bloody.

import java.util.*;
public class Solution {
    public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
        ArrayList<ArrayList<Integer>> list = new ArrayList<>() ;
        Arrays.sort(num) ;
        //使用双指针,固定中间的,左右两侧移动,可以降低时间复杂度
        if(num.length < 3){
            return list ;
        }
        
        for(int i=0; i<num.length; i++){
           if(num[i] > 0){
            return list ;
           }
            int left = i+1, right = num.length - 1 ;
            
            while(left > i && i < right && left < right){
             ArrayList<Integer> lst = new ArrayList<>() ;
            if(num[left] + num[right] + num[i] == 0){
                lst.add(num[i]) ;
                lst.add(num[left]) ;
                lst.add(num[right]) ;
                if(!list.contains(lst)){
                   list.add(lst) ;
                }
                left ++ ;
                right -- ;
            }else if(num[left] + num[right] + num[i] > 0){
                right -- ;
            }else{
                left ++ ;
            } 
            }
        }
        return list ;
    }
}

Guess you like

Origin blog.csdn.net/nuist_NJUPT/article/details/130756802
Recommended