406 Queue Reconstruction by Height
class Solution {
public:
vector<pair<int, int> > reconstructQueue(vector<pair<int, int> >& people)
{
int len=people.size();
vector<pair<int, int> > ans;
ans.clear();
sort(people.begin(),people.end(),[](pair<int,int> p1,pair<int,int> p2)
{
return p1.first>p2.first || (p1.first == p2.first && p1.second
<p2.second);
});
for(auto person:people)
{
ans.insert(ans.begin()+person.second,person);
}
return ans;
}
};
386 Lexicographical Numbers
class Solution {
public:
vector<int> ans;
vector<int> lexicalOrder(int n)
{
for(int i=1;i<=9;i++)
depthSearch(i,n);
return ans;
}
void depthSearch(int p,int n)
{
if (p<=n) ans.push_back(p);
for(int i=0;i<=9;i++)
{
int tmpp=p*10+i;
if (tmpp<=n)
{
depthSearch(tmpp,n);
}
else continue;
}
return ;
}
};
404 Sum of Left Leaves
class Solution {
public:
int sum;
int sumOfLeftLeaves(TreeNode* root)
{
sum=0;
if (root==NULL) return sum;
depthSearsh(root,false);
return sum;
}
void depthSearsh(TreeNode* r,bool leftornot)
{
if (r->left==NULL && r->right==NULL && leftornot)
sum+=r->val;
if (r->left!=NULL)
{
depthSearsh(r->left,true);
}
if (r->right!=NULL)
{
depthSearsh(r->right,false);
}
return ;
}
};
394 Decode String
class Solution {
public:
string decodeString(string s)
{
int len=s.size();
int num=0;
string ans="",str="";
stack<int> repeats;
stack<string> strings;
for(int i=0;i<len;i++)
{
if (s[i]=='[')
{
strings.push("[");
}
else if (s[i]==']')
{
str="";
while (strings.top()!="[")
{
str=strings.top()+str;
strings.pop();
}
strings.pop(); //pop [
int re=repeats.top();
repeats.pop();
ans="";
for(int j=0;j<re;j++)
ans=ans+str;
strings.push(ans);
}
else if (s[i]>='0' && s[i]<='9')
{
num=0;
while (s[i]>='0' && s[i]<='9')
{
num=num*10+s[i]-'0';
i++;
}
repeats.push(num);
i--;
}
else
{
strings.push(s.substr(i,1));
}
}
ans="";
while (!strings.empty())
{
ans=strings.top()+ans;
strings.pop();
}
return ans;
}
};
floyd
class Solution {
public:
map<string,int> nums;
int lnums=-1;
void findANumber(string name)
{
if (nums.find(name)==nums.end())
{
lnums++;
nums[name]=lnums;
}
}
vector<double> calcEquation(vector<pair<string, string> > equations,
vector<double>& values,
vector<pair<string, string> > queries)
{
vector<double> ans;
int lequations=equations.size();
for(int i=0;i<lequations;i++)
{
findANumber(equations[i].first);
findANumber(equations[i].second);
}
double f[lnums+10][lnums+10]={0};
for(int i=0;i<=lnums;i++)
{
for(int j=0;j<=lnums;j++)
f[i][j]=0;
}
for(int i=0;i<lequations;i++)
{
int n1=nums[equations[i].first];
int n2=nums[equations[i].second];
f[n1][n2]=values[i];
f[n2][n1]=1.0/values[i];
}
for(int k=0;k<=lnums;k++)
for(int i=0;i<=lnums;i++)
for(int j=0;j<=lnums;j++)
{
if (i==j || i==k || j==k) continue;
if (f[i][j]>0.0) continue;
if (f[i][k]>0.0 && f[k][j]>0.0) f[i][j]=f[i][k]*f[k][j];
}
int lqueries=queries.size();
for(int i=0;i<lqueries;i++)
{
string s1=queries[i].first;
string s2=queries[i].second;
if ((nums.find(s1)== nums.end()) ||(nums.find(s2)==nums.end()))
{
ans.push_back(-1.0);
continue;
}
if (s1==s2)
{
ans.push_back(1.0);
continue;
}
int n1=nums[s1],n2=nums[s2];
if (f[n1][n2]>0) ans.push_back(f[n1][n2]);
else
if (f[n2][n1]>0) ans.push_back(1.0/f[n2][n1]);
else
ans.push_back(-1.0);
}
return ans;
}
};