diff --git a/Hashing/2Sum.cpp b/Hashing/2Sum.cpp new file mode 100644 index 0000000..8f8f02b --- /dev/null +++ b/Hashing/2Sum.cpp @@ -0,0 +1,21 @@ +/*Given an array of integers, find two numbers such that they add up to a specific target number. + +The function twoSum should return indices of the two numbers such that they add up to the target, where index1 < index2. Please note that your returned answers (both index1 and index2 ) are not zero-based. +Put both these numbers in order in an array and return the array from your function ( Looking at the function signature will make things clearer ). Note that, if no pair exists, return empty list. + +If multiple solutions exist, output the one where index2 is minimum. If there are multiple solutions with the minimum index2, choose the one with minimum index1 out of them.*/ + +vector Solution::twoSum(const vector &A, int B) { + map Hash; + vector ans; + int n = A.size(); + for(int i=0;i > Solution::fourSum(vector &A, int B) { + int n = A.size(); + sort(A.begin(),A.end()); + int i,j; + unordered_map> hash; + vector> Ans; + vectortemp; + + + for(i=0;ip = hash[B-sum]; + if(p.first != i && p.first != j && p.second != i && p.second != j){ + temp.push_back(A[i]); + temp.push_back(A[j]); + temp.push_back(A[p.first]); + temp.push_back(A[p.second]); + sort(temp.begin(),temp.end()); + Ans.push_back(temp); + temp.clear(); + } + } + } + } + sort(Ans.begin(), Ans.end()); + Ans.erase(unique(Ans.begin(), Ans.end()), Ans.end()); + return Ans; +} diff --git a/Hashing/fraction.cpp b/Hashing/fraction.cpp new file mode 100644 index 0000000..665d943 --- /dev/null +++ b/Hashing/fraction.cpp @@ -0,0 +1,48 @@ +/*Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. + +If the fractional part is repeating, enclose the repeating part in parentheses. + +Example : + +Given numerator = 1, denominator = 2, return "0.5" +Given numerator = 2, denominator = 1, return "2" +Given numerator = 2, denominator = 3, return "0.(6)"*/ + + +string Solution::fractionToDecimal(int A, int B) { + int64_t n = A, d = B; + + if(n==0) return "0"; + + string result = ""; + + if(n<0 ^ d<0) result += '-'; + + n = abs(n); + d = abs(d); + + result += to_string((n/d)); + + if(n%d == 0) return result; + + result += '.'; + + unordered_map Hash; + + for(int64_t r=n%d; r; r %= d){ + + if(Hash.find(r)!=Hash.end()){ + result.insert(Hash[r],1,'('); + result += ')'; + break; + } + + Hash[r] = result.size(); + + r *= 10; + + result += (char)('0'+(r/d)); + + } + return result; +} diff --git a/Tree Data Structure/Inorder_Traversal.cpp b/Tree Data Structure/Inorder_Traversal.cpp new file mode 100644 index 0000000..8b08608 --- /dev/null +++ b/Tree Data Structure/Inorder_Traversal.cpp @@ -0,0 +1,22 @@ +/*Given a binary tree, return the inorder traversal of its nodes’ values*/ + +vector Solution::inorderTraversal(TreeNode* A) { + vector Answer; + stack s; + if(!A) return Answer; + + while(A!=NULL || !s.empty()){ + + while(A!=NULL){ + s.push(A); + A = A->left; + } + + A = s.top(); + s.pop(); + Answer.push_back(A->val); + + A = A->right; + } + return Answer; +} diff --git a/Tree Data Structure/Postorder_Traversal.cpp b/Tree Data Structure/Postorder_Traversal.cpp new file mode 100644 index 0000000..ac6e0f6 --- /dev/null +++ b/Tree Data Structure/Postorder_Traversal.cpp @@ -0,0 +1,21 @@ +/*Given a binary tree, return the postorder traversal of its nodes’ values.*/ + + +vector Solution::postorderTraversal(TreeNode* A) { + vector Answer; + stack stac; + if(!A)return Answer; + stac.push(A); + + while(!stac.empty()){ + struct TreeNode * nod = stac.top(); + Answer.push_back(nod -> val); + stac.pop(); + if(nod -> left) stac.push(nod->left); + if(nod -> right) stac.push(nod->right); + + } + reverse(Answer.begin(),Answer.end()); + return Answer; + +} diff --git a/Tree Data Structure/Preorder_Traversal.cpp b/Tree Data Structure/Preorder_Traversal.cpp new file mode 100644 index 0000000..d0186f0 --- /dev/null +++ b/Tree Data Structure/Preorder_Traversal.cpp @@ -0,0 +1,22 @@ +/*Given a binary tree, return the preorder traversal of its nodes’ values.*/ + +vector Solution::preorderTraversal(TreeNode* A){ + vector Answer; + stack stac; + if(!A) return Answer; + + stac.push(A); + while(stac.empty()==false){ + struct TreeNode* TreeNod = stac.top(); + Answer.push_back(TreeNod -> val); + stac.pop(); + + if(TreeNod -> right) + stac.push(TreeNod -> right); + if(TreeNod -> left) + stac.push(TreeNod -> left); + } + return Answer; + + +}