每周 ARTS 第 22 期

2019-09-23  本文已影响0人  落英坠露

1. Algorithm

15. 三数之和(中等)

描述:

给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

示例:
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为:
[
  [-1, 0, 1],
  [-1, -1, 2]
]
思路:

双指针法

首先对数组进行排序,然后固定一个数 nums[i],使用左右指针分别指向 nums[i] 后面的两端。计算三个数的和 sum,判断 sum 是否满足为 0。如果满足则添加进结果集,否则左右指针交替向中间移动。同时注意去重。

class Sulution {
    public List<List<Integer>> threeSum(int[] nums) {
        if (nums == null || nums.length < 3) {
            return Collections.emptyList();
        }
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<>();
        int length = nums.length;
        for (int i = 0; i < length; i++) {
            if (nums[i] > 0) {
                break;
            }
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int l = i + 1, r = length - 1;
            while (l < r) {
                int s = nums[i] + nums[l] + nums[r];
                if (s == 0) {
                    while (l < r && nums[l] == nums[l + 1]) {
                        l++;
                    }
                    while (l < r && nums[r] == nums[r - 1]) {
                        r--;
                    }
                    result.add(Arrays.asList(nums[i], nums[l], nums[r]));
                    l++;
                    r--;
                } else if (s > 0) {
                    r--;
                } else {
                    l++;
                }
            }
        }
        return result;
    }

}
分析:

2. Review

Advanced Coding Skills, Techniques, and Ideas

作者列举了一些编程的技巧。

代码经验都要靠积累,推荐阿里巴巴的代码规约

3. Tip

最近在读《深入理解计算机系统》,又称 CSAPP,真是神书!读完第一章,对计算机系统有了新的认识——深入理解计算机系统之计算机系统漫游

4. Share

说一说如何选书。写书是一件正式的活动,作者都会认真对待。但是作者的经验水平不一样,产出的内容质量也参差不齐。对于追热点的、空洞无物的、作者资历尚浅的图书,一般不值得阅读。辨别的方式是看豆瓣评分,经过时间筛选过的经典,最值得阅读。

上一篇 下一篇

猜你喜欢

热点阅读