leetcode 记录

这篇博客涵盖了多种算法问题的解决方案,包括LeetCode上的删除链表倒数第n个节点、IP地址无效化,以及糖果分配问题。还讨论了如何解决手套匹配问题,并探讨了N叉树的深度计算,展示了递归与非递归两种方法。这些内容涉及数据结构、算法和问题解决策略。
摘要由CSDN通过智能技术生成

19.删除链表的倒数第n个节点

只遍历一遍链表

class Solution19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode slow = head;
        ListNode fast = head;
        for (int i = 0; i < n; i++) {
            fast = fast.next;
        }
        if (fast == null) {
            return head.next;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }

        slow.next = slow.next.next;

        return head;
    }
}

1108.IP地址无效化

class Solution1108 {

    public static String defangIPaddr1(String address) {
        return address.replace(".", "[.]");
    }

    public String defangIPaddr2(String address) {
        StringBuilder res = new StringBuilder();
        int len = address.length();
        for (int i = 0; i < len; i++) {
            char ch = address.charAt(i);
            if (ch == '.') {
                res.append("[.]");
            } else {
                res.append(ch);
            }
        }
        return res.toString();
    }

}

575.分糖果

class Solution575 {
    class Solution {
        public int distributeCandies(int[] candyType) {
            HashSet<Integer> set = new HashSet<>();
            for (int i : candyType) {
                set.add(i);
            }
            return Math.min(set.size(), candyType.length / 2);
        }
    }
}

1103.分糖果II

class Solution1103 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(distributeCandies(7, 4)));
    }

    public static int[] distributeCandies(int candies, int num_people) {
        int[] res = new int[num_people];
        int getCandies = 1;

        while (candies > 0) {
            for (int i = 0; i < num_people; i++) {
                if (candies - getCandies > 0) {
                    candies -= getCandies;
                    res[i] += (getCandies++);
                } else {
                    res[i] += candies;
                    return res;
                }
            }
        }
        return res;
    }
}

手套问题

class SolutionSolves {
    public int findMinimum(int n, int[] left, int[] right) {
        int sum_left = 0;
        int sum_right = 0;
        int left_min = Integer.MAX_VALUE;
        int right_min = Integer.MAX_VALUE;
        int zero_color = 0;

        for (int i = 0; i < n; i++) {
            
            if (left[i] * right[i] == 0) {

                zero_color += left[i];
                zero_color += right[i];

            } else {
                left_min = Math.min(left_min, left[i]);
                right_min = Math.min(right_min, right[i]);

                sum_left += left[i];
                sum_right += right[i];

            }
        }

        return (sum_left < sum_right ? sum_left - left_min + zero_color + 2
                : sum_right - right_min + zero_color + 2);
    }
}

559.求N叉树的深度

class Solution559 {
    //递归实现
    public int maxDepth(Node root) {
        if (root == null) return 0;

        int res = 1;
        for (Node child : root.children) {
            res = Math.max(res, maxDepth(child) + 1);
        }
        return res;
    }

    //非递归实现
    public int maxDepth1(Node root) {
        if (root == null) return 0;
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        int depth = 0;
        while (!queue.isEmpty()) {
            depth++;
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                Node cur = queue.poll();
                for (Node child : cur.children) {
                    queue.offer(child);
                }
            }
        }
        return depth;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值