structSolution;implSolution{// 对于有序数组的两数之和,可以使用双指针fntwo_sum(i:usize,nums:&Vec<i32>,ans:&mutVec<Vec<i32>>){let(mutj,mutk)=(i+1,nums.len()-1);whilej<k{letsum=nums[i]+nums[j]+nums[k];ifsum<0{j+=1;}elseifsum>0{k-=1;}else{ans.push(vec![nums[i],nums[j],nums[k]]);lett=nums[j];whilej+1<nums.len()&&nums[j+1]==t{j+=1;}k-=1;j+=1;}}}pubfnthree_sum(mutnums:Vec<i32>)->Vec<Vec<i32>>{letmutans=Vec::<Vec<i32>>::new();nums.sort_unstable();letmuti=0;whilei<nums.len(){Self::two_sum(i,&nums,&mutans);lett=nums[i];whilei<nums.len()&&nums[i]==t{i+=1}}ans}}/// # 3Sum#[cfg(test)]pubmodtest_15{usecrate::hot100::two_pointers::p15::Solution;#[test]fntest_p15(){// You can use `assert_eq!(target,Solution::function(args))` to call the functionassert_eq!(vec![vec![-1,-1,2],vec![-1,0,1]],Solution::three_sum(vec![-1,0,1,2,-1,-4]))}}