From 78c43dbc3f45f6dd54e47fc46e959c57e20bd096 Mon Sep 17 00:00:00 2001 From: Surya Prakash Date: Tue, 6 Oct 2020 19:40:33 +0530 Subject: [PATCH] leetcode problem added --- 415. Add Strings.cpp | 29 ++++++++++++++++ 520. Detect Capital.cpp | 18 ++++++++++ 530. Minimum Absolute Difference in BST.cpp | 29 ++++++++++++++++ 532. K-diff Pairs in an Array.cpp | 16 +++++++++ 701. Insert into a Binary Search Tree.cpp | 38 +++++++++++++++++++++ 733. Flood Fill.cpp | 25 ++++++++++++++ 6 files changed, 155 insertions(+) create mode 100644 415. Add Strings.cpp create mode 100644 520. Detect Capital.cpp create mode 100644 530. Minimum Absolute Difference in BST.cpp create mode 100644 532. K-diff Pairs in an Array.cpp create mode 100644 701. Insert into a Binary Search Tree.cpp create mode 100644 733. Flood Fill.cpp diff --git a/415. Add Strings.cpp b/415. Add Strings.cpp new file mode 100644 index 0000000..a62fe4e --- /dev/null +++ b/415. Add Strings.cpp @@ -0,0 +1,29 @@ +// https://leetcode.com/problems/add-strings/ +class Solution { +public: + string addStrings(string num1, string num2) { + int i = num1.length()-1; + int j = num2.length()-1; + vector v; + int carry = 0; + while(i>=0 || j>=0){ + int sum = carry; + if(i>=0){ + sum += num1[i--] -'0'; + } + if(j>=0){ + sum += num2[j--] -'0'; + } + v.push_back(sum%10); + carry = sum/10; + } + if(carry!=0){ + v.push_back(carry); + } + string s=""; + for(int i=v.size()-1;i>=0;i--){ + s+=to_string(v[i]); + } + return s; + } +}; \ No newline at end of file diff --git a/520. Detect Capital.cpp b/520. Detect Capital.cpp new file mode 100644 index 0000000..9c549a4 --- /dev/null +++ b/520. Detect Capital.cpp @@ -0,0 +1,18 @@ +// https://leetcode.com/problems/detect-capital/ +class Solution { +public: + bool detectCapitalUse(string word) { + if(word.length()<2) return true; + int isCap = 0; + if(word[0]>=97 && word[1]>=97) isCap = 0; + else if(word[0]<=93 && word[1]>=97) isCap = 1; + else if(word[0]<=93 && word[1]<=93) isCap = 2; + else return false; + for(int i=2;i=97) return false; + } + return true; + } +}; \ No newline at end of file diff --git a/530. Minimum Absolute Difference in BST.cpp b/530. Minimum Absolute Difference in BST.cpp new file mode 100644 index 0000000..54c1271 --- /dev/null +++ b/530. Minimum Absolute Difference in BST.cpp @@ -0,0 +1,29 @@ +// https://leetcode.com/problems/minimum-absolute-difference-in-bst/ + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + void inorder(TreeNode* root, vector &v){ + if(root == NULL) return; + inorder(root->left,v); + v.push_back(root->val); + inorder(root->right,v); + } + int getMinimumDifference(TreeNode* root) { + vector v; + inorder(root,v); + int diff =INT_MAX; + for(int i=1;i& nums, int k) { + sort(nums.begin(), nums.end()); + unordered_set uset; + set> s; + int count =0; + for(int num : nums){ + if(uset.find(num-k) != uset.end()) s.insert({num-k, num}); + uset.insert(num); + } + + return s.size(); + } +}; \ No newline at end of file diff --git a/701. Insert into a Binary Search Tree.cpp b/701. Insert into a Binary Search Tree.cpp new file mode 100644 index 0000000..8e14508 --- /dev/null +++ b/701. Insert into a Binary Search Tree.cpp @@ -0,0 +1,38 @@ +// https://leetcode.com/problems/insert-into-a-binary-search-tree/ + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + TreeNode* insertIntoBST(TreeNode* root, int val) { + TreeNode * newNode= new TreeNode(val); + auto ptr = root; + if(root == NULL) return newNode; + while(ptr){ + if(ptr->val > val){ + if(!ptr->left){ + ptr->left = newNode; + return root; + } + ptr = ptr->left; + } + else{ + if(!ptr->right){ + ptr->right = newNode; + return root; + } + ptr = ptr->right; + } + } + return root; + } +}; \ No newline at end of file diff --git a/733. Flood Fill.cpp b/733. Flood Fill.cpp new file mode 100644 index 0000000..b5b6e37 --- /dev/null +++ b/733. Flood Fill.cpp @@ -0,0 +1,25 @@ +// https://leetcode.com/problems/flood-fill/ +class Solution { +public: + void dfs(vector>& image, int r, int c, int color, int newColor) { + if(r<0 || c<0 || r >image.size()-1 || c >image[0].size()-1){ + return; + } + + if(image[r][c] != color) return; + + image[r][c] = newColor; + dfs(image, r-1, c, color, newColor); + dfs(image, r, c-1, color, newColor); + dfs(image, r+1, c, color, newColor); + dfs(image, r, c+1, color, newColor); + } + vector> floodFill(vector>& image, int sr, int sc, int newColor) { + int color = image[sr][sc]; + if (color != newColor) { + dfs(image, sr, sc, color, newColor); + } + return image; + } + +}; \ No newline at end of file