Anagrams leetcode java
题目:
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
题解:
这道题看所给的字符串数组里面有多少个是同一个变形词变的。这道题同样使用HashMap来帮助存老值和新值,以及帮忙判断是否是变形词。
首先对每个string转换成char array然后排下序,HashMap里面的key存sort后的词,value存原始的词。然后如果这个排好序的词没在HashMap中出现过,那么就把这个sorted word和unsortedword put进HashMap里面。如果一个sorted word是在HashMap里面存在过的,说明这个词肯定是个变形词,除了把这个词加入到返回结果中,还需要把之前第一个存进HashMap里面的value存入result中。
代码如下:
1 public ArrayList<String> anagrams(String[] strs) {
2 ArrayList<String> result=new ArrayList<String>();
3
4 if (strs==null||strs.length==0)
5 return result;
6
7 HashMap<String,ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
8
9 for (String s:strs){
char[] temp=s.toCharArray();
Arrays.sort(temp);
String tempStr=new String(temp);
if (hm.containsKey(tempStr)){
if(hm.get(tempStr).size() == 1)
result.add(hm.get(tempStr).get(0));
hm.get(tempStr).add(s);
result.add(s);
}else{
ArrayList<String> tempList=new ArrayList<String>();
tempList.add(s);
hm.put(tempStr, tempList);
}
}
return result;
}
Anagrams leetcode java的更多相关文章
- Group Anagrams - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Group Anagrams - LeetCode 注意点 字母都是小写的 解法 解法一:用一个字符串表示strs[i]中出现的字母,比如:abc-> ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- 49. Group Anagrams - LeetCode
Question 49. Group Anagrams Solution 思路:维护一个map,key是输入数组中的字符串(根据字符排好序) Java实现: public List<List&l ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Anagrams [LeetCode]
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
随机推荐
- BoneBlack am335x利用SD卡烧写板卡上的emmc
参考ti论坛上面的一篇文章: 链接:https://pan.baidu.com/s/1SLSUbCRrIULJJf_BNI3sEQ 密码: hvem 自己稍微修改的debrick.sh 链接: htt ...
- “公共语言规范”(CLS)
一.什么是“公共语言规范”(CLS) 定义了一个最小公共集,任何编译器只有支持这个功能集,生成的类型才能兼容其他符合CLS.面向CLR的语言生成的组件 二.CLS规则 类型的每个成员要么是字段(数据) ...
- DHTML和HTML有什么区别?有什么不同
DHTML和HTML有什么区别?有什么不同 首先Dynamic HTML是一种制作网页的方式,而不是一种网络技术(就像JavaScript和ActiveX):它也不是一个标记.一个插件或者是一个浏览器 ...
- zoj 3537 区间dp+计算几何
题意:给定n个点的坐标,先问这些点是否能组成一个凸包,如果是凸包,问用不相交的线来切这个凸包使得凸包只由三角形组成,根据costi, j = |xi + xj| * |yi + yj| % p算切线的 ...
- hdu 2112 最短路
本来是拿来复习一下map的,没想搞了半天,一直wa,最后发现预处理没有处理到所有的点 就是个最短路 Sample Input 6 xiasha westlake xiasha station 60 x ...
- ThinkPHP实现登录限制时__construct和_initialize的区别
ThinkPHP支持两种构造方法: __construct和_initialize(ThinkPHP内置的构造方法). 测试URL为: http://oa.com/index.php/Admin/ ...
- How do I use Tasker to run a sync in FolderSync?
First of all the full version is required. The full version works as a Tasker plugin - when you ...
- gcc 内联汇编
http://www.cnblogs.com/zhuyp1015/archive/2012/05/01/2478099.html
- iOS中bundle的意义
什么是bundle? bundle就是一个文件夹,按照一定标准组织的目录结构.每个iOS APP至少有一个main bundle,这个main bundle包含了app的二进制代码及任何你用到的资源, ...
- MVC到底使用哪种方式传递Model,在ViewData、ViewBag、PartialView、TempData、ViewModel、Tuple之间取舍
在"MVC控制器传递多个Model到视图,使用ViewData, ViewBag, 部分视图, TempData, ViewModel, Tuple"中,体验了使用不同的方式传递多 ...