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 ... ...
随机推荐
- 所有标准API
序号 系统版本 模块 应用场景 类型 API/接口 参数规格 样例代码 备注 登记者 登记时间 关键字 1 12.1.3 AP 付款核销 API ap_pay_invoice_pkg.ap_pay_i ...
- python字符串格式化--dict传参
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python字符串格式化--dict传参 print "I'm %(name)s. I'm %(a ...
- 去除DataTable重复数据的三种方法(转)
转自:https://www.cnblogs.com/sunxi/p/4767577.html 业务需求 最近做一个把源数据库的数据批次导出到目标数据库.源数据库是采集程序采集而来的原始数据库,所以需 ...
- 3、eclipse 查看原始类出现The jar file rt.jar has no source attachment解决方法
因为rt的source在jdk目录的src.zip文件里,将文件设置为jdk下的src.zip就行了.具体如下 Window>Preferences>Java>Installed J ...
- Cygwin--unix/linux模拟环境
Cygwin是一个在windows平台上运行的类UNIX模拟环境,是cygnus solutions公司开发的自由软件(该公司开发了很多有用的工具,著名的还有eCos,不过现已被Redhat收购).它 ...
- Web Service基础——四种客户端调用方式
通过访问公网服务地址 http://www.webxml.com.cn/zh_cn/index.aspx 来演示四种不同的客户端调用方式 1. 生成客户端调用方式 1.1 Wsimport命令介绍 首 ...
- MySQL主从同步的一个小问题解决
由于历史遗留问题,我们的MySQL主从库的表结构不一致,主库的某个表tableA比从库表tableA少了一个字段. 当尝试在主库上更改表结构时,这行alter语句会随着binlog同步到从库,如果从库 ...
- windows系统定时重启自定义exe程序
工作需要, Windows系统定时重启自定义exe程序. 写了如下程序, 按照说明(readme.txt)修改批处理文件中的四个参数即可: 1.readme.txt 第一个参数:进程名(不用带exe) ...
- XP系统下建立WIFI热点让手机、电脑能上网
http://wenku.baidu.com/view/372c5b1fa300a6c30c229f42.html 这里记录xp系统下建立共享无线网络连接,若是支持手机设备上的话,网络适配器必须是wi ...
- 从使用 KVO 监听 readonly 属性说起
01.KVO 原理 KVO 是 key-value observing 的简写,它的原理大致是: 1.当一个 object(对象) 有观察者时候,动态创建这个 object(对象) 的类的子类(以 N ...