Skip to content

Commit b20eb6c

Browse files
committed
Committed
1 parent e2be022 commit b20eb6c

9 files changed

+269
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class MagicDictionary {
2+
public:
3+
/** Initialize your data structure here. */
4+
MagicDictionary() {
5+
6+
}
7+
8+
/** Build a dictionary through a list of words */
9+
void buildDict(vector<string> dict) {
10+
for(int i=0;i<dict.size();i++)
11+
words.insert(dict[i]);
12+
}
13+
14+
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
15+
bool search(string word) {
16+
17+
for(int i=0;i<word.length();i++){
18+
string temp=word;
19+
for(int j=0;j<26;j++){
20+
temp[i]='a'+ j;
21+
if(words.find(temp)!=words.end() && temp!=word){
22+
return true;
23+
}
24+
}
25+
}
26+
return false;
27+
}
28+
29+
private :
30+
unordered_set<string>words;
31+
};
32+
33+
34+
35+
/**
36+
* Your MagicDictionary object will be instantiated and called as such:
37+
* MagicDictionary obj = new MagicDictionary();
38+
* obj.buildDict(dict);
39+
* bool param_2 = obj.search(word);
40+
*/
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for an interval.
3+
* struct Interval {
4+
* int start;
5+
* int end;
6+
* Interval() : start(0), end(0) {}
7+
* Interval(int s, int e) : start(s), end(e) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
vector<Interval> intervalIntersection(vector<Interval>& A, vector<Interval>& B) {
13+
14+
int i=0;
15+
int j=0;
16+
17+
vector<Interval>ans;
18+
19+
while(i<A.size() && j<B.size()){
20+
int l=max(A[i].start,B[j].start);
21+
int r=min(A[i].end,B[j].end);
22+
23+
if(l<=r)
24+
ans.push_back(Interval(l,r));
25+
26+
if(A[i].end < B[j].end)
27+
i++;
28+
else
29+
j++;
30+
}
31+
32+
return ans;
33+
34+
}
35+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
public:
3+
int findPairs(vector<int>& nums, int k) {
4+
unordered_set<int>s;
5+
unordered_set<int>t;
6+
7+
8+
for(int i=0;i<nums.size();i++){
9+
s.insert(nums[i]);
10+
}
11+
12+
13+
int ans=0;
14+
15+
if(k<0)
16+
return 0;
17+
18+
if(k==0){
19+
unordered_map<int,int>mp;
20+
21+
for(int i=0;i<nums.size();i++){
22+
mp[nums[i]]++;
23+
if(mp[nums[i]]==2)
24+
ans++;
25+
}
26+
return ans;
27+
}
28+
29+
for(auto itr=s.begin();itr!=s.end();itr++){
30+
int c=*itr;
31+
32+
if(t.find(c+k)!=t.end())
33+
ans++;
34+
if(t.find(c-k)!=t.end())
35+
ans++;
36+
t.insert(c);
37+
}
38+
39+
return ans;
40+
}
41+
};

Leetcode/MaxChunksToSortArray1.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int maxChunksToSorted(vector<int>& arr) {
4+
int ans=0;
5+
6+
int last=-1;
7+
int mx=-1;
8+
int mn=0;
9+
10+
for(int i=0;i<arr.size();i++){
11+
mx=max(mx,arr[i]);
12+
13+
14+
if(mx-mn+1 == (i-last)){
15+
ans++;
16+
mn=i+1;
17+
last=i;
18+
}
19+
}
20+
21+
return ans;
22+
}
23+
};

Leetcode/MergeTrees.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
13+
if(!t1 && !t2)
14+
return NULL;
15+
if(t1 && !t2){
16+
TreeNode* r=new TreeNode(t1->val);
17+
r->left=mergeTrees(t1->left,t2);
18+
r->right=mergeTrees(t1->right,t2);
19+
return r;
20+
}
21+
else if(!t1 && t2){
22+
TreeNode* r=new TreeNode(t2->val);
23+
r->left=mergeTrees(t1,t2->left);
24+
r->right=mergeTrees(t1,t2->right);
25+
return r;
26+
}
27+
else{
28+
TreeNode* r=new TreeNode(t1->val+t2->val);
29+
r->left=mergeTrees(t1->left,t2->left);
30+
r->right=mergeTrees(t1->right,t2->right);
31+
return r;
32+
}
33+
return NULL;
34+
}
35+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
13+
vector<int>v;
14+
15+
void helper(TreeNode* root){
16+
if(!root)
17+
return;
18+
helper(root->left);
19+
v.push_back(root->val);
20+
helper(root->right);
21+
}
22+
23+
int getMinimumDifference(TreeNode* root) {
24+
helper(root);
25+
26+
int ans=INT_MAX;
27+
int n=v.size();
28+
29+
for(int i=0;i<n-1;i++){
30+
ans=min(ans,v[i+1]-v[i]);
31+
}
32+
33+
return ans;
34+
}
35+
};

Leetcode/PancakeSorting.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
vector<int> pancakeSort(vector<int>& A) {
4+
vector<int>ans;
5+
6+
for(int i=0;i<A.size();i++){
7+
int mp=max_element(A.begin(),A.end()-i)-A.begin();
8+
ans.push_back(mp+1);
9+
ans.push_back(A.size()-i);
10+
reverse(A.begin(),A.begin()+mp+1);
11+
reverse(A.begin(),A.end()-i);
12+
}
13+
14+
return ans;
15+
}
16+
};

Leetcode/ScoreOfParantheses.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int scoreOfParentheses(string s) {
4+
5+
int ans=0;
6+
int multiplier=0;
7+
8+
for(int i=0;i<s.length();i++){
9+
10+
if(s[i]=='(')
11+
multiplier++;
12+
else{
13+
multiplier--;
14+
if(s[i-1]=='(')
15+
ans+=1 << multiplier;
16+
}
17+
}
18+
19+
return ans;
20+
}
21+
};

Leetcode/SearchInBinaryTree.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
TreeNode* searchBST(TreeNode* root, int val) {
13+
if(!root)
14+
return NULL;
15+
16+
if(root->val > val)
17+
return searchBST(root->left,val);
18+
if(root->val < val)
19+
return searchBST(root->right,val);
20+
21+
return root;
22+
}
23+
};

0 commit comments

Comments
 (0)