leetcode 49 Group Anagram
lc49 Group Anagram
逻辑很简单,就是统计字母出现次数,然后将完全相同的字符串放入同一list
关键是怎么实现
统计的部分,可以通过将string排序,Arrays.sort(),或者像之前int[26]一样,
那么如何一次遍历,就能将相同字符串放入一个list呢?
这里用到了HashMap<String, List<String>>,String为key,用来判断排序过后的str是否相同,若是相同,直接原来的str加入List<String>
最后返回值就是把map的value转成List<List<String>>
可以用到一个小方法:
return new ArrayList<List<String>>(map.values())
因为HashMap的value都是List<String>
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> res = new ArrayList<>();
if(strs.length == 0)
return res;
HashMap<String, List<String>> map = new HashMap<>();
for(int i=0; i<strs.length; i++){
char[] tmp = strs[i].toCharArray();
Arrays.sort(tmp);
String key = String.valueOf(tmp);
if(!map.containsKey(key))
map.put(key, new ArrayList<String>());
map.get(key).add(strs[i]);
}
return new ArrayList<List<String>>(map.values());
}
}
leetcode 49 Group Anagram的更多相关文章
- [LeetCode] 49. Group Anagrams 分组变位词
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- LeetCode - 49. Group Anagrams
49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...
- leetcode@ [49] Group Anagrams (Hashtable)
https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...
- [leetcode]49. Group Anagrams变位词归类
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- LeetCode 49 Group Anagrams(字符串分组)
题目链接: https://leetcode.com/problems/anagrams/?tab=Description Problem:给一个字符串数组,将其中的每个字符串进行分组,要求每个分 ...
- [Leetcode] 49. Group Anagrams_Medium
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- [leetcode]49. Group Anagrams重排列字符串分组
是之前的重排列字符串的延伸,判断是重排列后存到HashMap中进行分组 这种HashMap进行分组的方式很常用 public List<List<String>> groupA ...
- [LeetCode] 249. Group Shifted Strings 分组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- LeetCode 49: 字母异位词分组 Group Anagrams
LeetCode 49: 字母异位词分组 Group Anagrams 题目: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. Given an array o ...
随机推荐
- 解决 php Call to undefined function shm_attach()
学习php 多进程的时候,运行脚本报错 Call to undefined function shm_attach() ,搜了一下,看到stack overflow 里面提示需要配置这些扩展 exte ...
- 校园商铺-4店铺注册功能模块-1Dao层之更新店铺
dao层增加更新店铺的方法 package com.csj2018.o2o.dao; import com.csj2018.o2o.entity.Shop; public interface Shop ...
- ASP.NET的底层体系2
文章引导 1.ASP.NET的底层体系1 2.ASP.NET的底层体系2 引言 接着上一篇ASP.NET的底层体系1我们继续往下走 一.System.Web.HttpRuntime.ProcessRe ...
- poj-3468-A Simple Problem with Integers-线段树入门+区间更新
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- 如何利用开源思想开发一个SEO友好型网
如果你有一个网站需要去做SEO优化的时候,不要期望你的努力能立即得到回报.耐心等待并更正内容营销策略,最终会发现你的网站很受用户欢迎.下面就教你如何利用开源思维开发一个SEO友好型网站! 首先,你应该 ...
- 初识OpenCV-Python - 006: 图像的几何变换
本次小节学习了图像的变换,主要应用到如下方法: cv2.resize(), cv2.warpAffine(), cv2.getRotationMatrix2D(), cv2.getAffineTran ...
- Algo: Dynamic programming
Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...
- Android开发 从代码里设置Drawable图片不显示的问题
问题描述 我们从代码里获得Drawable在设置给View时会发现,图片不显示的问题.比如如下代码: Drawable drawable = getResources().getDrawable(R. ...
- 在双重for循环内部使用async异步请求axios中遇到的问题
在methods中的方法 async getPro () { let _this = this let newArr = [] await axios.get(`api/v1/dailyProTbms ...
- [转]Entity Framework教程(第二版)
源起 很多年前刚毕业那阵写过一篇关于Entity Framework的文章,没发首页却得到100+的推荐.可能是当时Entity Framework刚刚发布介绍EF的文章比较少.一晃这么多年过去了,E ...