• 【Java】Deque接口与List接口中的remove方法


    Deque接口与List接口中的remove方法的区别

    太长不看系列: Deque的remove(Object o)。指定的是元素,List的remove(int index),指定的是索引。


    在刷力扣113.路径总和 II 时使用Deque的remove方法出现错误,记录一下原因和理清相关概念。

    先上代码:
    回溯时应该使用DequeremoveLast方法,错误使用将Dequeremove方法当做Listremove来使用,导致程序结果错误。

    class Solution {
        List<List<Integer>> res = new ArrayList<>();
        Deque<Integer> path = new LinkedList<>();
        public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
            if (root == null) return res;
            dfs(root, targetSum);
            return res;
        }
    
        private void dfs(TreeNode root, int targetSum) {
            path.add(root.val);
            if (root.left == null && root.right == null) {
                if (targetSum == root.val) {
                    res.add(new ArrayList<>(path));
                }
                return;
            }
            if (root.left != null) {
                dfs(root.left, targetSum - root.val);
                path.removeLast(); // 此处!!! 错误代码:path.remove(path.size() - 1)
            }
            if (root.right != null) {
                dfs(root.right, targetSum - root.val);
                path.removeLast(); // 此处!!!
            }
        }
    }
    
    • 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

    具体原因:Deque接口中的remove用来移除指定值的元素,List接口中的remove是index

    List

    List 接口中,remove(int index) 方法是用来移除位于指定位置的元素的。而 LinkedList 实现了 List 接口,所以 path.remove(path.size() - 1) 会尝试移除位于指定索引位置的元素。

    Deque

    LinkedList 同时也实现了 Deque 接口,而 Deque 接口中有一个方法 remove(Object o),它是用来移除第一次出现的指定元素的。当 path.remove(path.size() - 1) 被调用时,如果 path.size() - 1 正好也是 LinkedList 中的一个整数值,remove 方法可能会将这个整数值误解为要移除的元素,而不是索引。

    这会导致两个问题:(使用Deque接口的remove(path.size() - 1))

    1. 如果 path.size() - 1 作为整数值存在于列表中,它会移除这个值的第一次出现,而不是最后一个元素。
    2. 如果 path.size() - 1 作为整数值不存在于列表中,它会抛出 IndexOutOfBoundsException

    因此,当使用 LinkedList 作为 Deque 使用时,应该使用 addLastremoveLast 等明确的 Deque 操作来避免可能的混淆和错误。

  • 相关阅读:
    高防服务器中的秒解服务器什么意思?
    SpringBoot中required a bean of type ‘java.lang.String‘ that could not be found问题
    Liquid Studio 20.x.x Crack update
    每天学习3个小时能不能考上浙大MBA项目?
    【WinSDK】信号量不能看作弱化版互斥体
    猿粉猿动力-开发者活动袭!
    链式插补 (MICE):弥合不完整数据分析的差距
    猿创征文 | 关于游戏开发的那些工具
    C++学习笔记一: 变量和基本类型
    【战略合作】新的游戏合作伙伴来袭,CARV 助力 Aavegotchi 发展!
  • 原文地址:https://blog.csdn.net/weixin_44223180/article/details/136281355