单词模式问题
class Solution {
public:
bool wordPattern(string pattern, string s) {
//1、使用两个散列表互相映射;
//2、切割字符串使用stringstream;
//实际就是两个字符串的双重for循环,一一匹配,;如果一个匹配完了另一个还存在,最后三元运算符处理一下;
unordered_map<char, string> map;
unordered_map<string, char> rmap;
stringstream ss(s);
string str;
for(char c: pattern)
{
if(!(ss >> str) ||
(map.count(c) == 1 && map[c] != str) ||
(rmap.count(str) == 1 && rmap[str] != c)){
return false;
}
map[c] = str;
rmap[str] = c;
}
return (ss >> str)? false:true;
}
};
https://leetcode.cn/problems/next-greater-element-iii/
class Solution {
public:
int nextGreaterElement(int n) {
string s = to_string(n);
int nn = s.size();
if(nn <= 1)
{
return -1;
}
int i = nn-2;//从右往左找第一个小于右边值的数字;
for(; i >=0;--i)
{
if(s[i] < s[i+1])
{
break;
}
}
if(i < 0)
{
return -1;
}
int j = nn-1;
for(;j>=0;--j)
{
if(s[j] > s[i])
{
break;
}
}
swap(s[j],s[i]);
reverse(s.begin() + i+1,s.end());
long res = stol(s);
return res > INT_MAX ? -1 :res;
}
};