统计1-8个数组中的和为9的组合, 要求时间复杂度为 0(n)

    public static void main(String[] args) {
        int [] arr= {1,2,3,4,5,6,7,8};
        // 使用hash表作为缓存
        HashMap<Integer, Integer> buf = new HashMap<>();
        int second;
        for (int i = 1; i <=arr.length; i++) {
            second=9-i;
            buf.put(second,second);
            if (buf.containsKey(second)) {
                System.out.println("("+(i)+","+buf.get(second)+")");
            }
        }
    }
posted @ 2021-06-22 15:11  庭有奇树  阅读(62)  评论(0编辑  收藏  举报