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
Binary file added .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
"_UNICODE",
"__SIZE_TYPE__=long long unsigned int"
],
"intelliSenseMode": "gcc-x64"
}
Expand Down
1 change: 1 addition & 0 deletions AlgorithmCode
Submodule AlgorithmCode added at 7f0f0f
61 changes: 61 additions & 0 deletions IsPopOrder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <iostream>
#include <vector>
#include <stack>
using namespace std;

bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
bool res = false;

if (!pushed.empty() && !popped.empty())
{
int push = 0;
int pop = 0;
int lens = popped.size();
stack<int> tmpStack;

while (pop < lens)
{
while (tmpStack.empty() || tmpStack.top() != popped[pop])
{
if (push == lens)
{
break;
}
tmpStack.push(pushed[push]);
push++;
}
if (tmpStack.top() != popped[pop])
{
break;
}
tmpStack.pop();
pop++;
}
if (tmpStack.empty() && pop == lens)
{
res = true;
}
}
else
{
return true;
}

return res;
}

int main()
{
vector<int> pop;
vector<int> push;
int pushed[5] = {1,2,3,4,5};
int popped[5] = {4,5,3,2,1};
for (int i = 0; i < 5; i++)
{
pop.push_back(popped[i]);
push.push_back(pushed[i]);
}

bool res = validateStackSequences(push, pop);
return 0;
}
Binary file added IsPopOrder.exe
Binary file not shown.
72 changes: 72 additions & 0 deletions MedianFinder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <unordered_map>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;

priority_queue<double, vector<double>, greater<double>> small;
priority_queue<double> big;

void addNum(double num)
{
if (big.empty())
{
big.push(num);
return;
}

if (small.size() == big.size())
{
if (num > big.top())
{
small.push(num);
}
else
{
big.push(num);
}
}
else if (small.size() > big.size())
{
if (num < small.top())
{
big.push(num);
}
else
{
big.push(small.top());
small.pop();
small.push(num);
}
}
else if(big.size() > small.size())
{
if (num > big.top())
{
small.push(num);
}
else
{
small.push(big.top());
big.pop();
big.push(num);
}
}
}


double findMedian() {
if (small.size() == big.size())
{
return (small.top() + big.top()) / 2.0;
}
else if(small.size() > big.size())
{
return small.top();
}
return big.top();
}
66 changes: 66 additions & 0 deletions MinStack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
#include <vector>
#include <stack>
using namespace std;

// stack<int> datas;
// stack<int> mins;

// void push(int x) {
// datas.push(x);
// if (mins.empty() || x < mins.top())
// {
// mins.push(x);
// }
// else
// {
// mins.push(mins.top());
// }
// }

// void pop() {
// if (!mins.empty() && !datas.empty())
// {
// mins.pop();
// datas.pop();
// }
// }

// int top() {
// return datas.top();
// }

// int min() {
// return mins.top();
// }

stack<int> data;
stack<int> mins;

void push(int x) {
if (mins.empty() || x < mins.top())
{
mins.push(x);
}
else
{
mins.push(mins.top());
}
data.push(x);
}

void pop() {
if(!data.empty() && !mins.empty()){
data.pop();
mins.pop();
}

}

int top() {
return data.top();
}

int getMin() {
return mins.top();
}
44 changes: 44 additions & 0 deletions MyStack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <unordered_map>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;


queue<int> data;
void push(int x) {
queue<int> tmp_queue;
tmp_queue.push(x);
while (!data.empty())
{
tmp_queue.push(data.front());
data.pop();
}

while (!tmp_queue.empty())
{
data.push(tmp_queue.front());
tmp_queue.pop();
}

}

int pop() {
int x = data.front();
data.pop();
return x;
}

int top()
{
return data.front();
}

bool empty()
{
return data.empty();
}
70 changes: 70 additions & 0 deletions Surrounded.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

vector<vector<char>> map;
int n, m;

void floodfill(vector<vector<int>>& gra, int i, int j)
{
gra[i][j] = 1;
map[i][j] = '#';
if (i + 1 < n && map[i + 1][j] == 'O' && !gra[i + 1][j])
{
floodfill(gra, i + 1, j);
}
if (i - 1 >= 0 && map[i - 1][j] == 'O' && !gra[i - 1][j])
{
floodfill(gra, i - 1, j);
}
if (j + 1 < m && map[i][j + 1] == 'O' && !gra[i][j + 1])
{
floodfill(gra, i, j + 1);
}
if (j - 1 >= 0 && map[i][j - 1] == 'O' && !gra[i][j - 1])
{
floodfill(gra, i, j - 1);
}
}

void solve(vector<vector<char>>& board) {
n = board.size();
if (!n)
{
return;
}
m = board[0].size();
map = board;
vector<vector<int>> gra(n, vector<int>(m, 0));

for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (i == 0 || i == n - 1 || j == 0 || j == m - 1)
{
if (!gra[i][j] && map[i][j] == 'O')
{
floodfill(gra, i, j);
}
}
}
}

for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (map[i][j] == 'O')
{
map[i][j] = 'X';
}
if (map[i][j] == '#')
{
map[i][j] = 'O';
}
}
}
board = map;
}
58 changes: 58 additions & 0 deletions addBinary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <unordered_map>
#include <queue>
#include <algorithm>
using namespace std;

string addBinary(string a, string b) {
if (a.length() > b.length())
{
swap(a,b);
}

vector<int> sum(b.length() + 1, 0);

for (int i = b.length(), j = a.length(); i > 0; i--)
{
int bl = b[i - 1] - '0';
if (j > 0)
{
int al = a[j - 1] - '0';
sum[i] += al + bl;

if (sum[i] / 2 != 0)
{
sum[i - 1] += sum[i] / 2;
sum[i] %= 2;
}
j--;
}
else
{
sum[i] += bl;
sum[i - 1] += sum[i] / 2;
sum[i] %= 2;
}
}

string res = "";

for (int i = 0; i < sum.size(); i++)
{
if(sum[i] == 0 && i == 0)
continue;
res += sum[i] + '0';
}

return res;
}

int main()
{
string a = "0", b = "0";
addBinary(a, b);
return 0;
}
Binary file added addBinary.exe
Binary file not shown.
Loading