leetcode 4Sum Summary for unique and duplicates
We should pay attention to the duplicates in this problem. STL offers a way to be unique:[cpp]class Solution {public:vector<vector<int> > fourSum(vector<int> &num, int target) {vector<vector<int> > ret;if(num.size() < 4) return ret;sort(num.begin(), num.end());for(int i = 0; i < num.size(); ++i)for(int j = i + 1; j < num.size(); ++j) {int left = j + 1;int right = (int)num.size() - 1;int sum = 0;while(left < right){sum=num[i]+num[j]+num[left]+num[right];if(target == sum){ret.push_back(vector<int>(4));ret.back()[0]=num[i];ret.back()[1]=num[j];ret.back()[2]=num[left];ret.back()[3]=num[right];++left;--right;}else if(sum > target)--right;else++left;}}vector<vector<int>>:: iterator it = unique(ret.begin(),ret.end());ret.resize(distance(ret.begin(), it));return ret;}};The function unique Removes all but the first element from every consecutive group of equivalent elements in the range [first,last). Here's the example from the cplusplus.com. Vectors could be sorted since we could compare every two vectors.[cpp]// unique algorithm example#include <iostream> // std::cout#include <algorithm> // std::unique, std::distance#include <vector> // std::vectorbool myfunction (int i, int j) {return (i==j);}int main () {int myints[] = {10,20,20,20,30,30,20,20,10}; // 10 20 20 20 30 30 20 20 10std::vector<int> myvector (myints,myints+9);// using default comparison:std::vector<int>::iterator it;it = std::unique (myvector.begin(), myvector.end()); // 10 20 30 20 10 ? ? ? ?// ^myvector.resize( std::distance(myvector.begin(),it) ); // 10 20 30 20 10// using predicate comparison:std::unique (myvector.begin(), myvector.end(), myfunction); // (no changes)// print out content:std::cout << "myvector contains:";for (it=myvector.begin(); it!=myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;}Another way to avoid duplicates is using set, int num; string tmp += num; So num could be treated as a character. However, when num > 255, it could be seen as num%256. So,[cpp]#include<vector>#include<stdio.h>#include<iostream>#include<set>#include<map>#include<algorithm>#include<string>//不添加string会出现模版的错误using namespace std;class Solution {public:vector<vector<int> > fourSum(vector<int> &num, int target) {// Start typing your C/C++ solution below// DO NOT write int main() functionint nSize = num.size();vector< vector<int> > result;if (nSize < 4) return result;sort(num.begin(), num.end());vector<int> mid(4);//指定对象的大小为4,mid(4,1)含有4个元素,每个为1set<string> isExit;for (int i = 0; i < nSize - 3; ++i){mid[0] = num[i];for (int j = i + 1; j < nSize - 2; ++j){mid[1] = num[j];int l = j + 1;int r = nSize - 1;int sum = target - num[i] - num[j];while(l < r)补充:软件开发 , C++ ,
上一个:插入排序算法
下一个:Leetcode: Clone Graph
- 更多C/C++疑问解答:
- 关于c++的cout输出的问题。
- 在学校里学过C和C++,不过学的很一般,现在自学C#,会不会很难?
- 全国计算机二级C语言笔试题
- 已知某树有2个2度结点,3个3度结点,4个4度结点,问有几个叶子结点?
- c++数据结构内部排序问题,整数排序
- 2012九月计算机二级C语言全国题库,,急求急求
- 如果assert只有一个字符串作为参数,是什么意思呢?
- C语言中,哪些运算符具有左结合性,哪些具有右结合性,帮忙总结下,谢谢了!
- 为什么用结构体编写的程序输入是,0输不出来啊~~~
- 将IEEE—754的十六进制转化为十进制浮点类型,用C或C++都行,多谢各位大侠啊,非常感谢!
- 为什么这个程序求不出公式?
- 这个链表倒置的算法请大家分析下
- c语言函数库调用
- C语言unsigned int纠错
- C语言快排求解啊