一个整型数组,求任意三个元素和为0的集合,不能重复。

最近在研究leetcode上的算法,厌倦了天天修修补补的工作,准备换个环境开始新的码农生活。

既然要换个环境,那就要有所准备,算法是必须的,所以没事儿刷刷leetcode,学习下别人的编程思维,加快编程的速度,避免常见的BUG。记录的过程也是学习和加深理解的过程,以防以后不时之需方便查阅。废话不多说,直接上题。

1.原文的题目是:

Given an array nums of n integers, are there elements abin 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]
]

题目一目了然,相信都能看懂。

2.我的解法是:操作数组,首先对数组进行排序,先取出第一个,第二个,最后一个元素三个数字进行和比较,如果和小于0,说明第二个数字小啦,需要向后移动一位(middle++),再进行比较;如果大于0,则说明,最后一个数字大了,需向前移动一位(right--),依次进行比较。但是内含有许多细节,重复的问题,if(i > 0 && nums[i] == nums[i-1]) continue;这行代码就是对第一个数字进行重复的判断,之所以是i>0,因为第一次循环的时候第一个数是不需要判断的,所以由此条件;在循环内,固定了第一个数,此时需要移动第二个数和最后一个数进行比较,即while(middle < right){}部分。在进行第二个和最后一个比较的时候,也需要注意判重,即while(middle < right && nums[middle] == nums[middle-1]) middle ++;和while(middle < right && nums[right] == nums[right+1]) right --;以上即是我对此题的理解,时间复杂度和空间复杂度都O(n^2)。
public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums); // 先进行排序
        List<List<Integer>> result = new ArrayList<>();
        int length = nums.length;
        for(int i = 0; i< length-2; i++) { // 外层循环
            if(i > 0 && nums[i] == nums[i-1]) continue; // 排除重复的
            int middle = i + 1,right = length - 1; // 定义两个索引,方便计算
            while(middle < right) {
                int temp = nums[i] + nums[middle] + nums[right];
                if(temp < 0) middle ++; // 小于0,中间的数字小了,需要往后移动
                else if(temp > 0) right --; // 大于0,最后的数字大了,需往前移动
                else { // 等于,此时的三个数字就是我们所要找的
                    /**
                     *此处做了三个动作。1.将找到的i,middle,right加入到数组
                     * 2.用Arrays.asList将数组转换成list
                     * 3.所有middle加1操作,right减1操作,此操作过后,middle往后移动了right                      * 往前移动了
                     */
                    result.add(Arrays.asList(nums[i],nums[middle++],nums[right--]));
                    // 中间的数字和前一个比较,两个相同,则往后移动
                    while(middle < right && nums[middle] == nums[middle-1]) middle ++;
                    // 末尾的数字和后一个比较,两个相同,则往前移动
                    while(middle < right && nums[right] == nums[right+1]) right --;
                }
            }
        }
        return result;
    }

2.别人的算法:此人的解法妙不可言,代码简洁明了,再对比下自己的代码,简直不堪入目,做完之后还沾沾自喜,看完人家的代码有种无地自容的感觉,且看大神解法。

// This is my solution. Took 187 micro seconds on Leetcode.
// Fix inx at beginning of the array, incrementing by 1 for each iteration.
// 	Fix jnx to inx + 1 and knx to array length - 1 so that we go forward and backward for each inx pivot.
// Finally, collect the results into a SET so that the duplicates can be removed.
public List<List<Integer>> threeSum(int[] nums) 
{
    Arrays.sort( nums );
    Set<List<Integer>> numberListSet = new HashSet<List<Integer>>();

	int jnx = 1;
	int knx = nums.length - 1;
	
    for( int inx = 0; inx < nums.length - 1 ; inx ++ )
    {
    	jnx = inx + 1;
    	knx = nums.length - 1;
    	
    	while( jnx < knx )
    	{
    		if( nums[inx] + nums[jnx] + nums[knx] == 0 )
    		{
    			List<Integer> numberList = new ArrayList<Integer>();
				numberList.add( nums[inx] );
				numberList.add( nums[jnx] );
				numberList.add( nums[knx] );
				
		    	numberListSet.add( numberList );
		    	
		    	jnx++;
		    	knx--;
    		}
    		else if( nums[inx] + nums[jnx] + nums[knx] < 0 )
    		{
    			jnx++;
    		}
    		else
    		{
    			knx--;
    		}
    	}
    }
    
	List<List<Integer>> listOfLists = new ArrayList<List<Integer>>();
	listOfLists.addAll( numberListSet );
	return listOfLists;
}

代码相当简洁,此人是Netherlands(荷兰)的Pavan Dittakavi

注:以上若对您有任何侵权行为,请联系本人,谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值