Skip to content

Commit c63cc4a

Browse files
committedApr 18, 2020
Initial commit
1 parent 8533395 commit c63cc4a

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed
 

‎LeetCode/200-NumberPOfIslands.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
public:
3+
bool isSafe(int n, int m, int i, int j){
4+
if(i<0 || i>=n){
5+
return false;
6+
}
7+
if(j<0 || j>=m){
8+
return false;
9+
}
10+
return true;
11+
}
12+
13+
int numIslands(vector<vector<char>>& grid) {
14+
if(grid.empty() || grid[0].empty()){
15+
return 0;
16+
}
17+
int H = grid.size();
18+
int W = grid[0].size();
19+
int answer = 0;
20+
21+
vector<pair<int, int>> directions{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
22+
23+
for(int row = 0; row < H; row++){
24+
for(int col = 0; col < W; col++){
25+
if(grid[row][col] == '1'){
26+
answer++;
27+
grid[row][col] = 0;
28+
queue<pair<int, int>> q;
29+
q.push({row, col});
30+
while(!q.empty()){
31+
pair<int, int> p = q.front();
32+
q.pop();
33+
for(pair<int, int> dir : directions){
34+
int new_row = p.first + dir.first;
35+
int new_col = p.second + dir.second;
36+
if(isSafe(H, W, new_row, new_col) && grid[new_row][new_col] == '1'){
37+
q.push({new_row, new_col});
38+
grid[new_row][new_col] = 0;
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}
45+
return answer;
46+
}
47+
};
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
bool checkValidString(string s) {
4+
int balance = 0, stars = 0;
5+
int n = s.size();
6+
for(int i=0; i<n; i++){
7+
if(s[i] == '(' || s[i] == '*'){
8+
balance++;
9+
}else if(--balance == -1){
10+
return false;
11+
}
12+
}
13+
balance = 0;
14+
for(int i=n-1; i>=0; i--){
15+
if(s[i] == ')' || s[i] == '*'){
16+
balance++;
17+
}else if(--balance == -1){
18+
return false;
19+
}
20+
}
21+
return true;
22+
}
23+
};
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//author @Nishant
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
int prec(char opr){
7+
if (opr == '+' || opr == '-')
8+
return 1;
9+
else if (opr == '*' || opr == '/')
10+
return 2;
11+
else if (opr == '^')
12+
return 3;
13+
else
14+
return -1;
15+
}
16+
17+
string infixToPostfix(const std::string &exp){
18+
string postfix;
19+
stack<char> stk;
20+
21+
for (int i = 0; exp[i]; i++){
22+
if (exp[i] >= 'a' && exp[i] <= 'z' || exp[i] >= 'A' && exp[i] <= 'Z'){
23+
postfix.push_back(exp[i]);
24+
}else if (exp[i] == '('){
25+
stk.push('(');
26+
}else if (exp[i] == ')'){
27+
while (!stk.empty() && stk.top() != '('){
28+
postfix.push_back(stk.top());
29+
stk.pop();
30+
}
31+
if (!stk.empty())
32+
stk.pop();
33+
else{
34+
cerr << "Invalid infix expression: " << exp << endl;
35+
exit(1);
36+
}
37+
}else{
38+
while (!exp.empty() && prec(exp[i]) <= prec(stk.top())){
39+
postfix.push_back(stk.top());
40+
stk.pop();
41+
}
42+
stk.push(exp[i]);
43+
}
44+
}
45+
46+
while (!stk.empty()){
47+
postfix.push_back(stk.top());
48+
stk.pop();
49+
}
50+
51+
return postfix;
52+
}
53+
54+
int main()
55+
{
56+
57+
int i;
58+
string s;
59+
cin >> i;
60+
61+
while (i--)
62+
{
63+
cin >> s;
64+
cout << infixToPostfix(s) << endl;
65+
}
66+
67+
return 0;
68+
}

0 commit comments

Comments
 (0)