From 0888a63fbd4736cbcccc4cb9af4b9af3fefa94a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cpikachu28?= <“anjalisinghgsm@“gmail.com> Date: Mon, 7 Dec 2020 19:12:35 +0530 Subject: [PATCH 1/5] Week_1 Solution --- Week1/Count Negative Nos/Anjali_Singh.cpp | 15 +++++++++ Week1/Count and Say/Anjali_Singh.cpp | 18 ++++++++++ .../Anjali_Singh.cpp | 24 ++++++++++++++ Week1/Max Subarray/Anjali_Singh.cpp | 17 ++++++++++ Week1/Missing no/Anjali_Singh.cpp | 14 ++++++++ Week1/Monotonic Array/Anjali_Singh.cpp | 20 +++++++++++ Week1/Move Zeroes/Anjali_Singh.cpp | 12 +++++++ Week1/Pascal Triangle/Anjali_Singh.cpp | 15 +++++++++ Week1/Pivot Index/Anjali_Singh.cpp | 28 ++++++++++++++++ Week1/Product Of Array/Anjali_Singh.cpp | 20 +++++++++++ Week1/Remove Duplicates/Anjali_Singh.cpp | 20 +++++++++++ Week1/Reverse Only Letters/Anjali_Singh.cpp | 21 ++++++++++++ Week1/Reverse vowels/Anjali_Singh.cpp | 29 ++++++++++++++++ Week1/Rotate Array/Anjali_Singh.cpp | 22 +++++++++++++ Week1/Spiral Matrix/Anjali_Singh.cpp | 33 +++++++++++++++++++ Week1/Target Array/Anjali_Singh.cpp | 13 ++++++++ Week1/Valid Palindrome/Anjali_Singh.cpp | 25 ++++++++++++++ Week1/Xor operation/Anjali_Singh.cpp | 15 +++++++++ 18 files changed, 361 insertions(+) create mode 100644 Week1/Count Negative Nos/Anjali_Singh.cpp create mode 100644 Week1/Count and Say/Anjali_Singh.cpp create mode 100644 Week1/Longest Substring wo repeating chars/Anjali_Singh.cpp create mode 100644 Week1/Max Subarray/Anjali_Singh.cpp create mode 100644 Week1/Missing no/Anjali_Singh.cpp create mode 100644 Week1/Monotonic Array/Anjali_Singh.cpp create mode 100644 Week1/Move Zeroes/Anjali_Singh.cpp create mode 100644 Week1/Pascal Triangle/Anjali_Singh.cpp create mode 100644 Week1/Pivot Index/Anjali_Singh.cpp create mode 100644 Week1/Product Of Array/Anjali_Singh.cpp create mode 100644 Week1/Remove Duplicates/Anjali_Singh.cpp create mode 100644 Week1/Reverse Only Letters/Anjali_Singh.cpp create mode 100644 Week1/Reverse vowels/Anjali_Singh.cpp create mode 100644 Week1/Rotate Array/Anjali_Singh.cpp create mode 100644 Week1/Spiral Matrix/Anjali_Singh.cpp create mode 100644 Week1/Target Array/Anjali_Singh.cpp create mode 100644 Week1/Valid Palindrome/Anjali_Singh.cpp create mode 100644 Week1/Xor operation/Anjali_Singh.cpp diff --git a/Week1/Count Negative Nos/Anjali_Singh.cpp b/Week1/Count Negative Nos/Anjali_Singh.cpp new file mode 100644 index 0000000..a70ad65 --- /dev/null +++ b/Week1/Count Negative Nos/Anjali_Singh.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int countNegatives(vector>& grid) { + int r=grid.size(), c=grid[0].size(); + int count=0; + for(int i=0; imymap; + for(int i=0; i=left){ + left=mymap[s[i]]+1; + mymap[s[i]]=i; + + } + else{ + mymap[s[i]]=i; + } + } + res=max(res,i-left+1); + } + return res; + } +}; diff --git a/Week1/Max Subarray/Anjali_Singh.cpp b/Week1/Max Subarray/Anjali_Singh.cpp new file mode 100644 index 0000000..12c679b --- /dev/null +++ b/Week1/Max Subarray/Anjali_Singh.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int maxSubArray(vector& nums) { + int sum=0, maxSum=INT_MIN; + for(int i=0; imaxSum){ + maxSum=sum; + } + if(sum<0){ + sum=0; + } + } + return maxSum; + + } +}; diff --git a/Week1/Missing no/Anjali_Singh.cpp b/Week1/Missing no/Anjali_Singh.cpp new file mode 100644 index 0000000..1431e5c --- /dev/null +++ b/Week1/Missing no/Anjali_Singh.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int missingNumber(vector& nums) { + sort(nums.begin(),nums.end()); + cout<& A) { + int len = A.size(); + int count1 = 1; + int count2 = 1; + for(int i = 1; i < len; i++){ + if(A[i] >= A[i-1]){ + count1++; + } + if(A[i] <= A[i-1]){ + count2++; + } + } + if(count1 == len or count2 == len){ + return true; + } + return false; + } +}; diff --git a/Week1/Move Zeroes/Anjali_Singh.cpp b/Week1/Move Zeroes/Anjali_Singh.cpp new file mode 100644 index 0000000..6440564 --- /dev/null +++ b/Week1/Move Zeroes/Anjali_Singh.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + void moveZeroes(vector& nums) { + for(int i=0, j=0; i> generate(int numRows) { + vector>ans; + for(int i=0;irow(i+1,1); + for(int j=1;j& nums) { + int leftSum=0, rightSum=0, sum=0; + for(int i=0; i productExceptSelf(vector& nums) { + vector left(nums.size()), right(nums.size()); + left[0] = 1; + right[nums.size()-1] = 1; + for(int i=1; i=0; i--){ + right[i] = right[i+1]*nums[i+1]; + } + for(int i=0; i& nums) { + int n = nums.size(); + if(n==0||n==1){ + return n; + } + int temp[n]; + int j = 0; + for (int i=0; i& nums, int k) { + // while(k!=0){ + // int j = nums[nums.size()-1]; + // for(int i=nums.size()-1; i>0; i--){ + // nums[i] = nums[i-1]; + // } + // nums[0] = j; + // k--; + // } + int n=nums.size(); + int a[n]; + for(int i=0; i spiralOrder(vector>& matrix) { + int r=matrix.size(), c=matrix[0].size(); + vector ans; + int k=0, l=0; + while(k=l; i--){ + ans.push_back(matrix[r-1][i]); + } + r--; + } + if(l=k; i--){ + ans.push_back(matrix[i][l]); + } + l++; + } + } + + return ans; + } +}; diff --git a/Week1/Target Array/Anjali_Singh.cpp b/Week1/Target Array/Anjali_Singh.cpp new file mode 100644 index 0000000..835ce56 --- /dev/null +++ b/Week1/Target Array/Anjali_Singh.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + vector createTargetArray(vector& nums, vector& index) { + vectorans; + for(int i=0; i x; + int a=start, s; + for(int i=0; i Date: Mon, 7 Dec 2020 19:13:57 +0530 Subject: [PATCH 2/5] Revert "Week_1 Solution" This reverts commit 0888a63fbd4736cbcccc4cb9af4b9af3fefa94a7. --- Week1/Count Negative Nos/Anjali_Singh.cpp | 15 --------- Week1/Count and Say/Anjali_Singh.cpp | 18 ---------- .../Anjali_Singh.cpp | 24 -------------- Week1/Max Subarray/Anjali_Singh.cpp | 17 ---------- Week1/Missing no/Anjali_Singh.cpp | 14 -------- Week1/Monotonic Array/Anjali_Singh.cpp | 20 ----------- Week1/Move Zeroes/Anjali_Singh.cpp | 12 ------- Week1/Pascal Triangle/Anjali_Singh.cpp | 15 --------- Week1/Pivot Index/Anjali_Singh.cpp | 28 ---------------- Week1/Product Of Array/Anjali_Singh.cpp | 20 ----------- Week1/Remove Duplicates/Anjali_Singh.cpp | 20 ----------- Week1/Reverse Only Letters/Anjali_Singh.cpp | 21 ------------ Week1/Reverse vowels/Anjali_Singh.cpp | 29 ---------------- Week1/Rotate Array/Anjali_Singh.cpp | 22 ------------- Week1/Spiral Matrix/Anjali_Singh.cpp | 33 ------------------- Week1/Target Array/Anjali_Singh.cpp | 13 -------- Week1/Valid Palindrome/Anjali_Singh.cpp | 25 -------------- Week1/Xor operation/Anjali_Singh.cpp | 15 --------- 18 files changed, 361 deletions(-) delete mode 100644 Week1/Count Negative Nos/Anjali_Singh.cpp delete mode 100644 Week1/Count and Say/Anjali_Singh.cpp delete mode 100644 Week1/Longest Substring wo repeating chars/Anjali_Singh.cpp delete mode 100644 Week1/Max Subarray/Anjali_Singh.cpp delete mode 100644 Week1/Missing no/Anjali_Singh.cpp delete mode 100644 Week1/Monotonic Array/Anjali_Singh.cpp delete mode 100644 Week1/Move Zeroes/Anjali_Singh.cpp delete mode 100644 Week1/Pascal Triangle/Anjali_Singh.cpp delete mode 100644 Week1/Pivot Index/Anjali_Singh.cpp delete mode 100644 Week1/Product Of Array/Anjali_Singh.cpp delete mode 100644 Week1/Remove Duplicates/Anjali_Singh.cpp delete mode 100644 Week1/Reverse Only Letters/Anjali_Singh.cpp delete mode 100644 Week1/Reverse vowels/Anjali_Singh.cpp delete mode 100644 Week1/Rotate Array/Anjali_Singh.cpp delete mode 100644 Week1/Spiral Matrix/Anjali_Singh.cpp delete mode 100644 Week1/Target Array/Anjali_Singh.cpp delete mode 100644 Week1/Valid Palindrome/Anjali_Singh.cpp delete mode 100644 Week1/Xor operation/Anjali_Singh.cpp diff --git a/Week1/Count Negative Nos/Anjali_Singh.cpp b/Week1/Count Negative Nos/Anjali_Singh.cpp deleted file mode 100644 index a70ad65..0000000 --- a/Week1/Count Negative Nos/Anjali_Singh.cpp +++ /dev/null @@ -1,15 +0,0 @@ -class Solution { -public: - int countNegatives(vector>& grid) { - int r=grid.size(), c=grid[0].size(); - int count=0; - for(int i=0; imymap; - for(int i=0; i=left){ - left=mymap[s[i]]+1; - mymap[s[i]]=i; - - } - else{ - mymap[s[i]]=i; - } - } - res=max(res,i-left+1); - } - return res; - } -}; diff --git a/Week1/Max Subarray/Anjali_Singh.cpp b/Week1/Max Subarray/Anjali_Singh.cpp deleted file mode 100644 index 12c679b..0000000 --- a/Week1/Max Subarray/Anjali_Singh.cpp +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - int maxSubArray(vector& nums) { - int sum=0, maxSum=INT_MIN; - for(int i=0; imaxSum){ - maxSum=sum; - } - if(sum<0){ - sum=0; - } - } - return maxSum; - - } -}; diff --git a/Week1/Missing no/Anjali_Singh.cpp b/Week1/Missing no/Anjali_Singh.cpp deleted file mode 100644 index 1431e5c..0000000 --- a/Week1/Missing no/Anjali_Singh.cpp +++ /dev/null @@ -1,14 +0,0 @@ -class Solution { -public: - int missingNumber(vector& nums) { - sort(nums.begin(),nums.end()); - cout<& A) { - int len = A.size(); - int count1 = 1; - int count2 = 1; - for(int i = 1; i < len; i++){ - if(A[i] >= A[i-1]){ - count1++; - } - if(A[i] <= A[i-1]){ - count2++; - } - } - if(count1 == len or count2 == len){ - return true; - } - return false; - } -}; diff --git a/Week1/Move Zeroes/Anjali_Singh.cpp b/Week1/Move Zeroes/Anjali_Singh.cpp deleted file mode 100644 index 6440564..0000000 --- a/Week1/Move Zeroes/Anjali_Singh.cpp +++ /dev/null @@ -1,12 +0,0 @@ -class Solution { -public: - void moveZeroes(vector& nums) { - for(int i=0, j=0; i> generate(int numRows) { - vector>ans; - for(int i=0;irow(i+1,1); - for(int j=1;j& nums) { - int leftSum=0, rightSum=0, sum=0; - for(int i=0; i productExceptSelf(vector& nums) { - vector left(nums.size()), right(nums.size()); - left[0] = 1; - right[nums.size()-1] = 1; - for(int i=1; i=0; i--){ - right[i] = right[i+1]*nums[i+1]; - } - for(int i=0; i& nums) { - int n = nums.size(); - if(n==0||n==1){ - return n; - } - int temp[n]; - int j = 0; - for (int i=0; i& nums, int k) { - // while(k!=0){ - // int j = nums[nums.size()-1]; - // for(int i=nums.size()-1; i>0; i--){ - // nums[i] = nums[i-1]; - // } - // nums[0] = j; - // k--; - // } - int n=nums.size(); - int a[n]; - for(int i=0; i spiralOrder(vector>& matrix) { - int r=matrix.size(), c=matrix[0].size(); - vector ans; - int k=0, l=0; - while(k=l; i--){ - ans.push_back(matrix[r-1][i]); - } - r--; - } - if(l=k; i--){ - ans.push_back(matrix[i][l]); - } - l++; - } - } - - return ans; - } -}; diff --git a/Week1/Target Array/Anjali_Singh.cpp b/Week1/Target Array/Anjali_Singh.cpp deleted file mode 100644 index 835ce56..0000000 --- a/Week1/Target Array/Anjali_Singh.cpp +++ /dev/null @@ -1,13 +0,0 @@ -class Solution { -public: - vector createTargetArray(vector& nums, vector& index) { - vectorans; - for(int i=0; i x; - int a=start, s; - for(int i=0; i Date: Mon, 7 Dec 2020 21:20:28 +0530 Subject: [PATCH 3/5] Week_1_Anjali_Singh --- Week1/Count Negative Nos/Anjali_Singh.cpp | 15 +++++++++ Week1/Count and Say/Anjali_Singh.cpp | 18 ++++++++++ .../Anjali_Singh.cpp | 24 ++++++++++++++ Week1/Max Subarray/Anjali_Singh.cpp | 17 ++++++++++ Week1/Missing no/Anjali_Singh.cpp | 14 ++++++++ Week1/Monotonic Array/Anjali_Singh.cpp | 0 Week1/Move Zeroes/Anjali_Singh.cpp | 12 +++++++ Week1/Pascal Triangle/Anjali_Singh.cpp | 15 +++++++++ Week1/Pivot Index/Anjali_Singh.cpp | 28 ++++++++++++++++ Week1/Product Of Array/Anjali_Singh.cpp | 20 +++++++++++ Week1/Remove Duplicates/Anjali_Singh.cpp | 20 +++++++++++ Week1/Reverse Only Letters/Anjali_Singh.cpp | 21 ++++++++++++ Week1/Reverse vowels/Anjali_Singh.cpp | 29 ++++++++++++++++ Week1/Rotate Array/Anjali_Singh.cpp | 22 +++++++++++++ Week1/Spiral Matrix/Anjali_Singh.cpp | 33 +++++++++++++++++++ Week1/Target Array/Anjali_Singh.cpp | 13 ++++++++ Week1/Valid Palindrome/Anjali_Singh.cpp | 25 ++++++++++++++ Week1/Xor operation/Anjali_Singh.cpp | 15 +++++++++ 18 files changed, 341 insertions(+) create mode 100644 Week1/Count Negative Nos/Anjali_Singh.cpp create mode 100644 Week1/Count and Say/Anjali_Singh.cpp create mode 100644 Week1/Longest Substring wo repeating chars/Anjali_Singh.cpp create mode 100644 Week1/Max Subarray/Anjali_Singh.cpp create mode 100644 Week1/Missing no/Anjali_Singh.cpp create mode 100644 Week1/Monotonic Array/Anjali_Singh.cpp create mode 100644 Week1/Move Zeroes/Anjali_Singh.cpp create mode 100644 Week1/Pascal Triangle/Anjali_Singh.cpp create mode 100644 Week1/Pivot Index/Anjali_Singh.cpp create mode 100644 Week1/Product Of Array/Anjali_Singh.cpp create mode 100644 Week1/Remove Duplicates/Anjali_Singh.cpp create mode 100644 Week1/Reverse Only Letters/Anjali_Singh.cpp create mode 100644 Week1/Reverse vowels/Anjali_Singh.cpp create mode 100644 Week1/Rotate Array/Anjali_Singh.cpp create mode 100644 Week1/Spiral Matrix/Anjali_Singh.cpp create mode 100644 Week1/Target Array/Anjali_Singh.cpp create mode 100644 Week1/Valid Palindrome/Anjali_Singh.cpp create mode 100644 Week1/Xor operation/Anjali_Singh.cpp diff --git a/Week1/Count Negative Nos/Anjali_Singh.cpp b/Week1/Count Negative Nos/Anjali_Singh.cpp new file mode 100644 index 0000000..a70ad65 --- /dev/null +++ b/Week1/Count Negative Nos/Anjali_Singh.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int countNegatives(vector>& grid) { + int r=grid.size(), c=grid[0].size(); + int count=0; + for(int i=0; imymap; + for(int i=0; i=left){ + left=mymap[s[i]]+1; + mymap[s[i]]=i; + + } + else{ + mymap[s[i]]=i; + } + } + res=max(res,i-left+1); + } + return res; + } +}; diff --git a/Week1/Max Subarray/Anjali_Singh.cpp b/Week1/Max Subarray/Anjali_Singh.cpp new file mode 100644 index 0000000..12c679b --- /dev/null +++ b/Week1/Max Subarray/Anjali_Singh.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int maxSubArray(vector& nums) { + int sum=0, maxSum=INT_MIN; + for(int i=0; imaxSum){ + maxSum=sum; + } + if(sum<0){ + sum=0; + } + } + return maxSum; + + } +}; diff --git a/Week1/Missing no/Anjali_Singh.cpp b/Week1/Missing no/Anjali_Singh.cpp new file mode 100644 index 0000000..1431e5c --- /dev/null +++ b/Week1/Missing no/Anjali_Singh.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int missingNumber(vector& nums) { + sort(nums.begin(),nums.end()); + cout<& nums) { + for(int i=0, j=0; i> generate(int numRows) { + vector>ans; + for(int i=0;irow(i+1,1); + for(int j=1;j& nums) { + int leftSum=0, rightSum=0, sum=0; + for(int i=0; i productExceptSelf(vector& nums) { + vector left(nums.size()), right(nums.size()); + left[0] = 1; + right[nums.size()-1] = 1; + for(int i=1; i=0; i--){ + right[i] = right[i+1]*nums[i+1]; + } + for(int i=0; i& nums) { + int n = nums.size(); + if(n==0||n==1){ + return n; + } + int temp[n]; + int j = 0; + for (int i=0; i& nums, int k) { + // while(k!=0){ + // int j = nums[nums.size()-1]; + // for(int i=nums.size()-1; i>0; i--){ + // nums[i] = nums[i-1]; + // } + // nums[0] = j; + // k--; + // } + int n=nums.size(); + int a[n]; + for(int i=0; i spiralOrder(vector>& matrix) { + int r=matrix.size(), c=matrix[0].size(); + vector ans; + int k=0, l=0; + while(k=l; i--){ + ans.push_back(matrix[r-1][i]); + } + r--; + } + if(l=k; i--){ + ans.push_back(matrix[i][l]); + } + l++; + } + } + + return ans; + } +}; diff --git a/Week1/Target Array/Anjali_Singh.cpp b/Week1/Target Array/Anjali_Singh.cpp new file mode 100644 index 0000000..835ce56 --- /dev/null +++ b/Week1/Target Array/Anjali_Singh.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + vector createTargetArray(vector& nums, vector& index) { + vectorans; + for(int i=0; i x; + int a=start, s; + for(int i=0; i Date: Mon, 7 Dec 2020 21:22:08 +0530 Subject: [PATCH 4/5] Update Anjali_Singh.cpp --- Week1/Monotonic Array/Anjali_Singh.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Week1/Monotonic Array/Anjali_Singh.cpp b/Week1/Monotonic Array/Anjali_Singh.cpp index e69de29..a03be7f 100644 --- a/Week1/Monotonic Array/Anjali_Singh.cpp +++ b/Week1/Monotonic Array/Anjali_Singh.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + bool isMonotonic(vector& A) { + int len = A.size(); + int count1 = 1; + int count2 = 1; + for(int i = 1; i < len; i++){ + if(A[i] >= A[i-1]){ + count1++; + } + if(A[i] <= A[i-1]){ + count2++; + } + } + if(count1 == len or count2 == len){ + return true; + } + return false; + } +}; From a5112e9a6dd5a49475504d6368aca6c0962ba31e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cpikachu28?= <“anjalisinghgsm@“gmail.com> Date: Fri, 15 Jan 2021 15:00:11 +0530 Subject: [PATCH 5/5] Week_2_Anjali_Singh --- Miscellaneous/Binary_Search/Anjali_Singh.cpp | 16 +++++ .../Duplicate_Number/Anjali_Singh.cpp | 12 ++++ Miscellaneous/Peek_Index/Anjali_Singh.cpp | 16 +++++ .../BackSpace_String_Compare/Anjali_Singh.cpp | 50 ++++++++++++++++ Stacks/Make_string_great/Anjali_Singh.cpp | 21 +++++++ Stacks/Next_Greater_Element/Anjali_Singh.cpp | 24 ++++++++ .../Anjali_SIngh.cpp | 24 ++++++++ .../Anjali_Singh.cpp | 49 +++++++++++++++ .../Reverse_Polish_Notation/Anjali_Singh.cpp | 32 ++++++++++ Stacks/Valid_Parentheses/Anjali_Singh.cpp | 20 +++++++ .../Anjali_Singh.cpp | 46 +++++++++++++++ Week2/Intersection_of_2_LL/Anjali_Singh.cpp | 59 +++++++++++++++++++ Week2/Linked_List_Cycle/Anjali_Singh.cpp | 33 +++++++++++ Week2/Merge_2_Sorted_List/Anjali_Singh.cpp | 27 +++++++++ .../Middle_of_the_LinkedList/Anjali_Singh.cpp | 22 +++++++ Week2/Odd_Even_LL/Anjali_Singh.cpp | 43 ++++++++++++++ Week2/Palindrome_LinkedList/Anjali_Singh.cpp | 42 +++++++++++++ .../Anjali_Singh.cpp | 25 ++++++++ Week2/Remove_Nth_end_of_List/Anjali_Singh.cpp | 36 +++++++++++ Week2/Reverse_Linked_List/Anjali_Singh.cpp | 24 ++++++++ Week2/Sort_List/Anjali_Singh.cpp | 46 +++++++++++++++ 21 files changed, 667 insertions(+) create mode 100644 Miscellaneous/Binary_Search/Anjali_Singh.cpp create mode 100644 Miscellaneous/Duplicate_Number/Anjali_Singh.cpp create mode 100644 Miscellaneous/Peek_Index/Anjali_Singh.cpp create mode 100644 Stacks/BackSpace_String_Compare/Anjali_Singh.cpp create mode 100644 Stacks/Make_string_great/Anjali_Singh.cpp create mode 100644 Stacks/Next_Greater_Element/Anjali_Singh.cpp create mode 100644 Stacks/Remove_Adjacent_Duplicates_In_String/Anjali_SIngh.cpp create mode 100644 Stacks/Remove_Outermost_Parentheses/Anjali_Singh.cpp create mode 100644 Stacks/Reverse_Polish_Notation/Anjali_Singh.cpp create mode 100644 Stacks/Valid_Parentheses/Anjali_Singh.cpp create mode 100644 Week2/Convert_Binary_Number_LinkedList_to_Integer/Anjali_Singh.cpp create mode 100644 Week2/Intersection_of_2_LL/Anjali_Singh.cpp create mode 100644 Week2/Linked_List_Cycle/Anjali_Singh.cpp create mode 100644 Week2/Merge_2_Sorted_List/Anjali_Singh.cpp create mode 100644 Week2/Middle_of_the_LinkedList/Anjali_Singh.cpp create mode 100644 Week2/Odd_Even_LL/Anjali_Singh.cpp create mode 100644 Week2/Palindrome_LinkedList/Anjali_Singh.cpp create mode 100644 Week2/Remove_Duplicate_from_Sorted_List/Anjali_Singh.cpp create mode 100644 Week2/Remove_Nth_end_of_List/Anjali_Singh.cpp create mode 100644 Week2/Reverse_Linked_List/Anjali_Singh.cpp create mode 100644 Week2/Sort_List/Anjali_Singh.cpp diff --git a/Miscellaneous/Binary_Search/Anjali_Singh.cpp b/Miscellaneous/Binary_Search/Anjali_Singh.cpp new file mode 100644 index 0000000..860743e --- /dev/null +++ b/Miscellaneous/Binary_Search/Anjali_Singh.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int BinarySearch(vectornums, int l, int r, int target){ + if(r>=l){ + int mid = l + (r-l)/2; + if(nums[mid]==target) return mid; + else if(nums[mid]>target) return BinarySearch(nums, l, mid-1, target); + else return BinarySearch(nums, mid+1, r, target); + } + return -1; + } + int search(vector& nums, int target) { + int x = BinarySearch(nums, 0, nums.size()-1, target); + return x; + } +}; diff --git a/Miscellaneous/Duplicate_Number/Anjali_Singh.cpp b/Miscellaneous/Duplicate_Number/Anjali_Singh.cpp new file mode 100644 index 0000000..f5609dd --- /dev/null +++ b/Miscellaneous/Duplicate_Number/Anjali_Singh.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int findDuplicate(vector& nums) { + sort(nums.begin(), nums.end()); + for(int i=0; inums, int l, int r, int target){ + if(r>=l){ + int mid = l + (r-l)/2; + if(nums[mid]==target) return mid; + else if(nums[mid]>target) return BinarySearch(nums, l, mid-1, target); + else return BinarySearch(nums, mid+1, r, target); + } + return -1; + } + int search(vector& nums, int target) { + int x = BinarySearch(nums, 0, nums.size()-1, target); + return x; + } +}; diff --git a/Stacks/BackSpace_String_Compare/Anjali_Singh.cpp b/Stacks/BackSpace_String_Compare/Anjali_Singh.cpp new file mode 100644 index 0000000..980c425 --- /dev/null +++ b/Stacks/BackSpace_String_Compare/Anjali_Singh.cpp @@ -0,0 +1,50 @@ +class Solution { +public: + bool isEqual(stack &stack1, stack &stack2) { + if (stack1.empty() && stack2.empty()) + return true; + else if (stack1.empty() || stack2.empty()) + return false; + while (!stack1.empty() && !stack2.empty()) + { + if (stack1.top() != stack2.top()) + return false; + stack1.pop(); + stack2.pop(); + } + if (stack1.empty() && stack2.empty()) + return true; + else + return false; + } + + bool backspaceCompare(string S, string T) { + stack stack1; + stack stack2; + for(char s:S){ + if(s=='#' && !stack1.empty()){ + // cout< stk; + string ans; + for(char i: s){ + if(!stk.empty() && (i==stk.top()-32||i==stk.top()+32)){ + cout< nextGreaterElement(vector& nums1, vector& nums2) { + + stack st; + unordered_map hashmap; + for(int i=nums2.size()-1;i>=0;i--) + { + while(!st.empty()&&st.top() stk; + string ans; + for(char s: S){ + if(!stk.empty() && s==stk.top()) + { + stk.pop(); + } + else{ + stk.push(s); + } + } + while(!stk.empty()){ + ans.push_back(stk.top()); + cout< st; + string ans; + + + + for(char& c : S) + { + //for opening brackets + // if it is not the outer most bracket then we will take it into ans string and push it to stack for later comparison. + if (c == '(' && st.size()>=1) + { + ans.push_back('('); + st.push('('); + } + // if c is outer most bracket then push it into stack for later comparison + else + { + if (c == '(' && st.size() == 0) + { + st.push('('); + } + } + + + //for closing brackets + + if (c == ')' && st.size()>1) + { + ans.push_back(')'); + st.pop(); + } + else + { + if (c == ')' && st.size() == 1) + { + st.pop(); + } + } + } + + + return ans; + + } +}; diff --git a/Stacks/Reverse_Polish_Notation/Anjali_Singh.cpp b/Stacks/Reverse_Polish_Notation/Anjali_Singh.cpp new file mode 100644 index 0000000..f64a75b --- /dev/null +++ b/Stacks/Reverse_Polish_Notation/Anjali_Singh.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int evalRPN(vector& tokens) { + unordered_set operands = {"+", "-", "*", "/"}; + stack s; + for (string token : tokens) { + // We have a number + if (operands.find(token) == operands.end()) { + s.push(stoi(token)); + } + else { // We have an operand + int b = s.top(); + s.pop(); + int a = s.top(); + s.pop(); + if (token == "+") { + s.push(a + b); + } + else if (token == "-") { + s.push(a - b); + } + else if (token == "*") { + s.push(a * b); + } + else if (token == "/") { + s.push(a / b); + } + } + } + return s.top(); + } +}; diff --git a/Stacks/Valid_Parentheses/Anjali_Singh.cpp b/Stacks/Valid_Parentheses/Anjali_Singh.cpp new file mode 100644 index 0000000..68f80ee --- /dev/null +++ b/Stacks/Valid_Parentheses/Anjali_Singh.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + bool isValid(string s) { + stack stk; + for (char c : s) { + if (c == '(' || c == '{' || c == '[') { + stk.push(c); + continue; + } + // now we've all closing brackets + if (stk.empty()) return false; + + if (c == ')' && stk.top() != '(') return false; + if (c == '}' && stk.top() != '{') return false; + if (c == ']' && stk.top() != '[') return false; + stk.pop(); + } + return stk.empty(); + } +}; diff --git a/Week2/Convert_Binary_Number_LinkedList_to_Integer/Anjali_Singh.cpp b/Week2/Convert_Binary_Number_LinkedList_to_Integer/Anjali_Singh.cpp new file mode 100644 index 0000000..a435a55 --- /dev/null +++ b/Week2/Convert_Binary_Number_LinkedList_to_Integer/Anjali_Singh.cpp @@ -0,0 +1,46 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* MergeList(ListNode* hd1, ListNode* hd2){ + if (hd1==NULL && hd2==NULL) return NULL; + if (hd1==NULL) return hd2; + if (hd2==NULL) return hd1; + if (hd1->valval){ + ///Node* nh=hd1; + hd1->next=MergeList(hd1->next,hd2); + return hd1; + } else { + hd2->next=MergeList(hd1,hd2->next); + return hd2; + } + } + + ListNode* MidSplit(ListNode* hd){ + if (hd==NULL||hd->next==NULL) return hd; + ListNode* fp=hd, *sp=hd; + while (fp->next!=NULL&&fp->next->next!=NULL){ + sp=sp->next; + fp=fp->next->next; + } + ListNode* nh=sp->next; + sp->next=NULL; + return nh; + } + ListNode* sortList(ListNode* head) { + if (head==NULL||head->next==NULL) return head; + ListNode* nh=MidSplit(head); + head=sortList(head); + nh=sortList(nh); + return MergeList(head, nh); + + } +}; diff --git a/Week2/Intersection_of_2_LL/Anjali_Singh.cpp b/Week2/Intersection_of_2_LL/Anjali_Singh.cpp new file mode 100644 index 0000000..0fc5934 --- /dev/null +++ b/Week2/Intersection_of_2_LL/Anjali_Singh.cpp @@ -0,0 +1,59 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + if(headA==NULL||headB==NULL) + return NULL; + if(headB==headA) + return headA; + ListNode *tailA = headA; + ListNode *tailB = headB; + ListNode *tailsA = headA; + ListNode *tailsB = headB; + ListNode *x = NULL; + int sizeA=0, sizeB=0; + for(int i = 0; tailsA!=NULL; i++) + { + sizeA = i; + tailsA = tailsA->next; + } + for(int i = 0; tailsB!=NULL; i++) + { + sizeB = i; + tailsB = tailsB->next; + } + + sizeA++; + sizeB++; + while(tailA!=NULL&&tailB!=NULL) + { + if(sizeA>sizeB){ + x = tailA; + while(x!=NULL){ + if(tailB==x) + return x; + x = x->next; + } + } + else{ + x = tailB; + while(x!=NULL){ + if(tailA==x) + return x; + x = x->next; + } + } + tailB = tailB->next; + tailA = tailA->next; + + } + return NULL; + } +}; diff --git a/Week2/Linked_List_Cycle/Anjali_Singh.cpp b/Week2/Linked_List_Cycle/Anjali_Singh.cpp new file mode 100644 index 0000000..7e19674 --- /dev/null +++ b/Week2/Linked_List_Cycle/Anjali_Singh.cpp @@ -0,0 +1,33 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + bool hasCycle(ListNode *head) { + ListNode* sp=head,*fp=head; + if(head==NULL || head->next==NULL){ + return false; + } + while (true){ + if (fp==NULL || fp->next==NULL){ + return false; + } + sp=sp->next; + fp=fp->next->next; + if (sp==fp) break; + } + // sp=head; + // while (true){ + // sp=sp->next; + // fp=fp->next; + // if (sp==fp) break; + // } + return true; + + } +}; diff --git a/Week2/Merge_2_Sorted_List/Anjali_Singh.cpp b/Week2/Merge_2_Sorted_List/Anjali_Singh.cpp new file mode 100644 index 0000000..aaa9030 --- /dev/null +++ b/Week2/Merge_2_Sorted_List/Anjali_Singh.cpp @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + + ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { + if (l1==NULL && l2==NULL) return NULL; + if (l1==NULL) return l2; + if (l2==NULL) return l1; + if (l1->valval){ + ///Node* nh=hd1; + l1->next=mergeTwoLists(l1->next,l2); + return l1; + } else { + l2->next=mergeTwoLists(l1,l2->next); + return l2; + } + } +}; diff --git a/Week2/Middle_of_the_LinkedList/Anjali_Singh.cpp b/Week2/Middle_of_the_LinkedList/Anjali_Singh.cpp new file mode 100644 index 0000000..fab2ba8 --- /dev/null +++ b/Week2/Middle_of_the_LinkedList/Anjali_Singh.cpp @@ -0,0 +1,22 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* middleNode(ListNode* head) { + ListNode *fp = head; + ListNode *sp = head; + while(fp!=NULL && fp->next!=NULL){ + sp = sp->next; + fp= fp->next->next; + } + return sp; + } +}; diff --git a/Week2/Odd_Even_LL/Anjali_Singh.cpp b/Week2/Odd_Even_LL/Anjali_Singh.cpp new file mode 100644 index 0000000..9bdec07 --- /dev/null +++ b/Week2/Odd_Even_LL/Anjali_Singh.cpp @@ -0,0 +1,43 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* oddEvenList(ListNode* head) { + if(head==NULL) + return NULL; + ListNode* odd = head; + ListNode* even = head->next; + ListNode *evenFirst = even; + + while(true){ + if (!odd || !even || !(even->next)) + { + odd->next = evenFirst; + break; + } + odd->next = even->next; //even's next position will be obivously odd and thus will be become next position + odd = even->next; // moving odd pointer to next odd position + if(odd->next==NULL){ + even->next = NULL; + odd->next = evenFirst; // connecting odd and even LL together + break; + + } + even->next = odd->next; + even = odd->next; + + } + + return head; + } + + +}; diff --git a/Week2/Palindrome_LinkedList/Anjali_Singh.cpp b/Week2/Palindrome_LinkedList/Anjali_Singh.cpp new file mode 100644 index 0000000..a9537b3 --- /dev/null +++ b/Week2/Palindrome_LinkedList/Anjali_Singh.cpp @@ -0,0 +1,42 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + + bool isPalindrome(ListNode* head) { + if(head==NULL || head->next==NULL) return true; + ListNode *temp = reverseList(head); + ListNode *tempA = head; + while(tempA!=NULL && temp!=NULL){ + if(temp->val != tempA->val){ + return false; + } + cout<next->val<<" "; + cout<next->val<<" "; + temp = temp->next; + tempA = tempA->next; + } + return true; + } + + ListNode* reverseList(ListNode* head) { + if(head==NULL) + return NULL; + if(head->next==NULL) + return head; + ListNode *ans = reverseList(head->next); + head->next->next=head; + head->next=NULL; + return ans; + + } + +}; diff --git a/Week2/Remove_Duplicate_from_Sorted_List/Anjali_Singh.cpp b/Week2/Remove_Duplicate_from_Sorted_List/Anjali_Singh.cpp new file mode 100644 index 0000000..c7c92f4 --- /dev/null +++ b/Week2/Remove_Duplicate_from_Sorted_List/Anjali_Singh.cpp @@ -0,0 +1,25 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* deleteDuplicates(ListNode* head) { + ListNode* temp=head; + while(temp!=NULL&&temp->next!=NULL){ + if(temp->val == temp->next->val){ + temp->next = temp->next->next; + } + else{ + temp = temp->next; + } + } + return head; + } +}; diff --git a/Week2/Remove_Nth_end_of_List/Anjali_Singh.cpp b/Week2/Remove_Nth_end_of_List/Anjali_Singh.cpp new file mode 100644 index 0000000..8830995 --- /dev/null +++ b/Week2/Remove_Nth_end_of_List/Anjali_Singh.cpp @@ -0,0 +1,36 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* removeNthFromEnd(ListNode* head, int n) { + ListNode* first = head; + ListNode* second = head; + while(second->next!=NULL){ + if(n>0){ + second = second->next; + n--; + } + else{ + first = first->next; + second = second->next; + } + } + if(n==0){ + first->next = first->next->next; + } + else{ + head=head->next; + } + + return head; + + } +}; diff --git a/Week2/Reverse_Linked_List/Anjali_Singh.cpp b/Week2/Reverse_Linked_List/Anjali_Singh.cpp new file mode 100644 index 0000000..b094ee7 --- /dev/null +++ b/Week2/Reverse_Linked_List/Anjali_Singh.cpp @@ -0,0 +1,24 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* reverseList(ListNode* head) { + if(head==NULL) + return NULL; + if(head->next==NULL) + return head; + ListNode *ans = reverseList(head->next); + head->next->next=head; + head->next=NULL; + return ans; + + } +}; diff --git a/Week2/Sort_List/Anjali_Singh.cpp b/Week2/Sort_List/Anjali_Singh.cpp new file mode 100644 index 0000000..a435a55 --- /dev/null +++ b/Week2/Sort_List/Anjali_Singh.cpp @@ -0,0 +1,46 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* MergeList(ListNode* hd1, ListNode* hd2){ + if (hd1==NULL && hd2==NULL) return NULL; + if (hd1==NULL) return hd2; + if (hd2==NULL) return hd1; + if (hd1->valval){ + ///Node* nh=hd1; + hd1->next=MergeList(hd1->next,hd2); + return hd1; + } else { + hd2->next=MergeList(hd1,hd2->next); + return hd2; + } + } + + ListNode* MidSplit(ListNode* hd){ + if (hd==NULL||hd->next==NULL) return hd; + ListNode* fp=hd, *sp=hd; + while (fp->next!=NULL&&fp->next->next!=NULL){ + sp=sp->next; + fp=fp->next->next; + } + ListNode* nh=sp->next; + sp->next=NULL; + return nh; + } + ListNode* sortList(ListNode* head) { + if (head==NULL||head->next==NULL) return head; + ListNode* nh=MidSplit(head); + head=sortList(head); + nh=sortList(nh); + return MergeList(head, nh); + + } +};