Data Structure and Algorithm

  • 11. Container With Most Water

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.

    Notice that you may not slant the container.

    Example 1:

    img

    Input: height = [1,8,6,2,5,4,8,3,7]
    Output: 49
    Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
    

    Example 2:

    Input: height = [1,1]
    Output: 1
    

    Example 3:

    Input: height = [4,3,2,1,4]
    Output: 16
    

    Example 4:

    Input: height = [1,2,1]
    Output: 2
    

    Constraints:

    • n == height.length
    • 2 <= n <= 105
    • 0 <= height[i] <= 104
    class Solution {
        public int maxArea(int[] height) {
            int res = 0;
            for (int i = 0, j = height.length - 1; i < j; ) {
                int minHeight = height[i] < height[j] ? height[i++] : height[j--];
                res = Math.max(res, (j - i + 1) * minHeight); //plus 1
            }
            return res;
        }
    }
    
  • 70. Climbing Stairs

    You are climbing a staircase. It takes n steps to reach the top.

    Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

    Example 1:

    Input: n = 2
    Output: 2
    Explanation: There are two ways to climb to the top.
    1. 1 step + 1 step
    2. 2 steps
    

    Example 2:

    Input: n = 3
    Output: 3
    Explanation: There are three ways to climb to the top.
    1. 1 step + 1 step + 1 step
    2. 1 step + 2 steps
    3. 2 steps + 1 step
    

    Constraints:

    • 1 <= n <= 45
    class Solution {
        public int climbStairs(int n) {
            if (n < 4) return n;
            int a = 1, b = 2, c = 3;
            while (n-- > 3) {
                a = b;
                b = c;
                c = a + b;
            }
            return c;
        }
    }
    
  • 15. 3Sum

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

    Notice that the solution set must not contain duplicate triplets.

    Example 1:

    Input: nums = [-1,0,1,2,-1,-4]
    Output: [[-1,-1,2],[-1,0,1]]
    

    Example 2:

    Input: nums = []
    Output: []
    

    Example 3:

    Input: nums = [0]
    Output: []
    

    Constraints:

    • 0 <= nums.length <= 3000
    • -105 <= nums[i] <= 105
    class Solution {
        public List<List<Integer>> threeSum(int[] nums) {
            List<List<Integer>> res = new ArrayList<>();
            Arrays.sort(nums);
            for (int i = 0; i < nums.length - 2 && nums[i] <= 0; i++) {
                if (i > 0 && nums[i] == nums[i - 1]) continue;
                for (int j = i + 1, k = nums.length - 1; j < k; ) {
                    int sum = nums[j] + nums[k];
                    if (j > i + 1 && nums[j] == nums[j - 1] || sum < -nums[i]) {
                        j++;
                    } else if (k < nums.length - 1 && nums[k] == nums[k + 1] || sum > -nums[i]) {
                        k--;
                    } else {
                        res.add(Arrays.asList(nums[i], nums[j], nums[k]));
                        j++;
                        k--;
                    }
                }
            }
            return res;
        }
    }
    
  • 141. Linked List Cycle

    Given head, the head of a linked list, determine if the linked list has a cycle in it.

    There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.

    Return true if there is a cycle in the linked list. Otherwise, return false.

    Example 1:

    img

    Input: head = [3,2,0,-4], pos = 1
    Output: true
    Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
    

    Example 2:

    img

    Input: head = [1,2], pos = 0
    Output: true
    Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
    

    Example 3:

    img

    Input: head = [1], pos = -1
    Output: false
    Explanation: There is no cycle in the linked list.
    

    Constraints:

    • The number of the nodes in the list is in the range [0, 104].
    • -105 <= Node.val <= 105
    • pos is -1 or a valid index in the linked-list.
    public class Solution {
        public boolean hasCycle(ListNode head) {
            if (head == null || head.next == null) return false; 
            ListNode fast = head.next, slow = head;
            while (fast != null && fast.next != null) {
                if (fast == slow) return true;
                fast = fast.next.next;
                slow = slow.next;
            }
            return false;
        }
    }
    
  • 142. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

    There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.

    Notice that you should not modify the linked list.

    Example 1:

    img

    Input: head = [3,2,0,-4], pos = 1
    Output: tail connects to node index 1
    Explanation: There is a cycle in the linked list, where tail connects to the second node.
    

    Example 2:

    img

    Input: head = [1,2], pos = 0
    Output: tail connects to node index 0
    Explanation: There is a cycle in the linked list, where tail connects to the first node.
    

    Example 3:

    img

    Input: head = [1], pos = -1
    Output: no cycle
    Explanation: There is no cycle in the linked list.
    

    Constraints:

    • The number of the nodes in the list is in the range [0, 104].
    • -105 <= Node.val <= 105
    • pos is -1 or a valid index in the linked-list.

    Follow up: Can you solve it using O(1) (i.e. constant) memory?

    public class Solution {
        public ListNode detectCycle(ListNode head) {
            if (head == null || head.next == null) return null;
            ListNode fast = head, slow = head;
            while (fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
                if (fast == slow) {
                    fast = head;
                    while (fast != slow) {
                        fast = fast.next;
                        slow = slow.next;
                    }
                    return fast;
                }
            }
            return null;
        }
    }
    
  • 206. Reverse Linked List

    Given the head of a singly linked list, reverse the list, and return the reversed list.

    Example 1:

    img

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

    Example 2:

    img

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

    Example 3:

    Input: head = []
    Output: []
    

    Constraints:

    • The number of nodes in the list is the range [0, 5000].
    • -5000 <= Node.val <= 5000
    class Solution {
        public ListNode reverseList(ListNode head) {
            if (head == null || head.next == null) return head;
            ListNode res = new ListNode();
            while (head != null) {
                ListNode cur = head;
                head = head.next;
                cur.next = res.next;
                res.next = cur;
            }
            return res.next;
        }
    }
    
  • 24. Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head.

    Example 1:

    img

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

    Example 2:

    Input: head = []
    Output: []
    

    Example 3:

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

    Constraints:

    • The number of nodes in the list is in the range [0, 100].
    • 0 <= Node.val <= 100

    Follow up: Can you solve the problem without modifying the values in the list's nodes? (i.e., Only nodes themselves may be changed.)

    class Solution {
        public ListNode swapPairs(ListNode head) {
            if (head == null || head.next == null) return head;
            ListNode res = new ListNode();
            ListNode cur = res;
            while (head != null && head.next != null) {
                ListNode a = head, b = head.next;
                a.next = b.next;
                b.next = a;
                cur.next = b;
                cur = a;
                head = cur.next;
            }
            return res.next;
        }
    }
    
原文地址:https://www.cnblogs.com/peng8098/p/algorithm2.html