diff --git a/C++/2Sum.cpp b/C++/2Sum.cpp new file mode 100644 index 0000000..2264baf --- /dev/null +++ b/C++/2Sum.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) + { + unordered_map mp; + for(int i=0;i> fourSum(vector& nums, int target) + { + vector> res; + if(nums.size() < 4) return res; + set> hash; + sort(nums.begin(), nums.end()); + for(int i = 0; i < nums.size() - 2; i++) { + for(int j = i + 1; j < nums.size() - 1; j++) + { + int left = j + 1; + int right = nums.size() - 1; + while(left < right) + { + int sum = nums[i] + nums[left] + nums[right] + nums[j]; + if(sum == target && !hash.count({nums[i], nums[j], nums[left], nums[right]})) { + hash.insert({nums[i], nums[j], nums[left], nums[right]}); + res.push_back({nums[i], nums[j], nums[left], nums[right]}); + left++; + right--; + while(left < right && nums[left] == nums[left - 1]) + left++; + while(left < right && nums[right] == nums[right + 1]) + right--; + } + else if(target > sum) + left++; + else + right--; + } + } + } + return res; + } +}; \ No newline at end of file diff --git a/C++/Copy_Linkedlist_with_Random_Pointers.cpp b/C++/Copy_Linkedlist_with_Random_Pointers.cpp new file mode 100644 index 0000000..aab9549 --- /dev/null +++ b/C++/Copy_Linkedlist_with_Random_Pointers.cpp @@ -0,0 +1,36 @@ +class Node { +public: + int val; + Node* next; + Node* random; + + Node(int _val) { + val = _val; + next = NULL; + random = NULL; + } +}; +class Solution { +public: + Node* copyRandomList(Node* head) + { + unordered_map mp; + Node* copy = nullptr; + Node* original = head; + while(original != NULL) + { + copy = new Node(original->val); + mp[original] = copy; + original= original->next; + } + original = head; + while(original != NULL) + { + copy = mp[original]; + copy->next = mp[original->next]; + copy->random = mp[original->random]; + original = original->next; + } + return mp[head]; + } +}; \ No newline at end of file