LeetCode plan over and over again (1)

Hello everyone, I'm 方圆
over and over again, only hand-cooked Seoul


Hash related

1. the sum of two numbers

Insert picture description here

class Solution {
    
    
    public int[] twoSum(int[] nums, int target) {
    
    
        Map<Integer, Integer> map = new HashMap<>();
        
        for (int i = 0; i < nums.length; i++) {
    
    
            int complement = target - nums[i];
            if (map.containsKey(complement)) {
    
    
                return new int[] {
    
     map.get(complement), i };
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }

}

Linked list operation

2. Add two numbers

Insert picture description here

class Solution {
    
    
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    
    
        ListNode pre = new ListNode(0);
        //这个当前节点很妙
        ListNode cur = pre;
        int add = 0;

        while(l1 != null || l2 != null){
    
    
            int x = l1 != null ? l1.val : 0;
            int y = l2 != null ? l2.val : 0;
            int sum = x + y + add;

            int temp = sum % 10;
            add = sum / 10;

            cur.next = new ListNode(temp);
            cur = cur.next;

            //注意这里判断不是l1.next != null
            if(l1 != null) l1 = l1.next;
            if(l2 != null) l2 = l2.next;
        }
        if(add == 1)
            cur.next = new ListNode(1);

        return pre.next;
    }
}

19. Delete the Nth node from the bottom of the linked list

Insert picture description here

class Solution {
    
    
    public ListNode removeNthFromEnd(ListNode head, int n) {
    
    
        ListNode pre = new ListNode(0);
        pre.next = head;
        ListNode temp = pre;
        ListNode cur = pre;

        while(n != 0){
    
    
            temp = temp.next;
            n--;
        }
        while(temp.next != null){
    
    
            temp = temp.next;
            cur = cur.next;
        }
        cur.next = cur.next.next;

        return pre.next;
    }
}

61. Rotating Linked List

Insert picture description here

class Solution {
    
    
    public ListNode rotateRight(ListNode head, int k) {
    
    
        if(head == null || head.next == null)
            return head;
        
        ListNode old_tail = head;
        int length = 1;
        while(old_tail.next != null){
    
    
            length++;
            old_tail = old_tail.next;
        }
        //首尾相连
        old_tail.next = head;

        //找到新的尾巴需要走几步,比如说,实例1中
        //从尾巴开始找是走k步就行,但是我们是从头部开始找
        //就要用总长度减去k的值,还要减1是因为我们开始就在头部了
        int step = length - k%length - 1;
        ListNode new_tail = head;
        for(int i = 0;i < step;i++)
            new_tail = new_tail.next;
        ListNode res = new_tail.next;
        new_tail.next = null;

        return res;
    }
}

138. Copy linked list with random pointer

Insert picture description here

//做过第三遍了还是没想起来这个!
class Solution {
    
    
    Map<Node,Node> map = new HashMap<>();
    public Node copyRandomList(Node head) {
    
    
        if(head == null)
            return null;
        
        if(map.containsKey(head))
            return map.get(head);
        
        Node node = new Node(head.val);
        map.put(head,node);

        node.next = copyRandomList(head.next);
        node.random = copyRandomList(head.random);

        return node;
    }
}

206. Flip Linked List

Insert picture description here

//第三遍了。。。
class Solution {
    
    
    public ListNode reverseList(ListNode head) {
    
    
        if(head == null) return head;

        ListNode pre = new ListNode(0);
        ListNode cur = head;
        ListNode temp = null;

        while(cur != null){
    
    
            temp = cur.next;
            cur.next = pre.next;
            pre.next = cur;
            cur = temp;
        }

        return pre.next;
    }
}

Double pointer traversal (sliding window)

3. The longest substring without repeated characters

Insert picture description here

class Solution {
    
    
    public int lengthOfLongestSubstring(String s) {
    
    
        int length = s.length();
        int res = 0;
        HashMap<Character,Integer> map = new HashMap<>();

        if(length == 0) return 0;

        for(int start = 0,end = 0;end < length;end++){
    
    
            if(map.containsKey(s.charAt(end)))
                start = Math.max(start,map.get(s.charAt(end)) + 1);//abcab
            map.put(s.charAt(end),end);
            res = Math.max(res,end - start + 1);
        }

        return res;
    }
}

11. The container with the most water

Insert picture description here

class Solution {
    
    
    public int maxArea(int[] height) {
    
    
        int left = 0,right = height.length - 1;
        int res = 0;

        while(left <= right){
    
    
            res = Math.max(res,
            Math.min(height[left],height[right]) * (right - left));

            if(height[left] < height[right]) left++;
            else right--;
        }

        return res;
    }
}

15. Sansanowa

Insert picture description here

class Solution {
    
    
    public List<List<Integer>> threeSum(int[] nums) {
    
    
        List<List<Integer>> res = new ArrayList<>();
        int length = nums.length;

        if(nums == null || length < 3) return res;

        Arrays.sort(nums);
        for(int i = 0;i < length;i++){
    
    
            if(nums[i] > 0) break;
            if(i > 0 && nums[i] == nums[i - 1]) continue;

            int L = i + 1;
            int R = length - 1;
            while(L < R){
    
    
                int sum = nums[i] + nums[L] + nums[R];
                if(sum == 0){
    
    
                    res.add(Arrays.asList(nums[i],nums[L],nums[R]));
                    while(L < R && nums[L] == nums[L + 1]) L++;
                    while(L < R && nums[R] == nums[R - 1]) R--;
                    L++;
                    R--;
                }else if(sum > 0) R--;
                else L++;
            }
        }

        return res;
    }
}

16. The nearest sum of three numbers

Insert picture description here

class Solution {
    
    
    public int threeSumClosest(int[] nums, int target) {
    
    
        int res = nums[0] + nums[1] + nums[2];
        int length = nums.length;

        Arrays.sort(nums);
        for(int i = 0;i < length;i++){
    
    
            int L = i + 1;
            int R = length - 1;

            while(L < R){
    
    
                int sum = nums[i] + nums[L] + nums[R];
                if(Math.abs(sum - target) < Math.abs(res - target))
                    res = sum;
                
                if(sum > target) R--;
                else if(sum < target) L++;
                else return sum;
            }
        }

        return res;
    }
}

26. Remove duplicates in sorted array

Insert picture description here

class Solution {
    
    
    public int removeDuplicates(int[] nums) {
    
    
        if(nums == null || nums.length == 0) 
            return 0;

        int left = 0;
        int right = 1;
        while(right < nums.length){
    
    
            if(nums[left] != nums[right]){
    
    
                //把不重复的东西存储到左边儿
                nums[left + 1] = nums[right];
                left++; 
            }
            right++;
        }

        return left + 1;
    }
}

121. The best time to buy and sell stocks

Insert picture description here

class Solution {
    
    
    public int maxProfit(int[] prices) {
    
    
        int minPrice = Integer.MAX_VALUE;
        int res = 0;

        for(int i = 0;i < prices.length;i++){
    
    
            minPrice = Math.min(minPrice,prices[i]);
            res = Math.max(res,prices[i] - minPrice);
        }

        return res;
    }
}

209. The smallest subarray

Insert picture description here

class Solution {
    
    
    public int minSubArrayLen(int s, int[] nums) {
    
    
        if(nums.length == 0) return 0;

        int left = 0,right = 0;
        int sum = 0;
        int res = Integer.MAX_VALUE;

        while(right < nums.length){
    
    
            sum += nums[right];
            while(sum >= s){
    
    
                res = Math.min(res,right - left + 1);
                sum -= nums[left++];
            }
            right++;
        }

        return res == Integer.MAX_VALUE ? 0 : res;
    }
}

Cool!

Guess you like

Origin blog.csdn.net/qq_46225886/article/details/107455697