Wonderful algorithm [11] LeetCode- proprietary algorithm interview questions summary

  This is a special training program LeetCode above page address: https://leetcode-cn.com/explore/interview/card/top-interview-quesitons-in-2018/262/summery/

  Overall, more systematic and comprehensive. In addressing these issues, I always first try to use their own methods coding again, after a look at other more ingenious solutions to learn about.

0, program title warm-up

  0.1 The number appears only once

  Given a non-empty array of integers, in addition to an element appears only once, the rest of each element appears twice. To find out that only appears once in the elements.

Package com.cnblogs.mufasa.QA1_makeAhotDOU; 

Import org.junit.Test;
 Import java.util.Arrays; 

public  class Solution1 { 

    // speed is okay, the time complexity is approximately O (nlogn + n / 2) , relative performance to be worse 
    public  static  int singleNumber1 ( int [] the nums) { 
        Arrays.sort (the nums); 
        int len =-nums.length. 1 ;
         for ( int I = 0; I <len; = I + 2 ) {
             IF (the nums [ ! I] = the nums [I +. 1 ]) {
                 return the nums [I]; 
            } 
        } 
        return the nums [len]; 
    }

    // *** *** optimized solution, the time complexity is O (n-) 
    public  static  int singleNumber ( int [] the nums) {
         int TEMP = the nums [0 ];
         for ( int I =. 1; I <the nums .length; I ++ ) { 
            TEMP ^ = the nums [I]; 
        } 
        return TEMP; 
    } 

    @Test 
    public  void Test () {
 //         the nums int = new new int [] [] {1,1,2,2,6,7 , 7,6}; 
        int [] = the nums new new  int [] {4,1,2,1,2 };  
        System.out.println (singleNumber (the nums));
        System.out.println (singleNumber1 (the nums)); 
    } 
}

 

  0.2 seeking the mode

  Given a size  array, find all the number of them. The mode is greater than the number of times appear in the array  ⌊ n/2 ⌋ elements.

  You can assume that the array is non-empty, and there is always a given array number of the congregation.

Package Penalty for com.cnblogs.mufasa.QA1_makeAhotDOU; 

Import org.junit.Test; 

Import java.util.Arrays;
 Import java.util.Map;
 Import java.util.TreeMap; 

public  class Solution2 { 

    // perfect solution, time complexity can be reduced to O (NlogN) 
    public  int majorityElement1 ( int [] the nums) { 
        Arrays.sort (the nums); 
        return the nums [nums.length / 2 ]; 
    } 

    // use TreeMap to sort, size of the update frequency of occurrence 
    public  int majorityElement ( int [] the nums) { 
        the TreeMap <Integer, Integer> = TreeMap new new TreeMap<>();
        for(int i=0;i<nums.length;i++){
            if(treeMap.get(nums[i])!=null){
                treeMap.put(nums[i],treeMap.get(nums[i])+1);
            }else {
                treeMap.put(nums[i],1);
            }
        }
        int loc=-1,max=0;
        for(Map.Entry<Integer,Integer> kv:treeMap.entrySet()){
            if(kv.getValue()>max){
                loc=kv.getKey();
                max=kv.getValue();
                if(max>nums.length/2){
                    break;
                }
            }
        }
        return loc;
    }

    @Test
    public void test() {
        int[] nums=new int[]{2,2,1,1,1,2,2};
        System.out.println(majorityElement1(nums));
        System.out.println(majorityElement(nums));
    }
}

 

  Search two-dimensional matrix II 0.3

 

  0.4 merge two ordered arrays

 

  0.5 Egg Drop

  

1, string

  1.1 verify palindrome ['ve written, relatively simple]

  Given a string, verify that it is a palindrome string, consider only the alphabetic and numeric characters, can ignore letter case.

 

 

2, array

3, heap, stack, queue

4, the list

5, hash and map

6, tree

7, sorting and retrieval

8, dynamic programming

9, graph theory

10, Math &-bit computing

11, the analog face questions

 

Guess you like

Origin www.cnblogs.com/Mufasa/p/11563718.html