Data Structure and Algorithm - Day 01

  • Master Theorem

  • ArrayList

    When insert or delete an element, ArrayList will create a new Array and execute a System.arraycopy. Doing those constantly will reduce efficiency.

  • SkipList

    space for time | increase dimension

    multi-dimension index | log2n-dimension index

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

  • 283. Move Zeroes

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

    Example:

    Input: [0,1,0,3,12]
    Output: [1,3,12,0,0]
    

    Note:

    1. You must do this in-place without making a copy of the array.
    2. Minimize the total number of operations.
    class Solution {
        public void moveZeroes(int[] nums) {
            int i = 0;
            for (int j = 0; j < nums.length; j++) {
                if (nums[j] != 0) {
                    swap(nums, i, j);
                    i++;
                }
            }
        }
    
        private void swap(int[] nums, int i, int j) {
            int t = nums[i];
            nums[i] = nums[j];
            nums[j] = t;
        }
    }
    
posted @ 2021-03-10 19:51  鹏懿如斯  阅读(37)  评论(0编辑  收藏  举报