class Solution {
public:
//由于互为字母异位词的两个字符串包含的字母相同,因此对两个字符串分别进行排序之后得到的字符串一定是相同的,故可以将排序之后的字符串作为哈希表的键。
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string,vector<string>> mp;
for(string &str:strs)
{
string key=str;
sort(key.begin(),key.end());
mp[key].emplace_back(str);
}
vector<vector<string>>ans;
for(auto it=mp.begin();it!=mp.end();it++)
{
ans.emplace_back(it->second);
}
return ans;
}
};
class Solution {
public:
bool isAlienSorted(vector<string>& words, string order) {
//哈希表储存字符
vector<int>index(26);
for(int i=0;i<26;i++)
{
index[order[i]-'a']=i;
}
for(int i=1;i<words.size();i++)
{
bool vail=false;
for(int j=0;j<words[i-1].size()&&j<words[i].size();j++)
{
int pre=index[words[i-1][j]-'a'];
int curr=index[words[i][j]-'a'];
if(pre<curr)
{
vail=true;break;
}
if(pre>curr)
{
return false;
}
}
if(!vail)
{
/* 比较两个字符串的长度 */
if(words[i-1].size()>words[i].size())
{
return false;
}
}
}
return true;
}
};
class Solution {
int getMinutes(string &t) {
return (int(t[0] - '0') * 10 + int(t[1] - '0')) * 60 + int(t[3] - '0') * 10 + int(t[4] - '0');
}
public:
int findMinDifference(vector<string> &timePoints) {
int n = timePoints.size();
//鸽巢原理可知24×60=1440 大于1440一定存在相同的时间
if (n > 1440) {
return 0;
}
sort(timePoints.begin(), timePoints.end());
int ans = INT_MAX;
int t0Minutes = getMinutes(timePoints[0]);
int preMinutes = t0Minutes;
for (int i = 1; i < n; ++i) {
int minutes = getMinutes(timePoints[i]);
ans = min(ans, minutes - preMinutes); // 相邻时间的时间差
preMinutes = minutes;
}
ans = min(ans, t0Minutes + 1440 - preMinutes); // 首尾时间的时间差
return ans;
}
};
class Solution {
public:
int evalRPN(vector<string>& tokens) {
//创建一个栈
stack<int> stk;
//字符长度
int n = tokens.size();
//遍历
for (int i = 0; i < n; i++) {
string& token = tokens[i];
//确认为阿拉伯数字
if (isNumber(token)) {
//atoi(s)函数用于把一个字符串转换成一个整型数据
//c_str() 函数可以将 const string* 类型 转化为 cons char* 类型
stk.push(atoi(token.c_str()));
} else {
int num2 = stk.top();
stk.pop();
int num1 = stk.top();
stk.pop();
switch (token[0]) {
case '+':
stk.push(num1 + num2);
break;
case '-':
stk.push(num1 - num2);
break;
case '*':
stk.push(num1 * num2);
break;
case '/':
stk.push(num1 / num2);
break;
}
}
}
return stk.top();
}
bool isNumber(string& token) {
return !(token == "+" || token == "-" || token == "*" || token == "/");
}
};
class Solution {
public:
vector<int> asteroidCollision(vector<int>& asteroids) {
vector<int> st;
for(auto aster:asteroids)
{
bool alive=true;
//向左运行的行星是否碰撞
//st.back()栈顶元素
while(alive&&aster<0&&!st.empty()&&st.back()>0)
{
alive=st.back()<-aster;
if(st.back()<=-aster)
{
st.pop_back();
}
}
if(alive)
{
st.push_back(aster);
}
}
return st;
}
};
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
int n = temperatures.size();
//输出
vector<int> ans(n);
//存放下标第几天
stack<int> s;
//遍历
for (int i = 0; i < n; ++i) {
//非空&&该天温度大于栈顶温度
while (!s.empty() && temperatures[i] > temperatures[s.top()]) {
int previousIndex = s.top();
ans[previousIndex] = i - previousIndex;
s.pop();
}
//小于栈顶温度 则压入
s.push(i);
}
return ans;
}
};