暴力求解3sum超时,花了一上午写的,超时,有时间换个思路写吧暂时就不改了
class Solution {public: vector> threeSum(vector & nums) { int len = int(nums.size()); sort(nums.begin(), nums.end(),less ()); int newlenth = len*(len-1)*(len-2)/6; vector > res(newlenth,vector (3,0)); //vector > ivec(m ,vector (n,0)); int p=0; for (int i=0; i 0 && nums[i] == nums[i-1]){ continue; } for (int j=i+1; j 0 && nums[j] == nums[j-1]){ continue; } for (int k=j+1; k 0 && nums[k] == nums[k-1]){ continue; } if (nums[i]+nums[j]+nums[k]==0) { cout< < < < < < < <<" "< << " "< <
之后我又写了个二分查找,也超时了,竟然是应为打印 ,我用了一个count++ 计数,导致了超时,先添加错误代码,在添加正确代码,我觉得挺好理解的。添加代码改版了 没弄明白,晚上接着贴代码吧~