力扣 题目15-- 三数之和
题目
题解
1.先排序 如果遍历一下如果没有正数和负数的临界点 说明只有正数或者只有负数 不可能有三个数加和等于 0,直接返回结果。
2.对于重复元素:跳过,避免出现重复解
3.令左指针 L=i+1,右指针 R=n-1,当 L<R 时,执行循环:
当 nums[i]+nums[L]+nums[R]==0,执行循环,判断左界和右界是否和下一位置重复,去除重复解。并同时将 L,R 移到下一位置,寻找新的解
若和大于 0,说明 nums[R]太大,RR 左移
若和小于 0,说明 nums[L] 太小,LL 右移
4.对0可以统计一下0的情况然后把0缩小到1个 如果有3个及以上的0就在返回的vector里面加上{0,0,0} 然后把这三个删除两个只剩下一个给后面的循环
5.由于在遍历过程中num[i]大于0时已经不可能出现有用的情况 所以遍历只遍历到负数和正数的临界点即可
代码
代码1--正确思路
1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 using namespace std; 5 class Solution { 6 public: 7 vector<vector<int>> threeSum(vector<int>& nums) { 8 vector<vector<int>>three; 9 int zero[3] = {-1,-1,-1}; 10 int middle=-1; 11 //排序 12 sort(nums.begin(), nums.end()); 13 //将0进行留一个处理 并记录0的个数 14 for (int i = 0; i < nums.size(); i++) { 15 int num = zero[2]; 16 if (nums[i] == 0&& zero[1]==-1) { 17 zero[0] = i; 18 zero[2] = zero[2] + 1; 19 } 20 if (nums[i] == 0&&zero[0]!=-1) { 21 zero[1] = i; 22 zero[2]=zero[2] + 1; 23 } 24 if (zero[0] != -1&&zero[2]== num) { 25 break; 26 } 27 } 28 if (zero[2] > 1) { 29 nums.erase(nums.begin()+ zero[0], nums.begin() + zero[1]); 30 } 31 int min = nums.size() - 1; 32 //如果0的个数大于2 即有000存在 33 if (zero[2] > 2) { 34 three.push_back({ 0,0,0 }); 35 } 36 //找到负数和正数的临界点 37 for (int i = 0; i < nums.size(); i++) { 38 if (i>0&&nums[i] >= 0 && nums[i - 1] <= 0) { 39 middle = i;//1 40 break; 41 } 42 } 43 //如果没有即负数 44 if (middle == -1) { 45 return three; 46 } 47 //这里开始遍历 48 for (int i = 0; i < middle; i++) { 49 if (i > 0 and nums[i] == nums[i - 1]) { 50 continue; 51 } 52 int left = i + 1; 53 int right = nums.size() - 1; 54 while (left< right) 55 { 56 if (nums[left] + nums[i] + nums[right]==0) { 57 three.push_back({ nums[left],nums[i],nums[right] }); 58 cout << left << endl; 59 cout << right << endl; 60 while (left<right&&nums[left] == nums[left + 1]) { 61 left = left + 1; 62 } 63 while (left < right && nums[right] == nums[right - 1]) { 64 right = right - 1; 65 } 66 left = left + 1; 67 right = right - 1; 68 } 69 else if (nums[i] + nums[left] + nums[right] > 0) { 70 right = right - 1; 71 } 72 else { 73 left = left + 1; 74 } 75 } 76 } 77 78 return three; 79 } 80 }; 81 82 int main() { 83 Solution sol; 84 vector<int> nums = { -4,-2,1,-5,-4,-4,4,-2,0,4,0,-2,3,1,-5,0 }; 85 vector<vector<int>>num=sol.threeSum(nums); 86 for (int i = 0; i < num.size(); i++) { 87 cout << "num[" << i << "]" << "[0]=" << num[i][0] << endl; 88 cout << "num[" << i << "]" << "[1]=" << num[i][1] << endl; 89 cout << "num[" << i << "]" << "[2]=" << num[i][2] << endl; 90 } 91 }
代码2--蜜汁暴力破解(过不了力扣)
1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 using namespace std; 5 class Solution { 6 public: 7 vector<vector<int>> threeSum(vector<int>& nums) { 8 vector<vector<int>>three; 9 int zero[3] = {-1,-1,-1}; 10 int middle=-1; 11 //排序并 12 sort(nums.begin(), nums.end()); 13 //将0进行留一个处理 并记录0的个数 14 for (int i = 0; i < nums.size(); i++) { 15 int num = zero[2]; 16 if (nums[i] == 0&& zero[1]==-1) { 17 zero[0] = i; 18 zero[2] = zero[2] + 1; 19 } 20 if (nums[i] == 0&&zero[0]!=-1) { 21 zero[1] = i; 22 zero[2]=zero[2] + 1; 23 } 24 if (zero[0] != -1&&zero[2]== num) { 25 break; 26 } 27 } 28 if (zero[2] > 1) { 29 nums.erase(nums.begin()+ zero[0], nums.begin() + zero[1]); 30 } 31 //如果0的个数大于2 即有000存在 32 if (zero[2] > 2) { 33 three.push_back({ 0,0,0 }); 34 } 35 //找到中间 36 for (int i = 0; i < nums.size(); i++) { 37 if (i>0&&nums[i] >= 0 && nums[i - 1] <= 0) { 38 middle = i;//1 39 break; 40 } 41 } 42 if (middle == -1) { 43 return three; 44 } 45 //左遍历 46 for (int i = 0; i < middle; i++) { 47 if (i > 0 && nums[i] == nums[i - 1]) { 48 continue; 49 } 50 for (int j = i+1; j < middle; j++) { 51 if (nums[j] == nums[j + 1]) { 52 continue; 53 } 54 vector<int>::iterator result =find(nums.begin() + middle, nums.end(), -(nums[i]+nums[j])); 55 if (result != nums.end()) { 56 three.push_back({ nums[i],nums[j],*result }); 57 } 58 } 59 } 60 //右遍历 61 for (int i = middle; i < nums.size(); i++) { 62 if (i> middle &&nums[i] == nums[i - 1]) { 63 continue; 64 } 65 for (int j = i + 1; j < nums.size(); j++) { 66 if (j!= nums.size()-1&&nums[j] == nums[j + 1]) { 67 continue; 68 } 69 vector<int>::iterator result = find(nums.begin(), nums.begin() + middle, -(nums[i] + nums[j])); 70 if (result != nums.begin() + middle) { 71 three.push_back({ *result,nums[i],nums[j]}); 72 } 73 } 74 } 75 return three; 76 } 77 }; 78 79 int main() { 80 Solution sol; 81 vector<int> nums = { -4,-2,1,-5,-4,-4,4,-2,0,4,0,-2,3,1,-5,0 }; 82 vector<vector<int>>num=sol.threeSum(nums); 83 for (int i = 0; i < num.size(); i++) { 84 cout << "num[" << i << "]" << "[0]=" << num[i][0] << endl; 85 cout << "num[" << i << "]" << "[1]=" << num[i][1] << endl; 86 cout << "num[" << i << "]" << "[2]=" << num[i][2] << endl; 87 } 88 }