LeetCode-49.Group Anagrams
来源:互联网 发布:c json 双引号转义 编辑:程序博客网 时间:2024/05/01 23:56
https://leetcode.com/problems/anagrams/
Given an array of strings, group anagrams together.
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
,
Return:
[ ["ate", "eat","tea"], ["nat","tan"], ["bat"]]
Note:
- For the return value, each inner list's elements must follow the lexicographic order.
- All inputs will be in lower-case.
最直接的做法,使用242题 LeetCode-242.Valid Anagram 的答案,可惜数据量大时超时
public class Solution{ public IList<IList<string>> GroupAnagrams(string[] strs) { IList<IList<string>> res = new List<IList<string>>(); int n = strs.Length,j=0; if (n == 0) return res; Array.Sort(strs); IList<string> list = new List<string>(); list.Add(strs[0]); res.Add(list); if (n == 1) return res; for (int i = 1; i < n; i++) { for(j=0;j<res.Count;j++) { if (IsAnagram(res[j][0], strs[i])) { res[j].Add(strs[i]); break; } } if (j == res.Count) { list = new List<string>(); list.Add(strs[i]); res.Add(list); } } return res; } public bool IsAnagram(string s, string t) { int n = s.Length; if (t.Length != n) return false; int[] table = new int[26]; for (int i = 0; i < n; i++) { table[s[i] - 'a']++; table[t[i] - 'a']--; } for (int i = 0; i < 26; i++) { if (table[i] != 0) return false; } return true; }}
使用哈希表
public IList<IList<string>> GroupAnagrams(string[] strs) { int n = strs.Length; if (n == 0) return new List<IList<string>>(); Array.Sort(strs); Hashtable table = new Hashtable(); for (int i = 0; i < n; i++) { char[] c = strs[i].ToCharArray(); Array.Sort(c); string temp = new string(c); if (!table.Contains(temp)) table[temp] = new List<string>(); ((IList<string>)table[temp]).Add(strs[i]); } IList<IList<string>> res = new List<IList<string>>(); foreach (DictionaryEntry item in table) res.Add((IList<string>)item.Value); return res; }
比较作弊的办法,使用C#的Linq
public class Solution{ public IList<IList<string>> GroupAnagrams(string[] strs) { int n = strs.Length; if (n == 0) return new List<IList<string>>(); Array.Sort(strs); var lookup = strs.ToLookup(word => SortLetters(word)); IList<IList<string>> res = new List<IList<string>>(); foreach (var item in lookup) res.Add(new List<string>(item)); return res; } private string SortLetters(string word) { char[] letters = word.ToCharArray(); Array.Sort(letters); return new string(letters); }}
0 0
- [leetcode] 49.Group Anagrams
- LeetCode 49. Group Anagrams
- [LeetCode]49. Group Anagrams
- leetcode 49. Group Anagrams
- 49. Group Anagrams LeetCode
- Leetcode 49. Group Anagrams
- LeetCode 49. Group Anagrams
- LeetCode 49. Group Anagrams
- [leetcode] 49. Group Anagrams
- LeetCode 49. Group Anagrams
- LeetCode-49.Group Anagrams
- [Leetcode] 49. Group Anagrams
- LeetCode - 49. Group Anagrams
- [LeetCode] 49. Group Anagrams
- leetcode 49. Group Anagrams
- [leetcode] 49. Group Anagrams
- leetcode 49. Group Anagrams
- [LeetCode]--49. Group Anagrams
- 大数问题
- c++文件读写
- JS鼠标事件大全
- Tomcat 通过listener 启动netty 服务
- Centos7.1搭建openstack私有云
- LeetCode-49.Group Anagrams
- Hibernate分页查询和批量更新(学习笔记)
- ListView Item点击一直变色
- leetcode 110 Balanced Binary Tree C++
- npm body-parser 中文api
- Bootstrap 插件【三】
- myFocus焦点图框架使用心得
- ceph 译文 RADOS:A Scalable, Reliable Storage Service for Petabyte-scale Storage Clusters
- 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数 复制代码