310 Minimum Height Trees
class Solution
{
public:
vector<int> findMinHeightTrees(int n, vector<pair<int, int> >& edges)
{
vector< vector<int> > grid;
int len=edges.size();
grid.clear();
grid.resize(n+1);
vector<int> income(n+5,0);
vector<int> flag(n+5,0);
queue<int> q;
while(!q.empty()) q.pop();
int cnt = 0;
for(int i=0;i<len;i++)
{
int f=edges[i].first;
int s=edges[i].second;
grid[f].push_back(s);
grid[s].push_back(f);
income[f]++;
income[s]++;
}
while(n-cnt>2)
{
for(int i=0;i<n;i++)
{
if (flag[i]==0 && income[i]==1) q.push(i);
}
while(!q.empty())
{
int x=q.front();q.pop();
flag[x]=1;cnt++;
for(int i=0;i<grid[x].size();i++)
{
if (flag[grid[x][i]]==0)
{
income[grid[x][i]]--;
}
}
}
}
vector<int> ans;
for(int i=0;i<n;i++)
if (flag[i]==0) ans.push_back(i);
return ans;
}
};
class NumMatrix {
public:
vector<vector<int> > sum;
bool flag;
NumMatrix(vector<vector<int> > &matrix)
{
int row=matrix.size();
if (row==0) flag=true; else
{
flag=false;
int col=matrix[0].size();
sum.clear();
sum = vector<vector<int> >(row+1, vector<int>(col+1, 0));
for(int i=1;i<=row;i++)
for(int j=1;j<=col;j++)
{
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+matrix[i-1][j-1];
}
}
}
int sumRegion(int row1, int col1, int row2, int col2)
{
if (flag) return 0;
row1++;col1++;row2++;col2++;
return sum[row2][col2]+sum[row1-1][col1-1]-sum[row2][col1-1]-sum[row1-1][col2];
}
};145 Binary Tree Postorder Traversal
class Solution
{
public:
vector<int> ans;
vector<int> postorderTraversal(TreeNode* root)
{
ans.clear();
if (root==NULL) return ans;
depth(root);
return ans;
}
void depth(TreeNode* root)
{
if (root->left!=NULL) depth(root->left);
if (root->right!=NULL) depth(root->right);
ans.push_back(root->val);
}
};98 Validate Binary Search Tree
class Solution
{
public:
bool isValidBST(TreeNode* root)
{
if (root==NULL) return true;
return preOrderCheck(root,NULL,NULL);
}
bool preOrderCheck(TreeNode* root,TreeNode* low, TreeNode* high)
{
if (root==NULL) return true;
if ((low!=NULL) && (root->val <= low->val)) return false;
if ((high!=NULL) && (root->val >= high->val)) return false;
return (preOrderCheck(root->left,low,root) && preOrderCheck(root->right,root,high));
}
};301 Remove Invalid Parentheses
using namespace std;
class Solution {
public:
vector<string> removeInvalidParentheses(string s)
{
vector<string> ans;
vector< queue<string> >q;
set<string> appear;
set<string> qRepeat;
appear.clear();
ans.clear();
qRepeat.clear();
q.clear();q.resize(2);
if (isValid(s))
{
ans.push_back(s);
return ans;
}
int now =0;
q[now].push(s);
bool flag;
for(;;)
{
qRepeat.clear();
while (!q[now].empty())
{
string cur = q[now].front();q[now].pop();
int lcur = cur.size();
string tmp;
for(int i=0;i<lcur;i++)
{
tmp = cur.substr(0,i);
if (lcur > 1)
tmp += cur.substr(i+1,lcur-1-i);
if (qRepeat.find(tmp)==qRepeat.end())
{
q[1-now].push(tmp);
qRepeat.insert(tmp);
}
}
}
now = 1-now;
queue<string> chk = q[now];
flag = false;
appear.clear();
while(!chk.empty())
{
string str=chk.front();chk.pop();
if (isValid(str) && appear.find(str)==appear.end())
{
appear.insert(str);
ans.push_back(str);
flag=true;
}
}
if (flag) break;
}
return ans;
}
bool isValid(string s)
{
int len=s.size();
if(len==0) return true;
stack<int> st;
int flag[300]={0};
flag['(']=1;flag[')']=-1;
while (!st.empty()) st.pop();
st.push(4);
for(int i=0;i<len;i++)
{
if (flag[s[i]]==0) continue;
if (flag[s[i]]>0) st.push(flag[s[i]]);
else
{
if (flag[s[i]]+st.top()==0)
st.pop();
else
return false;
}
}
if (st.size()!=1) return false;else return true;
}
};
388 Longest Absolute File Path
class Solution {
public:
stack <pair<string, int > >s;
string ans;
int lengthLongestPath(string input)
{
vector< pair<string ,int> > vec;
input += "\n";
int locn;
string tmps;
for(;;)
{
locn = input.find("\n");
if (locn<0) break;
tmps = input.substr(0,locn);
int loct,cntt=0;
while(tmps[0]=='\t')
{
cntt++;
tmps=tmps.substr(1);
}
vec.push_back(pair<string,int>(tmps,cntt));
if (locn!=input.size()-1)input=input.substr(locn+1);
else break;
}
while(!s.empty()) s.pop();
int len =vec.size();
ans="/";
for(int i=0;i<len;i++)
{
if (s.empty())
{
update(vec[i]);
continue;
}
while(!s.empty() && (s.top().second>= vec[i].second))
{
// cout<<"----------pop----------"<<endl;
//cout<<s.top().first<<" "<<s.top().second<<endl;
//cout<<vec[i].second<<endl;
s.pop();
}
update(vec[i]);
}
return ans.size()-1;
}
void update(pair<string,int> x)
{
s.push(x);
stack<pair<string,int> > tmps =s;
string str="";
while(!tmps.empty())
{
str =tmps.top().first + "/"+str;
tmps.pop();
}
//cout<<"---------str---------"<<endl<<str<<endl;
//cout<<"---------ans---------"<<endl<<ans<<endl;
int p = str.find(".");
if (p<0 || p>=str.size()) return ;
if (str.size()>ans.size()) ans=str;
}
};
class Solution {
public:
vector<TreeNode*> num;
void recoverTree(TreeNode* root)
{
TreeNode* first;
TreeNode* second;
num.clear();
depthSearch(root);
int len=num.size();
for(int i=0;i<len-1;i++)
{
if (num[i]->val > num[i+1]->val)
{
first = num[i];
break;
}
}
for(int i=len-1;i>0;i--)
{
if (num[i]->val < num[i-1]->val)
{
second = num[i];
break;
}
}
int tmp = first->val;
first->val = second->val;
second->val = tmp;
return ;
}
void depthSearch(TreeNode* root)
{
if(root->left!=NULL) depthSearch(root->left);
num.push_back(root);
if (root->right!=NULL) depthSearch(root->right);
return ;
}
};

被折叠的 条评论
为什么被折叠?



