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. Azure Command Line(Azure CLI)指南

    1.安装. MSI安装程序:https://aka.ms/installazurecliwindows https://docs.microsoft.com/zh-cn/cli/azure/insta ...

  2. XOJ测试 2016.5.22

    哈哈 我是最先使用XOJ的人之一 膜拜zrt ing 首先是XOJ神奇的界面 还没有建设完的OJ是这个样子的 一共有5道题 这次小测有3道题 是T2T3T4 首先是骑士精神 (BZOJ1085) 上来 ...

  3. 2.sql server的管理

    sql server的管理:需要安装sql server 2005或者sql server 2008,若要使用sqlserver管理工具进行开发还要安装sql server management st ...

  4. SQLServer2008 使用BCP导入导出表数据

    --先开启cmdshell EXEC sp_configure 'show advanced options', 1 GO RECONFIGURE GO EXEC sp_configure 'xp_c ...

  5. js动态操作订单表格

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. IIS日志分析:SC-Status语义

    在网站属性-网站-日志(属性) 中进行设定该站点IIS日志常规属性和扩展属性,扩展属性设置IIS日志包含字段显示. HTTP协议状态(sc-status)码的含义  IIS中 100 Continue ...

  7. GrepWin:Win7下的文本替换工具

    工作环境退回到Win7之后,内容查找功能非常不给力,推荐一个文本内容查找工具grepWin. Win7下的文本查找/替换工具: grepWin

  8. OpenCv:椭圆上点的计算方程

    椭圆         椭圆(Ellipse)是平面内到定点F1.F2的距离之和等于常数(大于|F1F2|)的动点P的轨迹,F1.F2称为椭圆的两个焦点.其数学表达式为:                 ...

  9. 【sqli-labs】 less13 POST - Double Injection - Single quotes- String -twist (POST型单引号变形双注入)

    报错 闭合掉括号 这关登录成功之后不显示登录的用户名密码了

  10. C 语言复杂声明

    int board [8] [8] ; //声明一个内含 int 数组的数组 int ** ptr ; //声明一个指向指针的指针,被指向的指针指向 int int * risks [10] ; // ...