Algorithmic Liver Astoria

1. How to solve algorithm problems efficiently?

Five-step brushing method:

The first time: Try to solve the problem, but you can't solve it by reciting other people's solutions

The second time: Quickly write the problem solution in your mind on Leetcode, try to exceed 80% of the people in Leetcode

Third pass: reapply after 24 hours

The fourth time: Re-do the questions after a week

The fifth time: a week or a month before the interview, re-flash and hand-write the code (simulating the real environment)

2. 1-002-algorithm time complexity analysis

common time complexity

O(1) constant order

O(logn) logarithmic order

O(n) linear order

O(nlogn) linear log order

O(n^2) square order

O(n^3) cubic order

O(2^n) exponential order

O(n!) factorial order

O(n^n) 

The time complexity increases from top to bottom. In general, the time complexity is not necessary when it reaches the cubic order, and it is necessary to consider whether it is necessary when it reaches the square order.

3. What is recursion

1. A problem can be decomposed into the same several sub-problems

2. There is a termination condition

4.1-004-(LeetCode-70) Stair Climbing 

1. The first pass

import java.util.HashMap;
import java.util.Map;

class Solution {
    private Map<Integer, Integer> map = new HashMap<Integer,Integer>();
    //递归实现
    public int climbStairs(int n) {
        if(n==1) return 1;
        if(n==2) return 2;
        //如果map中存在值,返回,不存在,重新计算
        if(null != map.get(n)){
            return map.get(n);
        }else{
            //放入map中
            int sum = climbStairs(n-1)+climbStairs(n-2);
            map.put(n,sum);
            return sum;
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution().climbStairs(4));
    }
}

5. Fibonacci numbers

import java.util.HashMap;
import java.util.Map;

/**
 * 509. 斐波那契数
 * 使用递归+hashMap解决问题
 */
class Solution {
    Map<Integer,Integer> map = new HashMap<Integer,Integer>();
    public int fib(int n) {
        if(n == 0 ){ return 0;}
        if(n == 1 ){ return 1;}
        if(null != map.get(n)){
            return map.get(n);
        }else{
            int sum = fib(n-1) + fib(n-2);
            map.put(n,sum);
            return sum;
        }
    }
}

1. The sum of two numbers

import java.util.HashMap;
import java.util.Map;

/**
 * 1. 两数之和
 * 第一种方法:暴力穷举 时间复杂度O(N^2)
 * 第二种方法:遍历+hashMap 时间复杂度O(N)
 */
class Solution {
    public int[] twoSum(int[] nums, int target) {
        //暴力穷举
//        for(int i=0;i<nums.length;i++){
//            for(int j=0;j<nums.length;j++){
//                if(nums[i]+nums[j] == target && i!=j){
//                    return new int[]{i,j};
//                }
//            }
//        }

        //遍历+hashMap 时间复杂度O(N)
        Map<Integer,Integer> map = new HashMap<Integer,Integer>();
        for(int i=0;i<nums.length;i++){
            int value = target-nums[i];
            if(map.containsKey(value)){
                return new int[]{value,i};
            }else{
                map.put(nums[i],i);
            }
        }
        return new int[2];

//        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
//        for (int i =0 ;i<nums.length; i++){
//            if(map.containsKey(target-nums[i])){
//                return new int[]{map.get(target-nums[i]),i};
//            }
//            map.put(nums[i],i);
//        }
//
//        return new int[0];
    }

    public static void main(String[] args) {
        System.out.println(new Solution().twoSum(new int[]{3,2,4},6));
    }
}

88. Merge two sorted arrays

There are three solutions:

1. The second array is sorted by the first array, and the first array is sorted

2. Introduce the temp array, compare array 1 and array 2 into the temp array, and finally put the temp array into nums1 (todo)

3. Reverse order puts array comparison into nums1 (todo)

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
            for(int i = 0;i < nums2.length;i++){
                nums1[m+i] = nums2[i];
            }
            Arrays.sort(nums1);
    }
}

 283. Moving Zero

1. Use double pointers to solve, define i, j, and move the elements that are not equal to 0 to the front. There is a sorting problem visually.

2. Change all elements after j to 0

class Solution {
    public void moveZeroes(int[] nums) {
        //做一个非空校验
        if(null == nums || nums.length == 0) return;
        //定义一个双指针解法
        int j = 0;
        //1.循环遍历把值赋值给j
        for(int i=0;i < nums.length;i++){
            if(nums[i]!=0){
                nums[j++] = nums[i];
            }
        }

        //2.遍历完成后从j开始把后面的数据改为0
        for(int n=j;n< nums.length;n++){
            nums[n] = 0;
        }
    }
}

448. Find All Missing Numbers in an Array 

I don't quite understand this mathematical formula, so I can only remember it first, and write it silently tomorrow

public List<Integer> findDisappearedNumbers(int[] nums) {
        //定义一个返回的数组
        List<Integer> list = new ArrayList<>();

        //1.遍历(数字-1)求余+n,排除出来重复的数字
        int n = nums.length;
        for(int num : nums){
            int x = (num-1) % n;
            nums[x] += n;
        }

        //2.遍历,将重复的数字放入集合
        for(int i =0 ;i < n;i++){
            if(nums[i] <= n){
                list.add(i+1);
            }
        }
        return list;
    }

Guess you like

Origin blog.csdn.net/qq_21575929/article/details/124759539