How can I group words that are anagrams together efficiently in LeetCode 49?

2026-04-16 20:284阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计156个文字,预计阅读时间需要1分钟。

How can I group words that are anagrams together efficiently in LeetCode 49?

题目:签到的题+C++ class Solution {public: map m; vector groupAnagrams(vector& strs) { vector strs2=strs; for(int i=0; i res; return res; }}

题目

How can I group words that are anagrams together efficiently in LeetCode 49?

签到题

c++

class Solution { public: map<string,int> m; vector<vector<string>> groupAnagrams(vector<string>& strs) { vector<string> strs2 = strs; for(int i=0;i<strs.size();i++) { sort(strs[i].begin(),strs[i].end()); } vector<vector<string>> res; int pos=0; vector<string> ans; for(int i=0;i<strs.size();i++) { if(m[strs[i]]==0) { m[strs[i]]=++pos; ans.clear(); ans.push_back(strs2[i]); res.push_back(ans); } else { res[m[strs[i]]-1].push_back(strs2[i]); } } return res; } };

本文共计156个文字,预计阅读时间需要1分钟。

How can I group words that are anagrams together efficiently in LeetCode 49?

题目:签到的题+C++ class Solution {public: map m; vector groupAnagrams(vector& strs) { vector strs2=strs; for(int i=0; i res; return res; }}

题目

How can I group words that are anagrams together efficiently in LeetCode 49?

签到题

c++

class Solution { public: map<string,int> m; vector<vector<string>> groupAnagrams(vector<string>& strs) { vector<string> strs2 = strs; for(int i=0;i<strs.size();i++) { sort(strs[i].begin(),strs[i].end()); } vector<vector<string>> res; int pos=0; vector<string> ans; for(int i=0;i<strs.size();i++) { if(m[strs[i]]==0) { m[strs[i]]=++pos; ans.clear(); ans.push_back(strs2[i]); res.push_back(ans); } else { res[m[strs[i]]-1].push_back(strs2[i]); } } return res; } };