zoukankan      html  css  js  c++  java
  • 57. 三数之和 &&

    题目

    57. 三数之和
    
    给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组。
    样例
    
    如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集合的是:
    
    (-1, 0, 1)
    
    (-1, -1, 2)
    注意事项
    
    在三元组(a, b, c),要求a <= b <= c。
    
    结果不能包含重复的三元组。
    

    解析

    • 主要注意去除重复元素的方法
    class Solution_57 {
    public:
    	/**
    	* @param numbers: Give an array numbers of n integer
    	* @return: Find all unique triplets in the array which gives the sum of zero.
    	*/
    
    	vector<vector<int>> threeSum(vector<int> &numbers) {
    		// write your code here
    		vector<vector<int>> ret;
    		if (numbers.size() < 3)
    			return ret;
    		sort(numbers.begin(), numbers.end());
    		vector<int> vec;
    		for (int i = 0; i < numbers.size() - 2; i++)
    		{
    			if (i >= 1 && numbers[i - 1] == numbers[i]) //去重复元素
    			{
    				continue;
    			}
    
    			int b = i + 1;
    			int c = numbers.size() - 1;
    
    			while (b<c)
    			{
    				while (b<c&&c<numbers.size() - 1 && numbers[c + 1] == numbers[c]) //去重只需要在查找到过后进行,所以放在下面else里面,容易理解一些
    					c--;
    				while (b<c&&b - 1>i&&numbers[b - 1] == numbers[b])
    					b++;
    				if (b == c)
    					continue;
    
    				int sum = numbers[i] + numbers[b] + numbers[c];
    				if (sum>0)
    				{
    					c--;
    				}
    				else if (sum < 0)
    				{
    					b++;
    				}
    				else
    				{
    					vec.clear();
    					vec.push_back(numbers[i]);
    					vec.push_back(numbers[b]);
    					vec.push_back(numbers[c]);
    					ret.push_back(vec);
    					//break; //bug
    					c--;
    					b++;
    				}
    			}
    		}
    		return ret;
    	}
    
    
    	vector<vector<int>> threeSum(vector<int> &numbers) {
    		// write your code here
    		vector<vector<int>> ret;
    		if (numbers.size() < 3)
    			return ret;
    		sort(numbers.begin(), numbers.end());
    		vector<int> vec;
    		for (int i = 0; i < numbers.size() - 2; i++)
    		{
    			if (i >= 1 && numbers[i - 1] == numbers[i]) //去重复元素
    			{
    				continue;
    			}
    
    			int b = i + 1;
    			int c = numbers.size() - 1;
    
    			while (b<c)
    			{
    
    				int sum = numbers[i] + numbers[b] + numbers[c];
    				if (sum>0)
    				{
    					c--;
    				}
    				else if (sum < 0)
    				{
    					b++;
    				}
    				else
    				{
    					vec.clear();
    					vec.push_back(numbers[i]);
    					vec.push_back(numbers[b]);
    					vec.push_back(numbers[c]);
    					ret.push_back(vec);
    					//break; //bug
    					c--;
    					b++;
    
    					while (b < c&&numbers[c + 1] == numbers[c])
    						c--;
    					while (b < c&&numbers[b - 1] == numbers[b])
    						b++;
    
    				}
    			}
    		}
    		return ret;
    	}
    };
    
    
  • 相关阅读:
    21. Merge Two Sorted Lists
    496. Next Greater Element I
    (转载)深度学习的weight initialization
    Python collections模块
    Iterables vs. Iterators vs. Generators
    (转)iPhone开发关于UDID和UUID的一些理解
    uniqueIdentifier在ios7不支持后的替代方法
    Android——列表视图 ListView(一)Arrayadapter
    Android——对话框2(日期和时间对话框)
    Android——子线程操作主线程
  • 原文地址:https://www.cnblogs.com/ranjiewen/p/9590943.html
Copyright © 2011-2022 走看看