• 【力扣hot100】day2


    11. 盛最多水的容器【中】

    11.盛最多水的容器
    一:双指针

    • 时间复杂度 O(N);
    • 空间复杂度 O(1): 变量 i ,j,S,temp使用常数额外空间。
    //执行用时: 3 ms
    //内存消耗: 50.9 MB
    // S = (j - i) * h[j] 或者 S = (j - i) * h[i]
    class Solution {
        public int maxArea(int[] height) {
            int left = 0, right = height.length - 1;
            int S = 0, temp = 0;;
            while(left < right) {
                if(height[left] < height[right]) {
                    temp = height[left]*(right - left);
                    left++;
                } else {
                    temp = height[right]*(right - left);
                    right--;
                }
                S = Math.max(S, temp);
            }
            return S;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    //执行用时: 3 ms
    //内存消耗: 51.5 MB
    class Solution {
        public int maxArea(int[] height) {
            int l = 0, r = height.length - 1;
            int S = 0;
            while(l <= r) {
                if(height[l] < height[r]) {
                    S = Math.max(S, (r - l) * height[l]);
                    l++;
                } else {
                    S = Math.max(S, (r - l) * height[r]);
                    r--;
                }
            }
            return S;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    15. 三数之和【中】

    15.三数之和
    方法一:双指针

    • 时间复杂度:O(n^2),n 为数组长度
    //执行用时: 19 ms
    //内存消耗: 45.6 MB
    class Solution {
        public List<List<Integer>> threeSum(int[] nums) {
            List<List<Integer>> list = new ArrayList<>();
            Arrays.sort(nums);
            for(int i = 0; i < nums.length - 1; i++) {
                int left = i + 1, right = nums.length - 1;
             // int sum = nums[i] + nums[left] + nums[right]; 写在这里的话就错了
                if(nums[i] > 0) {
                    break;
                }
                if(i > 0 && nums[i] == nums[i - 1]) {
                    continue; //去重
                }
    
                while(left < right) {
                    int sum = nums[i] + nums[left] + nums[right]; 
                    if(sum == 0) {
                        list.add(Arrays.asList(nums[i],nums[left],nums[right]));
                        while(left < right && nums[left] == nums[left + 1]) {
                            left++; //去重
                        }
                        while(left < right && nums[right] == nums[right - 1]) {
                            right--; //去重
                        }
                        left++;
                        right--;
                    }
                    else if(sum < 0) {
                        left++;
                    } else {
                        right--;
                    }
                }
            }
            return list;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    17. 电话号码的字母组合【中】

    17.电话号码的字母组合
    方法一:回溯

    //执行用时: 0 ms
    //内存消耗: 39.9 MB
    class Solution {
        List<String> result = new ArrayList<>();
        StringBuilder res = new StringBuilder();
        String[] ss = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        
        public List<String> letterCombinations(String digits) {
            if(digits.length() == 0) return result;
            backTracking(digits, 0);
            return result;
        }
    
        public void backTracking(String digits, int index) {
            if(index == digits.length()) {
                result.add(res.toString());
                return ;
            }
            String tmp = ss[digits.charAt(index) - '0'];
            for(int i = 0; i < tmp.length(); i++) { 
            //这里真的要注意,i < tmp.length(),而不是 i < digits.length()
                res.append(tmp.charAt(i));
                backTracking(digits, index + 1);
                res.deleteCharAt(res.length() - 1);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    19. 删除链表的倒数第N个结点【中】

    19.删除链表的倒数第N个结点
    方法一:遍历
    先统计总的结点数count,删除倒数第N个结点也就是删除第(count - n + 1)个结点

    //执行用时: 0 ms
    //内存消耗: 39.9 MB
    class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {
            ListNode dummy = new ListNode(0);
            ListNode pre = dummy;
            pre.next = head;
            ListNode cur = head;
            int count = 0;
            while(cur != null) {
                cur = cur.next;
                count++;
            }
            for(int i = 0; i < count - n; i++) {
                pre = pre.next;
            }
            pre.next = pre.next.next;
            return dummy.next;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    方法二:双指针

    //执行用时: 0 ms
    //内存消耗: 39.5 MB
    class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {    
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            ListNode fast = dummy,slow = dummy;
            while(n != 0) {
                fast = fast.next;
                n--;
            }
            while(fast.next != null) {
                fast = fast.next;
                slow = slow.next;
            }
            slow.next = slow.next.next;
            return dummy.next;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    20. 有效的括号【易】

    20.有效的括号
    方法一:辅助栈

    //执行用时: 1 ms
    //内存消耗: 39.2 MB
    class Solution {
        public boolean isValid(String s) {
            if(s.isEmpty()) 
                return true;
            Stack<Character> stack = new Stack<>();
            for(char c : s.toCharArray()){
                if(c == '('){
                    stack.push(')');
                }else if(c == '['){
                    stack.push(']');
                }else if(c == '{'){
                    stack.push('}');
                }else if(stack.isEmpty() || c != stack.pop()){
                    return false;
                }
            }
            return stack.isEmpty();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    [JavaWeb] Tomcat服务器的配置
    Docker-dockerfile构建镜像
    VFP发送XML与MSSQL的互操作, 解决一个传大表查询的大大大问题
    算法Day16 | 104.二叉树的最大深度,559.n叉树的最大深度, 111.二叉树的最小深度,222.完全二叉树的节点个数
    Adobe Creative Cloud没有管理应用程序的权限怎么办?
    某程序员发现 CSDN官方“漏洞”,立省¥10000+,抓紧薅吧
    火车头翻译插件-免费火车头翻译插件-火车头自动翻译插件
    最新 Solidity 语法学习笔记
    蓝桥杯实战应用【算法知识篇】-分治法介绍及关键点解析
    typescript---模块与命名空间(十四)
  • 原文地址:https://blog.csdn.net/qq_55123599/article/details/127613325