遍历unordered_map
leetcode 49字母异位词分组
class Solution {
public:
vector> groupAnagrams(vector& strs) {
unordered_map> map;
for(auto str:strs)
{
string key = str;
sort(key.begin(),key.end());
map[key].push_back(str);
}
vector> res;
for(auto iter=map.begin();iter!=map.end();iter++)
{
res.push_back(iter->second);
}
return res;
}
};
字符串输入输出问题
intput
3
11
antontrygub
15
bestcoordinator
19
trywatchinggurabruh
code
#include
#include
#include
using namespace std;
const int N=210;
char s[N];
int main()
{
int t=0;
cin >> t;
while(t--)
{
int n;
cin >> n >> s;//输入string转为char[] 不需要s+n 直接输入s就行
sort(s,s+n);
for(int i=0;i
创建动态规划数组tle问题
创成bool数组不能过
class Solution {
public:
string longestPalindrome(string s) {
int n = s.size();
vector<vector<bool>>f(n,vector<bool>(n,0));
string res;
for(int i=n-1;i>=0;i--)
{
for(int j=i;j<n;j++)
{
if(j-i+1>2)f[i][j]=s[i]==s[j]?f[i+1][j-1]:false;
else f[i][j]=s[i]==s[j]?true:false;
if(f[i][j]&&j-i+1>res.size())res = s.substr(i,j-i+1);
}
}
return res;
}
};
创成int数组或者静态bool数组能过
const int N=100010;
bool f[n][n];
class Solution {
public:
string longestPalindrome(string s) {
int n = s.size();
vector<vector<int>>f(n,vector<int>(n,0));
string res;
int l,r,maxi;
for(int i=n-1;i>=0;i--)
{
for(int j=i;j<n;j++)
{
if(j-i+1>2&&s[i]==s[j])f[i][j]=f[i+1][j-1];
else f[i][j]=s[i]==s[j]?1:0;
if(f[i][j]==1&&j-i+1>maxi)
{
l=i,r=j,maxi=j-i+1;
}
}
}
return s.substr(l,r-l+1);
}
};