https://lina.bitcron.com/post/code/2018-summer-fb-intern-mian-jing
类似 3 sum smaller(fixed one side and move the other side and then pick any elems from that range).O(n)可解2sum + subsets结合的一题,给一个int array 没有重复 和一个target,要求数可以生成的subset数目满足条件 min(subset) + max(subset) < target
input: sorted array, int targetoutput: # of subsets
public int numOfSubsets(int nums[], int target){
int i = 0, j = nums.length-1;
int res = 0;
while(i<=j){
if(nums[i]+nums[j]>=target){
j--;
}
else{
res += Math.pow(2,(j-i));
i++;
}
}
return res;
}