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的更多相关文章

  1. LeetCode 049 Anagrams

    题目要求:Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All i ...

  2. 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 ...

  3. 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. ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. yii2系统定义的常用路径别名

    @yii 表示Yii框架所在的目录,也是 yii\BaseYii 类文件所在的位置: @app 表示正在运行的应用的根目录,一般是 digpage.com/frontend :物理路径 @vendor ...

  2. Myeclipse快捷键的使用

    存盘 Ctrl+s(肯定知道) 注释代码 Ctrl+/ 取消注释 Ctrl+\(Eclipse3已经都合并到Ctrl+/了) 代码辅助 Alt+/ 快速修复 Ctrl+1 代码格式化 Ctrl+Shi ...

  3. 思维导图XMiand

    XMiand: 异常强大的国产思维导图工具,还能将图同步到服务器上.做思维导图和头脑风暴必备软件,还能转换绘制鱼骨图.二维图.树形图.逻辑图.组织结构图.

  4. Bzoj3893 [Usaco2014 Dec]Cow Jog

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 302  Solved: 157 Description The cows are out exerci ...

  5. Erlang练习-UDP

    贴一下代码,例子是从别人那里直接抄来的: -module(myudp). -export([start/0, client/1]). %% Server start() -> spawn(fun ...

  6. FatMouse的交易问题

    想按照某个值排序,用sort()函数,结果想了半天不知道用数组怎么解决,然后看了答案,才知道原来可以用struct,想想我真是笨死了.. 原题描述以及答案如下: Problem Description ...

  7. android.net.wifi的简单使用方法

    获取Wifi的控制类WifiManager.  WifiManager  wm=(WifiManager)getSystemService(Context.WIFI_SERVICE); 接下来可以对w ...

  8. MongoDB的安装 转

    第1章 MongoDB的安装 (黎明你好原创作品,转载请注明) 1.1 MongoDB简介 MongoDB是一个基于分布式文件存储的数据库开源项目.由C++语言编写,旨在为WEB应用提供可护展的高性能 ...

  9. ACM 马拦过河卒(动态规划)

    解题思路: 用一个二维数组a[i][j]标记 马的位置和马的跳点(统称控制点)该位置=1: 再用一个二维数组f[i][j]表示行进的位置,如果前一行的当前列不是马的控制点,或者前一列的当前行不是马的控 ...

  10. 根据某列,将两个 dataframe 合并

    import pandas as pd import numpy as np df1 = pd.DataFrame(np.array([['a', 5, 9], ['b', 4, 61], ['c', ...