from typing import List
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
def nSumtarget(nums,n,start,target):
sz = len(nums)
res=[]
if n<2 or sz<n:
return res
# 2sum为base case
if n==2:
lo = start
hi = sz-1
while (lo<hi):
sum = nums[lo]+nums[hi]
left = nums[lo]
right = nums[hi]
if sum<target:
while lo<hi and nums[lo]==left:
lo+=1
elif sum>target:
while lo<hi and nums[hi]==right:
hi-=1
else:
res.append([left,right])
while lo<hi and nums[lo]==left:
lo+=1
while lo<hi and nums[hi]==right:
hi-=1
else:
#n>2时,递归计算(n-1)sum的结果
#for i in range(start,sz):
i=start
while i<sz:
print(i) #若第46行不存在,会陷入死循环
sub = nSumtarget(nums,n-1,i+1,target-nums[i])
#print("返回:%d"%i)
for arr in sub:
#(n-1)sum加上nums[i]就是nSum
arr.append(nums[i])
res.append(arr)
while (i<sz-1 and nums[i] == nums[i+1]):
#print(i)
i+=1
i=i+1 #注意,这里i值不加1会陷入死循环(i+=1的目的是实现外层循环)
return res
nums.sort()
print(nums)
return nSumtarget(nums,4,0,target)
if __name__=="__main__":
nums = [1,0,-1,0,-2,2]
target = 0
res = Solution().fourSum(nums,0)
print(res)
①运行结果(注意while循环的输出)
from typing import List
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
def nSumtarget(nums,n,start,target):
sz = len(nums)
res=[]
if n<2 or sz<n:
return res
# 2sum为base case
if n==2:
lo = start
hi = sz-1
while (lo<hi):
sum = nums[lo]+nums[hi]
left = nums[lo]
right = nums[hi]
if sum<target:
while lo<hi and nums[lo]==left:
lo+=1
elif sum>target:
while lo<hi and nums[hi]==right:
hi-=1
else:
res.append([left,right])
while lo<hi and nums[lo]==left:
lo+=1
while lo<hi and nums[hi]==right:
hi-=1
else:
#n>2时,递归计算(n-1)sum的结果
#for i in range(start,sz):
for i in range(start,sz):
print(i) #若第46行不存在,会陷入死循环
sub = nSumtarget(nums,n-1,i+1,target-nums[i])
#print("返回:%d"%i)
for arr in sub:
#(n-1)sum加上nums[i]就是nSum
arr.append(nums[i])
res.append(arr)
while (i<sz-1 and nums[i] == nums[i+1]):
#print(i)
i+=1
#i=i+1 #注意,这里i值不加1会陷入死循环(i+=1的目的是实现外层循环)
return res
nums.sort()
print(nums)
return nSumtarget(nums,4,0,target)
if __name__=="__main__":
nums = [1,0,-1,0,-2,2]
target = 0
res = Solution().fourSum(nums,0)
print(res)
②运行结果(for循环和while循环的不同之处)
c++中for循环和python3中存在差别,c++中下面的for循环变量i值可再循环内部中的while循环中更新,是全局的;而python的for循环变量i不会循环内部更新
using namespace std;
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums,int target) {
sort(nums.begin(), nums.end());
// n 为 3,从 nums[0] 开始计算和为 0 的三元组
return nSumTarget(nums, 4, 0, target);
}
/* 注意:调用这个函数之前一定要先给 nums 排序 */
// n 填写想求的是几数之和,start 从哪个索引开始计算(一般填 0),target 填想凑出的目标和
vector<vector<int>> nSumTarget(
vector<int>& nums, int n, int start, int target) {
int sz = nums.size();
vector<vector<int>> res;
// 至少是 2Sum,且数组大小不应该小于 n
if (n < 2 || sz < n) return res;
// 2Sum 是 base case
if (n == 2) {
// 双指针那一套 *** 作
int lo = start, hi = sz - 1;
while (lo < hi) {
int sum = nums[lo] + nums[hi];
int left = nums[lo], right = nums[hi];
//cout<<"判断左右指针大小\n";
if (sum < target) {
while (lo < hi && nums[lo] == left) lo++;
} else if (sum > target) {
while (lo < hi && nums[hi] == right) hi--;
} else {
res.push_back({left, right});
while (lo < hi && nums[lo] == left) lo++;
while (lo < hi && nums[hi] == right) hi--;
}
}
//cout<<"退出循环\n";
} else {
// n > 2 时,递归计算 (n-1)Sum 的结果
for (int i = start; i < sz; i++) {
cout<<i<<endl;
vector<vector<int>>
sub = nSumTarget(nums, n - 1, i + 1, target - nums[i]);
//cout<<"返回:"<
for (vector<int>& arr : sub) {
// (n-1)Sum 加上 nums[i] 就是 nSum
arr.push_back(nums[i]);
res.push_back(arr);
}
while (i < sz - 1 && nums[i] == nums[i + 1]) {
//cout<
i++;
//cout<
//cout<
//cout<
}
}
}
return res;
}
};
代码来源:https://mp.weixin.qq.com/s/fSyJVvggxHq28a0SdmZm6Q
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)