#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 ...
随机推荐
- python-day4 运算符,if判断, for循环
1.运算符 算术运算符 + - * / % 赋值运算符 = += -= 比较运算符 == < <= > >= != 逻辑 ...
- Y - Anton and Letters
Problem description Recently, Anton has found a set. The set consists of small English letters. Anto ...
- HTML <!DOCTYPE>标签
一般一个基本html页面的结构,如下代码所示: <html> <head> <title>我是基本的页面结构</title> </head> ...
- Vue蚂蜂窝Vue-cli+webpack做的
先来看下效果 项目地址 喜欢star一下哦
- vim的快捷键大全
vim是开发利器,掌握快捷可以事半功倍,这里总结下常用的快捷键,提高开发速度这里写代码片 1.vim ~/.vimrc 进入配置文件 如果不知道vimrc文件在哪,可使用 :scriptnames 来 ...
- Centos7.5 在桌面创建AndroidStudio快捷方式
Centos7 在桌面创建AndroidStudio快捷方式 前言 最近安装了Centos7,打算将开发平台转移到Linux下,安装好AndroidStudio后,桌面没有快捷方式有些不习惯,随自己创 ...
- 解决 C# webbrowser 弹出json下载问题
把以下内容保存为 .reg ,然后导入注册表,即可解决C# webbrowser 弹出json下载问题,也可通过程序修改. Windows Registry Editor Version 5.00 [ ...
- vue-awesome-swiper组件的使用
一.轮播图组件是这样安装的 npm i --save-dev vue-awesome-swiper main.js里面 import 'swiper/dist/css/swiper.css' impo ...
- scrapy爬虫--10分钟入门
# -*- coding: utf-8 -*- # @Time : 2019/4/18 9:10 # @Author : wujf # @Email : 1028540310@qq.com # @Fi ...
- [接口管理平台] eoLinker AMS 专业版 V3.5 :加入数据结构管理、通用函数管理、API 快速测试等近 30 项更新
eoLinker AMS是集API文档管理.API自动化测试.开发协作三位一体的综合API开发管理平台,是中国最大的在线API管理平台.目前eoLinker AMS已经为来自全球的超过两万家企业托管超 ...