Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions 415. Add Strings.cpp
Original file line number Diff line number Diff line change
@@ -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<int> 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;
}
};
18 changes: 18 additions & 0 deletions 520. Detect Capital.cpp
Original file line number Diff line number Diff line change
@@ -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<word.length();i++){
if(isCap == 0 && word[i]<=93) return false;
if(isCap == 1 && word[i]<=93) return false;
if(isCap == 2 && word[i]>=97) return false;
}
return true;
}
};
29 changes: 29 additions & 0 deletions 530. Minimum Absolute Difference in BST.cpp
Original file line number Diff line number Diff line change
@@ -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<int> &v){
if(root == NULL) return;
inorder(root->left,v);
v.push_back(root->val);
inorder(root->right,v);
}
int getMinimumDifference(TreeNode* root) {
vector<int> v;
inorder(root,v);
int diff =INT_MAX;
for(int i=1;i<v.size();i++) diff=min(diff, v[i]-v[i-1]);
return diff;
}
};
16 changes: 16 additions & 0 deletions 532. K-diff Pairs in an Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// https://leetcode.com/problems/k-diff-pairs-in-an-array/
class Solution {
public:
int findPairs(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
unordered_set<int> uset;
set<pair<int, int>> 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();
}
};
38 changes: 38 additions & 0 deletions 701. Insert into a Binary Search Tree.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};
25 changes: 25 additions & 0 deletions 733. Flood Fill.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// https://leetcode.com/problems/flood-fill/
class Solution {
public:
void dfs(vector<vector<int>>& 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<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int color = image[sr][sc];
if (color != newColor) {
dfs(image, sr, sc, color, newColor);
}
return image;
}

};