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 ... ...
随机推荐
- javaWeb 批量下载图片
批量下载网页图片 CreateTime--2017年9月26日15:40:43 Author:Marydon 所用技术:javascript.java 测试浏览器:chrome 开发工具:Ecli ...
- 23、java 通过System.getProperties()获取系统参数
1.java的System.getProperty()方法可以获取的值 java.version Java 运行时环境版本 java.vendor Java 运行时环境供应商 java.vendor. ...
- Swing(Java)--维基百科
Swing是一个为Java设计的GUI工具包.Swing是Java基础类的一部分.Swing包括了图形用户界面(GUI)组件如:文本框,文本域,按钮,分隔窗格和表. Swing提供许多比AWT更好的屏 ...
- java socket输入输出中文乱码问题
http://hi.baidu.com/linjk03/item/e2028bfd990c14ea1a111feb 统一了输入输出的编码格式,是不会有乱码问题出现的. 构造Reader或Write ...
- 下载url地址的图片
// string url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=cNaaMfvhpb1vmcVRXRAdI ...
- AOV网与拓扑排序
在一个表示工程的有向图中,用顶点表示活动,用弧表示活动之间的优先关系,这样的有向图为顶点表示活动的网,我们称之为AOV网(Activity on Vextex Network).AOV网中的弧表示活动 ...
- 浅析I/O模型
以下是本文的目录大纲: 一.什么是同步?什么是异步? 二.什么是阻塞?什么是非阻塞? 三.什么是阻塞IO?什么是非阻塞IO? 四.什么是同步IO?什么是异步IO? 五.五种IO模型 六.两种高性能IO ...
- SqlServer 获取汉字的拼音首字母
一.该函数传入字符串,返回数据为:如果为汉字字符,返回该字符的首字母,如果为非汉字字符,则返回本身.二.用到的知识点:汉字对应的UNICODE值,汉字的排序规则.三.数据库函数: CREATE FUN ...
- 使用NuGet发布自己的.NET NuGet 包( .NET Standard & Windows)
发布自己的nuget包 STEP 1:获取API Key 首先,你需要到NuGet上注册一个新的账号,然后在My Account页面,获取一个API Key,或者是自建服务器上生成一个API Key( ...
- mark Pay http://git.oschina.net/littleCrazy/dianshangpingtai-zhifu/blob/master/OrderPayController
@Controller @RequestMapping("/api/pay/") public class OrderPayController extends BaseContr ...