Skip to content

Commit e840d4d

Browse files
committed
Practise 05-Jun-2020
1 parent aae58d7 commit e840d4d

File tree

5 files changed

+259
-0
lines changed

5 files changed

+259
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Write a function that reverses a string. The input string is given as an array of characters char[].
2+
3+
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
4+
5+
You may assume all the characters consist of printable ascii characters.
6+
7+
8+
9+
Example 1:
10+
11+
Input: ["h","e","l","l","o"]
12+
Output: ["o","l","l","e","h"]
13+
Example 2:
14+
15+
Input: ["H","a","n","n","a","h"]
16+
Output: ["h","a","n","n","a","H"]
17+
Hide Hint #1
18+
The entire logic for reversing a string is based on using the opposite directional two-pointer approach!
19+
20+
21+
22+
23+
class Solution {
24+
public:
25+
void reverseString(vector<char>& s) {
26+
int n=s.size();
27+
for(int i=0,j=n-1;i<n/2;i++,j--){
28+
char tmp=s[i];
29+
s[i]=s[j];
30+
s[j]=tmp;
31+
}
32+
}
33+
34+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Share
2+
Invert a binary tree.
3+
4+
Example:
5+
6+
Input:
7+
8+
4
9+
/ \
10+
2 7
11+
/ \ / \
12+
1 3 6 9
13+
Output:
14+
15+
4
16+
/ \
17+
7 2
18+
/ \ / \
19+
9 6 3 1
20+
Trivia:
21+
This problem was inspired by this original tweet by Max Howell:
22+
23+
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.
24+
25+
26+
27+
28+
29+
/**
30+
* Definition for a binary tree node.
31+
* struct TreeNode {
32+
* int val;
33+
* TreeNode *left;
34+
* TreeNode *right;
35+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
36+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
37+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
38+
* };
39+
*/
40+
class Solution {
41+
public:
42+
TreeNode* invertTree(TreeNode* root) {
43+
if(!root) return root;
44+
queue<TreeNode*>q;
45+
q.push(root);
46+
while(!q.empty()){
47+
TreeNode* cur = q.front();
48+
q.pop();
49+
swap(cur->left, cur->right);
50+
if(cur->left) q.push(cur->left);
51+
if(cur->right) q.push(cur->right);
52+
}
53+
return root;
54+
}
55+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Write a function that reverses a string. The input string is given as an array of characters char[].
2+
3+
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
4+
5+
You may assume all the characters consist of printable ascii characters.
6+
7+
8+
9+
Example 1:
10+
11+
Input: ["h","e","l","l","o"]
12+
Output: ["o","l","l","e","h"]
13+
Example 2:
14+
15+
Input: ["H","a","n","n","a","h"]
16+
Output: ["h","a","n","n","a","H"]
17+
18+
19+
20+
21+
22+
class Solution {
23+
public:
24+
void reverseString(vector<char>& s) {
25+
int n=s.size();
26+
for(int i=0,j=n-1;i<n/2;i++,j--){
27+
char tmp=s[i];
28+
s[i]=s[j];
29+
s[j]=tmp;
30+
}
31+
}
32+
33+
};

stack/min_stack_O(1).cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include<iostream>
2+
#include<stack>
3+
using namespace std;
4+
void push(int a);
5+
bool isFull(int n);
6+
bool isEmpty();
7+
int pop();
8+
int getMin();
9+
10+
11+
stack<int> s;
12+
int main(){
13+
int t;
14+
cin>>t;
15+
while(t--){
16+
int n,a;
17+
cin>>n;
18+
while(!isEmpty()){
19+
pop();
20+
}
21+
while(!isFull(n)){
22+
cin>>a;
23+
push(a);
24+
}
25+
cout<<getMin()<<endl;
26+
}
27+
}
28+
29+
int minx;
30+
31+
void push(int x)
32+
{
33+
if(s.empty()){
34+
s.push(x);
35+
minx=x;
36+
} else{
37+
if(x>=minx) s.push(x);
38+
else {
39+
s.push(2*x-minx);
40+
minx=x;
41+
}
42+
}
43+
}
44+
45+
bool isFull(int n)
46+
{
47+
if(s.size()==n) return true;
48+
else return false;
49+
}
50+
51+
bool isEmpty()
52+
{
53+
return s.empty();
54+
}
55+
56+
int pop()
57+
{
58+
if(s.empty()) return -1;
59+
if(s.top() >= minx){ int tmp=s.top(); s.pop(); return tmp; }
60+
else {
61+
int tmp=minx;
62+
minx=2*minx-s.top();
63+
s.pop();
64+
return tmp;
65+
}
66+
}
67+
68+
int getMin()
69+
{
70+
if(s.empty()) return -1;
71+
else return minx;
72+
}
73+
74+
75+
int getTop(){
76+
if(s.empty()) return -1;
77+
if(s.top()>= minx) return s.top();
78+
else return minx;
79+
}

stack/min_stack_extra_space.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include<iostream>
2+
#include<stack>
3+
using namespace std;
4+
void push(int a);
5+
bool isFull(int n);
6+
bool isEmpty();
7+
int pop();
8+
int getMin();
9+
10+
11+
stack<int> s;
12+
int main(){
13+
int t;
14+
cin>>t;
15+
while(t--){
16+
int n,a;
17+
cin>>n;
18+
while(!isEmpty()){
19+
pop();
20+
}
21+
while(!isFull(n)){
22+
cin>>a;
23+
push(a);
24+
}
25+
cout<<getMin()<<endl;
26+
}
27+
}
28+
29+
stack<int>ss;
30+
31+
void push(int a)
32+
{
33+
s.push(a);
34+
if(ss.empty() || ss.top() >= a) ss.push(a);
35+
}
36+
37+
bool isFull(int n)
38+
{
39+
if(s.size()==n) return true;
40+
else return false;
41+
}
42+
43+
bool isEmpty()
44+
{
45+
return s.empty();
46+
}
47+
48+
int pop()
49+
{ if(s.empty()) return -1;
50+
if(s.top()==ss.top()) ss.pop();
51+
s.pop();
52+
}
53+
54+
int getMin()
55+
{
56+
if(!s.empty()) return ss.top();
57+
else return -1;
58+
}

0 commit comments

Comments
 (0)