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的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. Laravel5.1学习笔记10 系统架构2 应用程序结构

    应用程序结构 简介 根目录 App 目录 为应用程序设置命名空间 简介 默认的 Laravel 应用程序结构是为了给无论构建大型还是小型应用程序都提供一个良好的开始.当然,你可以依照喜好自由地组织应用 ...

  2. 浏览器缓存机制(HTTP缓存机制)

    其机制是根据HTTP报文的缓存标识进行的. 过程:浏览器发起HTTP请求 – 服务器响应该请求.那么浏览器第一次向服务器发起该请求后拿到请求结果,会根据响应报文中HTTP头的缓存标识,决定是否缓存结果 ...

  3. Android Retrofit 2.0文件上传

    Android Retrofit 实现(图文上传)文字(参数)和多张图片一起上传 使用Retrofit进行文件上传,肯定离不开Part & PartMap. public interface ...

  4. wamp中的mysql服务与原来安装的mysql服务冲突的解决办法

    如果原来机器上已经安装了mysql,在安装wamp之后,打开wamp上的mysql时会打不开,或者会将原来安装的mysql服务关闭.原因是两个mysql共用了3306端口,解决办法是更改其中的一个端口 ...

  5. sql server 数据库优化--显示执行计划

      刚开始用SQL Server的时候,我没有用显示执行计划来对查询进行分析.我曾经一直认为我递交的SQL查询都是最优的,而忽略了查询性能究竟如何,从而对“执行计划”重视不够.在我职业初期,我只要能获 ...

  6. classNum 表示学生的班号,例如“class05”。 有如下List  List list = new ArrayList();

    package a927; import java.util.ArrayList; import java.util.List; class Student { private String name ...

  7. Metric Learning度量学习:**矩阵学习和图学习

    DML学习原文链接:http://blog.csdn.net/lzt1983/article/details/7884553 一篇metric learning(DML)的综述文章,对DML的意义.方 ...

  8. 【sqli-labs】 less32 GET- Bypass custom filter adding slashes to dangrous chars (GET型转义了'/"字符的宽字节注入)

    转义函数,针对以下字符,这样就无法闭合引号,导致无法注入 ' --> \' " --> \" \ --> \\ 但是,当MySQL的客户端字符集为gbk时,就可能 ...

  9. vue-router 嵌套路由没反应

    先看下route.js //route.js const App = () => import('../App.vue'); const Login = () => import('../ ...

  10. 查看占用某端口的进程——netstat、findstr 的使用

    netstat   检验本机各端口的网络连接情况 -a 显示所有连接和侦听端口(如Windows共享服务 的135,445端口) -n 不进行IP地址到主机名的解析 -o 显示拥有的与每个连接关联的进 ...