Data Structure and Algorithm - Day 03

  • Stack

    extends class Vector

    Stack<T> stack = new Stack<>();
    

    API: empty() peek() pop() push(E item) search(Object o)

    Recommended in practice:

    Deque<T> stack = new ArrayDeque<T>();
    
  • Queue

    not a class, is a interface | has many class

    API:

  • Deque

    announce:/dek/ | Double-End Queue

    not a class, is a interface | has many class

    API:

  • Priority Queue

    implements Collection and Queue

    【Time complex】Insert: O(1), Get: O(log n)

    backend:heap / BST - binary search tree / treap

    API:

  • Summary of common data structure operations

  • 26. Remove Duplicates from Sorted Array

    Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.

    Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

    Clarification:

    Confused why the returned value is an integer but your answer is an array?

    Note that the input array is passed in by reference, which means a modification to the input array will be known to the caller as well.

    Internally you can think of this:

    // nums is passed in by reference. (i.e., without making a copy)
    int len = removeDuplicates(nums);
    
    // any modification to nums in your function would be known by the caller.
    // using the length returned by your function, it prints the first len elements.
    for (int i = 0; i < len; i++) {
        print(nums[i]);
    }
    

    Example 1:

    Input: nums = [1,1,2]
    Output: 2, nums = [1,2]
    Explanation: Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the returned length.
    

    Example 2:

    Input: nums = [0,0,1,1,1,2,2,3,3,4]
    Output: 5, nums = [0,1,2,3,4]
    Explanation: Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively. It doesn't matter what values are set beyond the returned length.
    

    Constraints:

    • 0 <= nums.length <= 3 * 104
    • -104 <= nums[i] <= 104
    • nums is sorted in ascending order.
    class Solution {
        public int removeDuplicates(int[] nums) {
            int i = 0;
            for (int j = 1; j < nums.length; j++) {
                if (nums[i] != nums[j]) {
                    nums[++i] = nums[j];
                }
            }
            return i + 1;
        }
    }
    
  • 189. Rotate Array

    Given an array, rotate the array to the right by k steps, where k is non-negative.

    Follow up:

    • Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
    • Could you do it in-place with O(1) extra space?

    Example 1:

    Input: nums = [1,2,3,4,5,6,7], k = 3
    Output: [5,6,7,1,2,3,4]
    Explanation:
    rotate 1 steps to the right: [7,1,2,3,4,5,6]
    rotate 2 steps to the right: [6,7,1,2,3,4,5]
    rotate 3 steps to the right: [5,6,7,1,2,3,4]
    

    Example 2:

    Input: nums = [-1,-100,3,99], k = 2
    Output: [3,99,-1,-100]
    Explanation: 
    rotate 1 steps to the right: [99,-1,-100,3]
    rotate 2 steps to the right: [3,99,-1,-100]
    

    Constraints:

    • 1 <= nums.length <= 2 * 104
    • -231 <= nums[i] <= 231 - 1
    • 0 <= k <= 105
    class Solution {
        public void rotate(int[] nums, int k) {
            int len = nums.length;
            k = k % len;
            reverse(nums, 0, len - 1);
            reverse(nums, 0, k - 1);
            reverse(nums, k, len - 1);
        }
    
        private void reverse(int[] nums, int i, int j) {
            while (i < j) {
                int t = nums[i];
                nums[i] = nums[j];
                nums[j] = t;
                i++;
                j--;
            }
        }
    }
    
  • 88. Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

    The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has a size equal to m + n such that it has enough space to hold additional elements from nums2.

    Example 1:

    Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
    Output: [1,2,2,3,5,6]
    

    Example 2:

    Input: nums1 = [1], m = 1, nums2 = [], n = 0
    Output: [1]
    

    Constraints:

    • nums1.length == m + n
    • nums2.length == n
    • 0 <= m, n <= 200
    • 1 <= m + n <= 200
    • -109 <= nums1[i], nums2[i] <= 109
    class Solution {
        public void merge(int[] nums1, int m, int[] nums2, int n) {
            int i = m - 1, j = n - 1;
            for (int k = m + n - 1; i >= 0 && j >= 0; k--) {
                nums1[k] = nums1[i] > nums2[j] ? nums1[i--] : nums2[j--];
            }
            while (j >= 0) nums1[j] = nums2[j--];
        }
    }
    
  • 66. Plus One

    Given a non-empty array of decimal digits representing a non-negative integer, increment one to the integer.

    The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.

    You may assume the integer does not contain any leading zero, except the number 0 itself.

    Example 1:

    Input: digits = [1,2,3]
    Output: [1,2,4]
    Explanation: The array represents the integer 123.
    

    Example 2:

    Input: digits = [4,3,2,1]
    Output: [4,3,2,2]
    Explanation: The array represents the integer 4321.
    

    Example 3:

    Input: digits = [0]
    Output: [1]
    

    Constraints:

    • 1 <= digits.length <= 100
    • 0 <= digits[i] <= 9
    class Solution {
        public int[] plusOne(int[] digits) {
            int len = digits.length;
            int carry = 1;
            for (int i = len - 1; i >= 0 && carry > 0; i--) {
                int num = digits[i] + 1;
                digits[i] = num % 10;
                carry = num / 10;
            }
            if (carry == 0) return digits;
            int[] res = new int[len + 1];
            res[0] = 1;
            return res;
        }
    }
    
  • 21. Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists.

    Example 1:

    img

    Input: l1 = [1,2,4], l2 = [1,3,4]
    Output: [1,1,2,3,4,4]
    

    Example 2:

    Input: l1 = [], l2 = []
    Output: []
    

    Example 3:

    Input: l1 = [], l2 = [0]
    Output: [0]
    

    Constraints:

    • The number of nodes in both lists is in the range [0, 50].
    • -100 <= Node.val <= 100
    • Both l1 and l2 are sorted in non-decreasing order.
    class Solution {
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            ListNode res = new ListNode();
            ListNode cur = res;
            while (l1 != null && l2 != null) {
                if (l1.val < l2.val) {
                    cur.next = l1;
                    l1 = l1.next;
                } else {
                    cur.next = l2;
                    l2 = l2.next;
                }
                cur = cur.next;
            }
            if (l1 != null) cur.next = l1;
            if (l2 != null) cur.next = l2;
            return res.next;
        }
    }
    
  • 25. Reverse Nodes in k-Group

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

    k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is.

    Follow up:

    • Could you solve the problem in O(1) extra memory space?
    • You may not alter the values in the list's nodes, only nodes itself may be changed.

    Example 1:

    img

    Input: head = [1,2,3,4,5], k = 2
    Output: [2,1,4,3,5]
    

    Example 2:

    img

    Input: head = [1,2,3,4,5], k = 3
    Output: [3,2,1,4,5]
    

    Example 3:

    Input: head = [1,2,3,4,5], k = 1
    Output: [1,2,3,4,5]
    

    Example 4:

    Input: head = [1], k = 1
    Output: [1]
    

    Constraints:

    • The number of nodes in the list is in the range sz.
    • 1 <= sz <= 5000
    • 0 <= Node.val <= 1000
    • 1 <= k <= sz
    class Solution {
        public ListNode reverseKGroup(ListNode head, int k) {
            if (head == null || k == 1) return head;
            ListNode res = new ListNode();
            ListNode cur = res;
            Deque<ListNode> deque = new LinkedList<>();
            while (head != null) {
                for (int i = 0; i < k && head != null; i++) {
                    deque.addLast(head);
                    head = head.next;
                }
                if (deque.size() != k) {
                    cur.next = deque.pollFirst();
                    break;
                }
                while (!deque.isEmpty()) {
                    ListNode node = deque.pollLast();
                    node.next = null;
                    cur.next = node;
                    cur = cur.next;
                }
            }
            return res.next;
        }
    }
    
posted @ 2021-03-12 15:06  鹏懿如斯  阅读(31)  评论(0编辑  收藏  举报