-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
907 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
EvaluateReversePolishNotation/EvaluateReversePolishNotation.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include <iostream> | ||
#include <stack> | ||
#include <vector> | ||
#include <cstring> | ||
#include <cstdlib> | ||
|
||
using namespace std; | ||
|
||
int evalRPN(vector<string>& tokens) { | ||
int len = tokens.size(); | ||
|
||
stack<int> stk; | ||
|
||
for (int i=0;i<len;i++){ | ||
if (tokens[i] == "+"){ | ||
int first = stk.top(); | ||
stk.pop(); | ||
int second = stk.top(); | ||
stk.pop(); | ||
int re = second + first; | ||
stk.push(re); | ||
} | ||
else if (tokens[i] == "-"){ | ||
int first = stk.top(); | ||
stk.pop(); | ||
int second = stk.top(); | ||
stk.pop(); | ||
int re = second - first; | ||
stk.push(re); | ||
} | ||
else if (tokens[i] == "*"){ | ||
int first = stk.top(); | ||
stk.pop(); | ||
int second = stk.top(); | ||
stk.pop(); | ||
int re = second * first; | ||
stk.push(re); | ||
} | ||
else if (tokens[i] == "/"){ | ||
int first = stk.top(); | ||
stk.pop(); | ||
int second = stk.top(); | ||
stk.pop(); | ||
int re = second / first; | ||
stk.push(re); | ||
} | ||
else{ | ||
int elem = stoi(tokens[i]); | ||
stk.push(elem); | ||
} | ||
} | ||
|
||
int re = stk.top(); | ||
stk.pop(); | ||
|
||
return re; | ||
} | ||
|
||
int main(){ | ||
vector<string> tokens; | ||
tokens.push_back("2"); | ||
tokens.push_back("1"); | ||
tokens.push_back("+"); | ||
tokens.push_back("3"); | ||
tokens.push_back("*"); | ||
int re = evalRPN(tokens); | ||
cout<<re; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* // This is the interface that allows for creating nested lists. | ||
* // You should not implement it, or speculate about its implementation | ||
* class NestedInteger { | ||
* public: | ||
* // Return true if this NestedInteger holds a single integer, rather than a nested list. | ||
* bool isInteger() const; | ||
* | ||
* // Return the single integer that this NestedInteger holds, if it holds a single integer | ||
* // The result is undefined if this NestedInteger holds a nested list | ||
* int getInteger() const; | ||
* | ||
* // Return the nested list that this NestedInteger holds, if it holds a nested list | ||
* // The result is undefined if this NestedInteger holds a single integer | ||
* const vector<NestedInteger> &getList() const; | ||
* }; | ||
*/ | ||
|
||
class NestedIterator { | ||
private: | ||
vector<int> vc; | ||
int index; | ||
public: | ||
void parse(vector<NestedInteger> &nl){ | ||
int len = nl.size(); | ||
for (int i=0;i<len;i++){ | ||
NestedInteger tmp = nl[i]; | ||
if (tmp.isInteger()){ | ||
vc.push_back(tmp.getInteger()); | ||
} | ||
else{ | ||
parse(tmp.getList()); | ||
} | ||
} | ||
} | ||
NestedIterator(vector<NestedInteger> &nestedList) { | ||
parse(nestedList); | ||
index = -1; | ||
} | ||
|
||
int next() { | ||
return vc[++index]; | ||
} | ||
|
||
bool hasNext() { | ||
return (index+1)<vc.size(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <fstream> | ||
#include <map> | ||
|
||
using namespace std; | ||
|
||
bool Union(int i, int j, int* id,int len){ | ||
if (id[i]==id[j]) | ||
return false; | ||
|
||
int id1 = id[i]; | ||
int id2 = id[j]; | ||
|
||
for (int p=0;p<len;p++){ | ||
if (id[p]==id2) | ||
id[p] = id1; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
int num_component(int* id,int len){ | ||
int count = 0; | ||
map<int,int> m; | ||
for (int i=0;i<len;i++){ | ||
if (m.count(id[i])==0) | ||
m[id[i]] = 1; | ||
} | ||
return m.size(); | ||
} | ||
|
||
bool validTree(int n, vector<pair<int, int> >& edges){ | ||
int id[n]; | ||
int len = edges.size(); | ||
|
||
for (int i=0;i<n;i++) | ||
id[i] = i; | ||
|
||
for (int i=0;i<len;i++){ | ||
pair<int,int> tmp = edges[i]; | ||
int id1 = tmp.first; | ||
int id2 = tmp.second; | ||
if (id[id1]==id[id2]) | ||
return false; | ||
for (int p=0;p<n;p++){ | ||
if (id[p]==id2) | ||
id[p] = id1; | ||
} | ||
} | ||
|
||
int num_com = num_component(id,n); | ||
|
||
return num_com==1?true:false; | ||
} | ||
|
||
int main(){ | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <iostream> | ||
#include <stack> | ||
|
||
using namespace std; | ||
|
||
class Queue { | ||
private: | ||
stack<int> stk1; | ||
stack<int> stk2; | ||
|
||
public: | ||
// Push element x to the back of queue. | ||
void push(int x) { | ||
stk1.push(x); | ||
} | ||
|
||
// Removes the element from in front of queue. | ||
void pop(void) { | ||
int len = stk1.size(); | ||
if (stk2.empty()) | ||
for (int i=0;i<len;i++){ | ||
stk2.push(stk1.top()); | ||
stk1.pop(); | ||
} | ||
stk2.pop(); | ||
} | ||
|
||
// Get the front element. | ||
int peek(void) { | ||
int len = stk1.size(); | ||
if (stk2.empty()) | ||
for (int i=0;i<len;i++){ | ||
stk2.push(stk1.top()); | ||
stk1.pop(); | ||
} | ||
return stk2.top(); | ||
} | ||
|
||
// Return whether the queue is empty. | ||
bool empty(void) { | ||
return stk1.empty()&&stk2.empty(); | ||
} | ||
}; | ||
|
||
int main(){ | ||
Queue* q = new Queue(); | ||
q->push(1); | ||
q->push(2); | ||
int tm = q->peek(); | ||
cout<<tm; | ||
|
||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <iostream> | ||
#include <queue> | ||
|
||
using namespace std; | ||
|
||
class Stack { | ||
private: | ||
queue<int> q; | ||
public: | ||
// Push element x onto stack. | ||
void push(int x) { | ||
q.push(x); | ||
} | ||
|
||
// Removes the element on top of the stack. | ||
void pop() { | ||
int len = q.size(); | ||
for (int i=0;i<len-1;i++){ | ||
q.push(q.front()); | ||
q.pop(); | ||
} | ||
q.pop(); | ||
} | ||
|
||
// Get the top element. | ||
int top() { | ||
int len = q.size(); | ||
for (int i=0;i<len-1;i++){ | ||
q.push(q.front()); | ||
q.pop(); | ||
} | ||
int out = q.front(); | ||
q.push(out); | ||
q.pop(); | ||
return out; | ||
} | ||
|
||
// Return whether the stack is empty. | ||
bool empty() { | ||
return q.empty(); | ||
} | ||
}; | ||
|
||
int main(){ | ||
Stack stk; | ||
stk.push(1); | ||
stk.push(2); | ||
stk.pop(); | ||
int re = stk.top(); | ||
cout<<re; | ||
|
||
return 0; | ||
} |
Binary file not shown.
42 changes: 42 additions & 0 deletions
42
LargestRectangleinHistogram/LargestRectangleinHistogram.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <stack> | ||
|
||
using namespace std; | ||
|
||
|
||
int largestRectangleArea(vector<int>& heights) { | ||
int maxArea = 0; | ||
stack<int> stk; | ||
|
||
int len = heights.size(); | ||
if (len == 0) | ||
return maxArea; | ||
heights.push_back(0); | ||
|
||
for (int i=0;i<len+1;i++){ | ||
while (!stk.empty() && heights[stk.top()] > heights[i]){ | ||
int tmp = stk.top(); | ||
stk.pop(); | ||
int lenright = i - tmp - 1; | ||
int lenleft = stk.empty()?(tmp + 1):(tmp - stk.top()); | ||
int area = (lenright + lenleft) * heights[tmp]; | ||
if (area > maxArea) | ||
maxArea = area; | ||
} | ||
stk.push(i); | ||
} | ||
|
||
return maxArea; | ||
} | ||
|
||
int main(){ | ||
vector<int> heights; | ||
heights.push_back(0); | ||
heights.push_back(1); | ||
//heights.push_back(2); | ||
|
||
int re = largestRectangleArea(heights); | ||
cout<<re; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <fstream> | ||
|
||
using namespace std; | ||
|
||
int longestConsecutive(vector<int>& nums) { | ||
int len = nums.size(); | ||
if (len<=0) | ||
return 0; | ||
sort(nums.begin(),nums.end()); | ||
int lenm = 1; | ||
int lent = 1; | ||
|
||
for (int i=1;i<len;i++){ | ||
if (nums[i]-nums[i-1]==1){ | ||
lent++; | ||
if (lent>lenm) | ||
{ | ||
lenm = lent; | ||
lent = 1; | ||
} | ||
} | ||
else{ | ||
lent = 1; | ||
} | ||
} | ||
return lenm; | ||
} | ||
|
||
int main(){ | ||
|
||
} |
Oops, something went wrong.