leetcode做题总结,题目Anagrams 2012/03/19
来源:互联网 发布:linux svn ignore 编辑:程序博客网 时间:2024/05/21 21:47
这道题是https://oj.leetcode.com/problems/anagrams/
题目有点不太清晰,意思是说把有异构的字符串进行归类,然后按照类别依次输出,只有超过两个字符串异构才进行输出。我学到的有把一个String转成char array可以用toCharArray(),但是要注意如果String是“”返回的Array也是空的。还有就是存在容器里的list,array对其进行修改之后都是不用打回容器里的。
public class Solution { public List<String> anagrams(String[] strs) { HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>(); ArrayList<String> res = new ArrayList<String>(); if(strs.length==0) return res; for(int i=0;i<strs.length;i++){ char[] store = strs[i].toCharArray(); Arrays.sort(store); String sortstr = new String(store);//cannot use toString() here if(strs[i].equals("")) sortstr=""; if(map.containsKey(sortstr)){ map.get(sortstr).add(strs[i]);//unnecessary to put it back since it is a list. }else{ ArrayList<String> l = new ArrayList<String>(); l.add(strs[i]); map.put(sortstr,l); } } // Iterator<ArrayList<String>> iter = map.values().iterator(); while(iter.hasNext()){ ArrayList<String> tmp = iter.next(); if(tmp.size()>1) res.addAll(tmp); } return res; }}
Update 2015/08/31: 分享一个九章的解法,使用数组记录有哪些字母然后求hash,其他思路一样
public class Solution { /** * @param strs: A list of strings * @return: A list of strings */ private int getHash(int[] count) { int hash = 0; int a = 378551; int b = 63689; for (int num : count) { hash = hash * a + num; a = a * b; } return hash; } public List<String> anagrams(String[] strs) { // write your code here ArrayList<String> result = new ArrayList<String>(); HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>(); for (String str : strs) { int[] count = new int[26]; for (int i = 0; i < str.length(); i++) { count[str.charAt(i) - 'a']++; } int hash = getHash(count); if (!map.containsKey(hash)) { map.put(hash, new ArrayList<String>()); } map.get(hash).add(str); } for (ArrayList<String> tmp : map.values()) { if (tmp.size() > 1) { result.addAll(tmp); } } return result; }}
0 0
- leetcode做题总结,题目Anagrams 2012/03/19
- leetcode做题总结,题目Valid Sudoku 2012/03/03
- leetcode做题总结,题目Rotate List 2012/03/27
- leetcode做题总结,题目Permutation Sequence 2012/03/27
- leetcode做题总结,题目Pow(x, n)-------- 2012/03/19
- leetcode做题总结,题目Plus One 2012/04/02
- leetcode做题总结,题目Generate Parentheses 2012/02/12
- leetcode做题总结,题目Gray Code 2012/05/20
- leetcode做题总结,题目Partition List 2012/04/30
- leetcode做题总结,题目Valid Parentheses 2012/01/30
- leetcode做题总结,题目Sort Colors 2012/04/08
- leetcode做题总结,题目Combinations 2012/04/18
- leetcode做题总结,题目Palindrome Number 2012/01/04
- leetcode做题总结,题目Search Insert Position 2012/03/03
- leetcode做题总结,题目Remove Duplicates from Sorted ArrayI/II--- 2012/02/16-04/19
- leetcode做题总结,题目Merge Two Sorted Lists 2012/03/30
- leetcode做题总结,题目First Missing Positive 2012/03/08
- leetcode做题总结,题目Spiral Matrix I/II 2012/03/24
- Hibernate的List映射
- 访问网站时只是显示主页(index.jsp),没有请求连接数据库读取数据。
- Android Studio 1.0 苹果电脑安装配置
- 《大话设计模式》读后感
- 再论libmesh,options on engineering simulation software design and develop (现代工程仿真软件设计开发的一个考虑)
- leetcode做题总结,题目Anagrams 2012/03/19
- Excel Sheet Column Title
- 空间科学学报
- java基础2
- Longest Palindromic Substring
- HH生病了(hpu1136)
- 2048 Puzzle游戏攻略
- Edit Distance
- 移动开发的饕餮盛宴-Zoomla!逐浪CMS2 x2.1正式发布