左耳听风ARTS第十七周

Algorithms

15. 3Sum
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

The solution set must not contain duplicate triplets.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

Solution 1——重点需要去重,时间复杂度为O(n2)。

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
         Arrays.sort(nums);
        int length = nums.length - 1;
        List<List<Integer>> list = new ArrayList<>();
        for (int i = 0;i < length -1;i++) {
            int sum = -nums[i];
            if (i > 0 &&nums[i] == nums[i - 1]){
                continue;       //去重
            }
            for (int j = i+1,end = length;j < end;) {
                if (nums[j] + nums[end] == sum) {
                    list.add(Arrays.asList(nums[i], nums[j], nums[end]));
                    while (j < end && nums[j] == nums[j - 1]) {
                        j++;
                    }
                    while (j < end && nums[end] == nums[end - 1]) {
                        end--;
                    }
                    end--;
                }else if (nums[j] + nums[end] > sum) {
                    end--;
                }else {
                    j++;
                }
            }
        }
            return list;
    }
}

Review

The Best Code is No Code At All
1、作为软件开发者,你是你自己最大的敌人,越早意识到这一点,对你越好。
2、评估代码的几个方面:代码简洁、功能丰富、执行速度、编码时间、稳健性、扩展性
3、以代码简洁作为首要点,然后按照测试需要增加其他几个维度的重要性。
4、最好的代码就是no code。

Tips

在复制一个目录的时候怎么排除掉相关文件或者子目录

rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude

如:想把test目录的内容复制到tmp目录,并且排除掉目录folder1和文件file1。

rsync -av --progress /test  /tmp --exclude folder1 --exlucde file1

Share

设计模式之观察者模式(四)

猜你喜欢

转载自blog.csdn.net/wuweiwoshishei/article/details/88943407