Li Kou Day20 (happy New Year's Eve)

344,Reverse string

Write a function that inverts the input string. The input string is given as a character array s.

Do not allocate additional space to another array. You must modify the input array in place and use the additional space of O(1) to solve this problem.

 

class Solution {
    public void reverseString(char[] s) {  
        //I was blind and thought I was going to make all the characters in the character array lowercase
        int j=s.length-1;
        char temp;
        for(int i=0;i<s.length/2;i++){
            //Array exchange element
            temp=s[i];
            s[i]=s[j];
            s[j]=temp;
            j--;
        }
    }
}

349,Intersection of two arrays

Given two arrays , nums1 , and , nums2, return , their intersection. Each element in the output result must be {unique. We can {ignore the order of output results.

 // int[] arr = set.stream().mapToInt(Integer::intValue).toArray(); //set directly into array

// int[] arr = list.stream().mapToInt(Integer::intValue).toArray(); //list directly into array

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Map<Integer,Integer>map=new HashMap<>();//You can use list without map
        Set<Integer>set=new HashSet<>();
        for(int i=0;i<nums1.length;i++){//Get the value of nums1
            map.put(nums1[i],i);
        }

        for(int i:nums2){
            if(map.containsKey(i)){//Judge whether the map has the value of num
                set.add(i);//duplicate removal
            }
        }

// int[] arr = set.stream().mapToInt(Integer::intValue).toArray(); //set directly into array
// int[] arr = list.stream().mapToInt(Integer::intValue).toArray(); //list directly into array
        
        int []arr=new int[set.size()];
        int j=0;
        for(int i:set){
            arr[j++]=i;
        }
        return arr;
    }
}

355,Intersection of two arrays II

Here are two integer arrays, nums1 and nums2. Please return the intersection of the two arrays in the form of array. The number of occurrences of each element in the returned result should be consistent with the number of occurrences of elements in both arrays (if the number of occurrences is inconsistent, consider taking the smaller value). The order of output results can be ignored.

be careful:

1. remove has two overloaded methods. If the bracket is of type int, it returns the value corresponding to the index. If the bracket is not of type int, it deletes the value.
2. It happens that our value is of type int. if we directly return i, an error will be reported by default to return the value corresponding to index i, so we need to convert int into Integer

Method 1:

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
       List<Integer>list1=new ArrayList<>();
       List<Integer>list2=new ArrayList<>();
       for(int i:nums1){
           list1.add(i);
       }

        for(int i:nums2){
            if(list1.contains(i)){
                list2.add(i);
                list1.remove(Integer.valueOf(i));
                //remove has two overloaded methods. If the type in parentheses is int, it returns the value corresponding to the index
                //If the bracket is not of type int, the value is deleted.
                //It happens that our value is of type int. if we directly return i, an error will be reported by default to return the value corresponding to index i,                
                //So we need to convert int i to Integer 
            }
        }

       int[]arr=new int[list2.size()];
       int j=0;
       for(int i:list2){
           arr[j++]=i;
       }
       return arr;
    }
}

Method 2:

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
      Map<Integer, Integer> map = new HashMap<>();
        // Put the value and frequency of nums1 into the map
        for (int num : nums1) {
            Integer count = map.get(num);
            if (count == null) {
                map.put(num, 1);
            } else {
                map.put(num, ++count);
            }
        }
        List<Integer> list = new ArrayList<>();
        for (int num : nums2) {
            // Gets the frequency of occurrence of this value in the map
            Integer count = map.get(num);
            if (count != null && count != 0) {
                list.add(num);
                // Note that after each matching, the frequency of this value needs to be reduced by 1 (the frequency of nums1 and nums2 matching values should be the same)
                map.put(num, --count);
            }
        }
        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }
}

Note:

Map is a set of key value pairs. Each element in the map set contains a key object and a value object. The key object cannot be repeated, while the value object can be repeated.

If the key of the Map is repeated, the key that added the duplicate last time will prevail.

As shown in the figure: four elements with key 1 are added. Because the key is repeated, the last element with key 1 will be taken, that is: < 1,3 >

System.out.println(map.remove(2,5));// Judge whether an element in the map set meets the requirements of < key = 2, value = 5 >. If satisfied, return true and delete the element

Keywords: Algorithm leetcode

Added by toibs on Tue, 01 Feb 2022 08:13:59 +0200