From dda6d708fcb3d6064f433750c53a6e839e7e3dd5 Mon Sep 17 00:00:00 2001 From: MITHILESH Date: Thu, 1 Oct 2020 12:49:55 +0530 Subject: [PATCH] Create 3-Sum.cpp --- cpp/3-Sum.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 cpp/3-Sum.cpp diff --git a/cpp/3-Sum.cpp b/cpp/3-Sum.cpp new file mode 100644 index 0000000000..1c99b34de1 --- /dev/null +++ b/cpp/3-Sum.cpp @@ -0,0 +1,47 @@ +class Solution { +public: + vector> threeSum(vector& nums) { + + // + vector> res; + //sort + sort(nums.begin(),nums.end()); + + for(unsigned int i = 0; i < nums.size();i++){ + // initialize left and right + //to handle duplicates + if(i > 0 && nums[i]==nums[i-1]) + continue; + int l = i + 1; + int r = nums.size() - 1; + int x = nums[i]; + + while (l < r) + { + if (x + nums[l] + nums[r] == 0) { + res.push_back(vector{x,nums[l],nums[r]}); + //to handle duplicates + while (l pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy