Skip to content

Commit b8d5182

Browse files
committed
done adding 255 question solution
1 parent 85aa209 commit b8d5182

File tree

255 files changed

+6849
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

255 files changed

+6849
-0
lines changed

01.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
vector<int> twoSum(vector<int>& nums, int target){
4+
for(int i=0;i<nums.size()-1;i++){
5+
for(int j=i+1;j<nums.size();j++){
6+
if(nums[i]+nums[j]==target){
7+
return {i,j};
8+
}
9+
}
10+
}
11+
return {};
12+
}
13+
14+
int main(){
15+
vector<int> nums({2,7,10,15});
16+
twoSum(nums,9);
17+
return 0;
18+
}
19+
// this code is done by mohit kumar

100.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include<bits/stdc++.h>
2+
/*some of the ide does not support the upper include so the below code is important*/
3+
#include<iostream>
4+
using namespace std;
5+
6+
int same(TreeNode*p , TreeNode*q){
7+
if(!p && !q){
8+
return true;
9+
}
10+
if(p && q && p->val == q->val){
11+
return same(p->left,q->left) && same(p->right, q->right);
12+
}
13+
}
14+
15+
int main()
16+
{
17+
return 0;
18+
}

1002.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include<bits/stdc++.h>
2+
#include<iostream>
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
cout<<"hello world"<<endl;
9+
return 0;
10+
}

1002.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# // print the common word in the list
2+
# leetcode dialy challenge
3+
def common(words):
4+
res = []
5+
for char in set(words[0]):
6+
# here we are including the minimum occurance of the character
7+
# like if b is not present in the third word then it min occurance will be 0
8+
res += char*min([word.count(char) for word in words])
9+
print(res)
10+
lst=["bella","label","roller"]
11+
common(lst)
12+
13+
14+

1004.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution{
5+
public:
6+
int longestOnes(vector<int> & nums, int k){
7+
int left=0,right=0;
8+
int maxlen = 0;
9+
int zeros=0;
10+
int len = 0;
11+
while(right<nums.size()){
12+
if(nums[right]==0){zeros++;}
13+
if(zeros>k){
14+
if(nums[left]==0){
15+
zeros--;
16+
}
17+
left++;
18+
}
19+
if(zeros<=k){
20+
len = right-left+1;
21+
maxlen = max(len , maxlen);
22+
}
23+
right ++;
24+
}
25+
return maxlen;
26+
27+
}
28+
}
29+
30+
int main(){
31+
return 0;
32+
}
33+

1004_another.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int longestOnes(vector<int> &nums, int k) {
5+
int left = 0, right = 0, count = k, maxLen = 0;
6+
7+
while (right < nums.size()) {
8+
if (nums[right] == 0) {
9+
if (count > 0) {
10+
count--;
11+
right++;
12+
} else {
13+
maxLen = max(maxLen, right - left);
14+
while (nums[left] == 1) {
15+
left++;
16+
}
17+
left++;
18+
right++;
19+
}
20+
} else {
21+
right++;
22+
}
23+
return max(maxLen, right - left);
24+
}
25+
}
26+
27+
int main() { return 0; }

1008.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
TreeNode* bstFromPreorder(vector<int>& preorder){
5+
int index = 0;
6+
return helper(preorder, index, INT_MAX);
7+
}
8+
TreeNode* helper(vector<int>& preorder, int& index, int bound){
9+
if(index>=preorder.size() || preorder[index]>bound){
10+
return nullptr;
11+
}
12+
int rootVal = preorder[index++];
13+
TreeNode* node = new TreeNode(rootVal);
14+
node->left = helper(preorder, index, rootVal);
15+
node->right = helper(preorder,index, bound);
16+
return node;
17+
}
18+
19+
20+
int main()
21+
{
22+
return 0;
23+
}

101.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
5+
class Solution {
6+
public:
7+
bool isSymmetric(TreeNode* root) {
8+
if(root==NULL) return true;
9+
return mirror(root->left, root->right);
10+
}
11+
bool mirror(TreeNode* left, TreeNode* right){
12+
if(left==nullptr && right==nullptr){
13+
return true;
14+
}
15+
if(left==nullptr || right==nullptr){
16+
return false;
17+
}
18+
return (left->val == right->val) && mirror(left->left, right->right) && mirror(left->right, right->left);
19+
}
20+
21+
};
22+
23+
int main()
24+
{
25+
return 0;
26+
}

102.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<vector<int>> levelorder(TreeNode* root){
5+
vector<vector<int>> ans;
6+
if(root==NULL){
7+
return ans;
8+
}
9+
queue<TreeNode*> q;
10+
q.push(root);
11+
while(!q.empty()){
12+
int size= q.size();
13+
vector<int> level;
14+
for(int i =0;i<size;i++){
15+
TreeNode* temp = q.front();
16+
q.pop();
17+
level.push_back(temp->val);
18+
if(temp->left!=NULL){
19+
q.push(temp->left);
20+
}
21+
if(temp->right!=NULL){
22+
q.push(temp->right);
23+
}
24+
}
25+
ans.push_back(level);
26+
}
27+
return ans;
28+
}
29+
30+
int main()
31+
{
32+
return 0;
33+
}

103.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<vector<int>> zigzagLevelOrder(TreeNode* root){
5+
vector<vector<int>> res;
6+
if(root == nullptr){
7+
return ans;
8+
}
9+
queue<TreeNode*> q;
10+
q.push(root);
11+
while(!q.empty()){
12+
int s = q.size();
13+
vector<int> level;
14+
for(int i = 0;i<s;i++){
15+
TreeNode* node = q.front();
16+
q.pop();
17+
level.push_back(node->val);
18+
if(node->left){
19+
q.push(node->left);
20+
}
21+
if(node->right){
22+
q.push(node->right);
23+
}
24+
}
25+
if(ans.size()%2){
26+
reverse(level.begin(), level.end());
27+
}
28+
ans.push_back(level);
29+
}
30+
}
31+
32+
int main()
33+
{
34+
return 0;
35+
}

104.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
/**
4+
* Definition for a binary tree node.
5+
* struct TreeNode {
6+
* int val;
7+
* TreeNode *left;
8+
* TreeNode *right;
9+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
10+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
11+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
12+
* };
13+
*/
14+
class Solution {
15+
public:
16+
vector<vector<int>> levelorder(TreeNode* root){
17+
vector<vector<int>> ans;
18+
if(root == nullptr){
19+
return ans;
20+
}
21+
queue<TreeNode*> q;
22+
q.push(root);
23+
while(!q.empty())
24+
{
25+
vector<int> level;
26+
int s = q.size();
27+
for(int i =0;i<s;i++){
28+
TreeNode* temp = q.front();
29+
q.pop();
30+
level.push_back(temp->val);
31+
if(temp->left!=NULL){q.push(temp->left);}
32+
if(temp->right!=NULL){q.push(temp->right);}
33+
}
34+
ans.push_back(level);
35+
}
36+
return ans;
37+
}
38+
int maxDepth(TreeNode* root) {
39+
vector<vector<int>> res = levelorder(root);
40+
return res.size();
41+
42+
}
43+
// You can also do
44+
int maxDepth(TreeNode* root){
45+
if(root==nullptr){
46+
return 0;
47+
}
48+
return 1+max(maxDepth(root->left),maxDepth(root->right));
49+
}
50+
};
51+
int main()
52+
{
53+
return 0;
54+
}

1047.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
string removeDuplicate(string s){
6+
stack<char> st;
7+
string ans;
8+
for(int i=0;i<s.size();i++){
9+
if(st.empty() || s[i]!=st.top()){
10+
st.push(s[i]);
11+
}
12+
else{
13+
st.pop();
14+
}
15+
}
16+
while(!st.empty()){
17+
ans+=st.top();
18+
st.pop();
19+
}
20+
reverse(ans.begin(), ans.end());
21+
return ans;
22+
}
23+
24+
int main()
25+
{
26+
string s= "abbaca";
27+
removeDuplicate(s);
28+
return 0;
29+
}

105.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder){
5+
int n = preorder.size();
6+
int index= 0;
7+
return helper(preorder,inorder,0,n-1,index);
8+
9+
}
10+
TreeNode* helper(vector<int>& preorder, vector<int>& inorder, int start, int end, int& index)
11+
{
12+
if(start>end){
13+
return NULL;
14+
}
15+
int rootVal = preorder[index];
16+
int i = start;
17+
for(;i<=n;i++){
18+
if(inorder[i]==rootVal){
19+
break;
20+
}
21+
}
22+
index++;
23+
TreeNode* root = new TreeNode(rootVal);
24+
root->left = helper(preorder,inorder,start,i-1,index);
25+
root->right = helper(preorder,inorder,i+1,end,index);
26+
27+
return root;
28+
}
29+
30+
int main()
31+
{
32+
return 0;
33+
}

1051.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int heightChecker(vector<int> & heights){
6+
int count =0;
7+
int n = heights.size();
8+
vector<int> check=heights;
9+
sort(check.begin(),check.end());
10+
for(int i =0;i<heights.size();i++){
11+
if(check[i]==heights[i]){
12+
count++;
13+
}
14+
}
15+
return n-count;
16+
17+
}
18+
19+
int main()
20+
{
21+
vector<int> heights({1,1,4,2,1,3});
22+
int res = heightChecker(heights);
23+
cout<<res;
24+
return 0;
25+
}
26+
27+

0 commit comments

Comments
 (0)