LeetCode: Anagrams 解题报告
Anagrams
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
思路:
建Hashtable,用排序过的string作为key,它的anagram作为ArrayList
这道题之前用暴力写的O(N^2)的TLE了,改用Hashtable来写
题目的意思是给一个String数组,找出其中由相同字母组成的单词。
例如:
S = ["abc", "bca", "bac", "bbb", "bbca", "abcb"]
答案为:
["abc", "bca", "bac", "bbca", "abcb"]
只有"bbb"没有相同字母组成的单词。
ref: http://blog.csdn.net/fightforyourdream/article/details/14217985
public class Solution {
public List<String> anagrams(String[] strs) {
List<String> ret = new ArrayList<String>();
if (strs == null) {
return ret;
}
HashMap<String, List<String>> map = new HashMap<String, List<String>>();
int len = strs.length;
for (int i = ; i < len; i++) {
String s = strs[i];
// Sort the string.
char[] chars = s.toCharArray();
Arrays.sort(chars);
String strSort = new String(chars);
// Create a ArrayList for the sorted string.
if (!map.containsKey(strSort)) {
map.put(strSort, new ArrayList<String>());
}
// Add a new string to the list of the hashmap.
map.get(strSort).add(s);
}
// go through the map and add all the strings into the result.
for (Map.Entry<String, List<String>> entry: map.entrySet()) {
List<String> list = entry.getValue();
// skip the entries which only have one string.
if (list.size() == ) {
continue;
}
// add the strings into the list.
ret.addAll(list);
}
return ret;
}
}
2015.1.3 redo:
public class Solution {
public List<String> anagrams(String[] strs) {
List<String> ret = new ArrayList<String>();
if (strs == null) {
return ret;
}
HashMap<String, List<String>> map = new HashMap<String, List<String>>();
for (int i = 0; i < strs.length; i++) {
String s = strs[i];
char[] chars = s.toCharArray();
Arrays.sort(chars);
String sSort = new String(chars);
if (map.containsKey(sSort)) {
map.get(sSort).add(s);
} else {
List<String> list = new ArrayList<String>();
list.add(s);
map.put(sSort, list);
}
}
// Bug 1: should use map.entrySet() instead of MAP.
for (Map.Entry<String, List<String>> entry: map.entrySet()) {
List<String> list = entry.getValue();
if (list.size() > 1) {
ret.addAll(list);
}
}
return ret;
}
}
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/hash/Anagrams.java
LeetCode: Anagrams 解题报告的更多相关文章
- LeetCode: Permutations 解题报告
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- 【LeetCode】49. Group Anagrams 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+hash 日期 题目地址:https://le ...
- leetcode—Palindrome 解题报告
1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...
- LeetCode C++ 解题报告
自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
- LeetCode: Subsets 解题报告
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- LeetCode: Triangle 解题报告
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- LeetCode: isSameTree1 解题报告
isSameTree1 Given two binary trees, write a function to check if they are equal or not. Two binary t ...
- LeetCode: Combinations 解题报告
Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...
随机推荐
- ios开发之公交卡系统的设计与实现
// // main.m // Bus-公交 /* 公交一卡通充值系统.有两种功能选择.第一种投入硬币或者纸币 选择购票,另外一种一卡通充值,充值面额是20,50.100 */ #import < ...
- jquery之往一个CSS属性里写入多个值
<html> <head> <title>test</title> <base href="<%=basePath%>&qu ...
- 重叠IO overlapped I/O 运用详解
2009年02月21日 星期六 下午 07:54 I/O设备处理必然让主程序停下来干等I/O的完成,对这个问题有 方法一:使用另一个线程进行I/O.这个方案可行,但是麻烦. ...
- CSS3+JS 实现的便签应用
概述 利用HTML5新增的 locationStorage 实现的便签应用,没有使用 JQuery,主要是为了练习原生JS的使用,采用响应式开发,在手机端和桌面端都有良好的体验,而且使用CSS3添加了 ...
- 从零开始学做微信小程序,看这些就够了!
随着正式开放公测,微信小程序再次万众瞩目,越来越多的企业和个人涌入到小程序开发的大军中.小程序究竟是什么?适合做小程序的产品有哪些?做小程序需要提前准备什么?如何零基础学做小程序?此文,将列出OSC上 ...
- Apache Hadoop 3.0新版本介绍及未来发展方向
过去十年,Apache Hadoop从无到有,从理论概念演变到如今支撑起若干全球最大的生产集群.接下来的十年,Hadoop将继续壮大,并发展支撑新一轮的更大规模.高效和稳定的集群. 我们此次将向大家全 ...
- springmvc编码问题
web.xml中加入 <filter> <filter-name>encodingFilter</filter-name> <filter-class> ...
- IE浏览器SCRIPT5拒绝访问,谷歌浏览器XMLHttpRequest can't load file:/......
一.背景 在测试ajax时,写了一个ajax.html,目的是访问example.txt中的文本,写好后,右键该html选择在浏览器中打开,浏览器页面上无内容.调出调试窗口: IE浏览器:SCRIPT ...
- HDUOJ----(2064)汉诺塔III
汉诺塔III Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- JFinal 源码知识点
1.JFinal中自带json工具类,没必要导入其他的转化包. 使用:setAttr("status","success"), renderJson() 会将所 ...