#leetcode#Anagrames
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
分析:推断两个String是不是anagrams,比較简单的方法就是先转换成charArray,然后排序,然后又一次生成String。看是否同样。 这个String能够作为Key, Value放原String。
这里我犯了一个错误,就是推断是否要加当前value时推断 list.size() 是否为0, 事实上应该推断size是否大于等于2, 也就是是否大于1。 由于至少出现一对String才算一个group啊.... 差之毫厘,失之千里 !!
!
另外一个地方须要注意就是对HashMap的遍历方法, 能够用Iterator。 也能够直接用map.values(),
假设用Iterator,则最后的遍历部分代码为:
Iterator iter = map.values().iterator();
while(iter.hasNext()){
List<String> list = (List<String>)iter.next();
if(list.size() > 1){
res.addAll(list);
}
}
public class Solution {
public List<String> anagrams(String[] strs) {
List<String> res = new ArrayList<>();
if(strs == null || strs.length == 0){
return res;
}
Map<String, List<String>> map = new HashMap<>();
for(String str : strs){
char[] arr = str.toCharArray();
Arrays.sort(arr);
String tmp = new String(arr);
if(!map.containsKey(tmp)){
List<String> item = new ArrayList<>();
item.add(str);
map.put(tmp, item);
}else{
map.get(tmp).add(str);
}
}
for(List<String> list : map.values()){
// if(list.size() != 0){
if(list.size() > 1){
res.addAll(list);
}
}
return res;
}
}
#leetcode#Anagrames的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- Redis(三)-Ubuntu下安装
Ubuntu 下安装 在 Ubuntu 系统安装 Redi 可以使用以下命令: $sudo apt-get update $sudo apt-get install redis-server 启动 R ...
- 6月10遇到的bug
3.遇到的BUG 1.@Service(version = "1.0.0", interfaceClass = TransactionItemService.clas ...
- Django学习案例一(blog):五. 开发主页(博客列表展示)
主页是一个“博客列表”页.博客要按发布时间的倒序来排列,每个博客都要包含标题.作者.分类.发布时间的显示(年-月-日 时:分)及节选的正文内容(前 100 个字).点击单独的博客可以进入其详情页. 1 ...
- 网页前端状态管理库Redux学习笔记(一)
最近在博客园上看到关于redux的博文,于是去了解了一下. 这个Js库的思路还是很好的,禁止随意修改状态,只能通过触发事件来修改.中文文档在这里. 前面都很顺利,但是看到异步章节,感觉关于异步说得很乱 ...
- python监听鼠标和键盘
import PyHook3 def OnMouseEvent(event): print('MessageName:',event.MessageName) print('Message:',eve ...
- 红黑联盟 php相关资讯
http://www.2cto.com/tag/phpbanben.html
- OAuth四种模式
授权码模式(authorization code)----适用于网站服务端去oauth服务端申请授权 简化模式(implicit)----没有服务端,js+html页面去oauth服务端申请授权 密码 ...
- Vue2 封装的 Quill 富文本编辑器组件 Vue-Quill-Editor
1.安装 npm install vue-quill-editor --save 2.使用 import { quillEditor } from 'vue-quill-editor' 3.组件中 & ...
- SSM项目中表单分页操作(PageHepler使用)
Maven pom.xml添加依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifa ...
- 关于DataGridViewComboBoxColumn的二三事
近日开发一个基于WinForm的工具,用到了DataGridViewComboBoxColumn. 关于数据: DataGridView的数据源是代码生成的DataTable DataGridView ...