LeetCode【HOT100<二>】

本文深入探讨了LeetCode热门100题中的经典算法问题,包括解决思路、代码实现及应用场景,旨在提升程序员的算法能力和职场竞争力。
摘要由CSDN通过智能技术生成

 public ListNode detectCycle(ListNode head) {
        ListNode pos = head;
        HashSet<ListNode> set = new HashSet<>();
        while (pos != null){
            if (set.contains(pos)){
                return pos;
            }else {
                set.add(pos);
            }
            pos = pos.next;
        }
        return null;
    }

 public ListNode sortList(ListNode head) {
        if (head == null || head.next == null){
            return head;
        }
        ListNode fast = head.next;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode tmp = slow.next;
        slow.next = null;
        ListNode left = sortList(head);
        ListNode right = sortList(tmp);
        ListNode h = new ListNode(0);
        ListNode res = h;
        while (left != null && right != null){
            if (left.val < right.val){
                h.next = left;
                left = left.next;
            }else {
                h.next = right;
                right = right.next;
            }
            h = h.next;
        }
        h.next = left != null ? left : right;
        return res.next;
    }

 private Stack<Integer> stack;
    private Stack<Integer> minStack;
    public  MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }
    public void push(int val) {
        stack.push(val);
        if (minStack.empty()){
            minStack.push(val);
        }else {
            int top = minStack.peek();
            if (val <= top){
                minStack.push(val);
            }
        }
    }
    public void pop() {
        int val = stack.pop();
        if (!minStack.empty()){
            int top = minStack.peek();
            if (val == top){
                minStack.pop();
            }
        }
    }
    public int top() {
        if (stack.empty()){
            return -1;
        }else {
            return stack.peek();
        }
    }
    public int getMin() {
        if (minStack.empty()){
            return -1;
        }else {
            return minStack.peek();
        }
    }

  public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        HashSet<ListNode> set = new HashSet<>();
        ListNode tmp = headA;
        while (tmp != null){
            set.add(tmp);
            tmp = tmp.next;
        }
        tmp = headB;
        while (tmp != null){
            if (set.contains(tmp)){
                return tmp;
            }
            tmp = tmp.next;
        }
        return null;
    }

private int[] memo;
    public int rob(int[] nums){
        memo = new int[nums.length];
        Arrays.fill(memo,-1);
        return dp(nums,0);
    }
    private int dp(int[] nums, int i) {

        if (i >= nums.length){
            return 0;
        }
        if (memo[i] != -1){
            return memo[i];
        }
        int res = Math.max(dp(nums,i+1),nums[i] + dp(nums,i+2));
        memo[i] = res;
        return res;
    }

 public int numIslands(char[][] grid) {
        int res = 0;
        int m = grid.length,n = grid[0].length;
        for (int i = 0; i < m; i++){
            for (int j = 0; j < n; j++){
                if (grid[i][j] == '1'){
                    res++;
                    dfs(grid,i,j);
                }
            }
        }
        return res;
    }

    private void dfs(char[][] grid, int i, int j) {
        int m = grid.length,n = grid[0].length;
        if (i < 0 || j < 0 || i >= m || j >= n){
            return;
        }
        if (grid[i][j] == '0'){
            return;
        }
        grid[i][j] = '0';
        dfs(grid,i+1,j);
        dfs(grid,i,j+1);
        dfs(grid,i-1,j);
        dfs(grid,i,j-1);
    }

  public ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null){
            ListNode curNext = cur.next;
            cur.next = pre;
            pre = cur;
            cur = curNext;
        }
        return pre;
    }

  public int findKthLargest(int[] nums, int k) {
    PriorityQueue<Integer> pq = new PriorityQueue<>();
        for (int e : nums){
            pq.offer(e);
            if (pq.size() > k){
                pq.poll();
            }
        }
        return pq.peek();
    }v

public int maximalSquare(char[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        int[][] dp = new int[m][n];
        for (int i = 0; i < m; i++){
            dp[i][0] = matrix[i][0] - '0';
        }
        for (int j = 0; j < n; j+
你可以使用以下方法对维向量vector<vector<int>>进行排序: 1. 使用STL中的sort函数进行排序。引用中提到了一些排序算法,比如堆排序(heap_sort),希尔排序(shell_sort),选择排序(select_sort)和快速排序(quick_sort_swap)。你可以根据需要选择其中一个算法对vector<vector<int>>进行排序。 2. 如果你想在创建维向量时就进行排序,你可以使用引用中的方法通过vector创建维数组,并在创建时对其中的元素进行排序。 3. 如果你已经创建了一个维向量,你可以使用引用中的方法使用resize函数对维向量进行重新分配,并在重新分配时对其中的元素进行排序。 无论你选择哪种方法,都可以对维向量vector<vector<int>>进行排序。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [vectorstring和vectorint](https://blog.csdn.net/toCVer/article/details/122484732)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [leetcode2sumc-Code-Storm2019:力密码2019](https://download.csdn.net/download/weixin_38700430/19937142)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-孤单又灿烂的神-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值