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.
Notice that the solution set must not contain duplicate triplets.
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Notice that the solution set must not contain duplicate quadruplets.
classSolution { public: vector<vector<int>> fourSum(vector<int>& nums, int target) { vector<vector<int>> re; sort(nums.begin(), nums.end()); int length = nums.size(); int hi, lo; for(int i = 0;i<length-3;i++) { if(i>0&&nums[i]==nums[i-1]) continue; //去重 /*3sum*/ for(int j = i+1;j<length-2;j++) { if(j>i+1&&nums[j]==nums[j-1]) continue; lo = j+1; hi = length-1; while(lo<hi) { int value = nums[i]+nums[j]+nums[lo]+nums[hi]; if(value==target) { re.push_back({nums[i],nums[j],nums[lo],nums[hi]}); lo++; hi--; while(lo<hi&&nums[lo]==nums[lo-1]) lo++; while(lo<hi&&nums[hi]==nums[hi+1]) hi--; } elseif(value>target) { hi--; while(lo<hi&&nums[hi]==nums[hi+1]) hi--; } else { lo++; while(lo<hi&&nums[lo]==nums[lo-1]) lo++; } } } } return re; } };
1.4 four sum Ⅱ
【题目】
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.
To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.
【解析】
这题比four sum Ⅰ简单,因为只需要算出数量而不需要返回具体下标。因为有多次查询所以优先考虑哈希表。所以先将A和B所有可能的和存入hashmap中,然后计算C和D所有可能的和的相反数,在hashmap中一一查找它们即可。
There are some spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it’s horizontal, y-coordinates don’t matter, and hence the x-coordinates of start and end of the diameter suffice. The start is always smaller than the end.
An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps traveling up infinitely.
Given an array points where points[i] = [xstart, xend], return the minimum number of arrows that must be shot to burst all balloons.
题意:给定一些区间,将所有具有公共重叠部分的区间分到一组,问最少有几组。
Example 1:
Input: points = [[10,16],[2,8],[1,6],[7,12]] Output: 2 Explanation: One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons).
/* 将原数组下标x映射到离散化数组的下标(若有重复,则返回第一个)*/ intfind(int x) { int l = 0, r = alls.size() - 1; while (l < r) { int mid = l + r >> 1; if (alls[mid] >= x) r = mid; else l = mid + 1; } return r; // 映射到0, 1, 2, ...n }
/*现在可以利用find函数构造离散化数组了。原数组中【下标间距可能很大】的所有元素按照它们的相对 位置被存放到离散化数组a的a[0],a[1],a[2]...中。*/ for(int i = 0;i<离散化处理数组.size();i++) { int x = find(离散化处理数组[i]);//根据元素在原数组中的下标找到它在离散化数组中的下标 a[x] = 原数组[离散化处理数组[i]]; //原数组无法用数组表示,因此这一句只是逻辑上的 }