Java for LeetCode 049 Anagrams
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
解题思路:首先要理解,什么是anagrams,ie。“tea”、“tae”、“aet”,然后就十分好做了,new一个hashmap,使用一个排过序的String作为key,重复了就往里面添加元素,这里出现一个小插曲,就是排序的时候不要用PriorityQueue,因为PriorityQueue是采用堆排序的,仅保证堆顶元素为优先级最高的(害了我好久)JAVA实现如下:
static public List<String> anagrams(String[] strs) {
List<String> list = new ArrayList<String>();
HashMap<String, List<String>> hm = new HashMap<String, List<String>>();
for (int i = 0; i < strs.length; i++) {
char [] c=strs[i].toCharArray();
Arrays.sort(c);
String sortString=new String(c);
if (!hm.containsKey(sortString))
hm.put(sortString.toString(), new ArrayList<String>());
hm.get(sortString.toString()).add(strs[i]);
}
Iterator<String> iterator = hm.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
if (hm.get(key).size() > 1)
list.addAll(hm.get(key));
}
return list;
}
Java for LeetCode 049 Anagrams的更多相关文章
- LeetCode 049 Anagrams
题目要求:Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All i ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- 关于ActionContext.getContext()的用法
为了避免与Servlet API耦合在一起,方便Action类做单元测试,Struts 2对HttpServletRequest.HttpSession和ServletContext进行了封装,构造了 ...
- python 学习5--matplotlib画图实践
### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 学习参考: http://www.cnblogs.com/vamei/archive/2013/01/30/28 ...
- Spynner 安装
Spynner 安装 Windows7 下安装 1.easy_install spynner 2.下载pyqt sip https://sourceforge.net/projects/pyqt/fi ...
- 输入你的性别,身高及体重,判断你的身材是否标准。(用if......else语句)
Console.WriteLine("请输入你的性别,身高和体重:"); string s = Console.ReadLine(); double h = double.Pars ...
- 安装coreseek找不到mysql
1.安装 coreseek-3.2.14 遇到问题:“ERROR: cannot find MySQL include files,随即在网上搜索各种答案说是要找到mysql.h的正确路径加入./co ...
- Redis配置文件主要功能说明
原文地址:http://blog.csdn.net/htofly/article/details/7686436 配置文件参数说明: 1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改 ...
- 理解模板引擎Razor 的原理(转载)
Razor是ASP.NET MVC 3中新加入的技术,以作为ASPX引擎的一个新的替代项.简洁的语法与.NET Framework 结合,广泛应用于ASP.NET MVC 项目.Razor Pad是一 ...
- linux wget 命令用法详解(附实例说明)
Linux wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,尤其对于网络管理员,经常要下载一些软件或从远程服务器恢复备份到本地服务器 Linux wget是一个下 ...
- js获取单选框里面的值
rt,如果想获取单选框里面的值,该如何获取呢. <script> window.onload = function(){ //通过名字获取 getElementsByName //var ...
- Linux建立软连接
实例:ln -s /home/gamestat /gamestat linux下的软链接类似于windows下的快捷方式 ln -s a b 中的 a 就是源文件,b是链接文件名,其作用是当进入 ...