设计键

哈希映射可以很好地按键分组信息。但是我们不能直接使用原始字符串作为键。我们必须设计一个合适的键来呈现字母异位词的类型。例如,有字符串 “eat” 和 “ate” 应该在同一组中。但是 “eat” 和 “act” 不应该组合在一起。

字母异位词分组

给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。

示例:

输入: ["eat", "tea", "tan", "ate", "nat", "bat"],
输出:
[["ate","eat","tea"],["nat","tan"],["bat"]
]

代码:

class Solution {public List<List<String>> groupAnagrams(String[] strs) {List<List<String>> list = new ArrayList<List<String>>();int len = strs.length;if(len<1){return list;}Map<String,List<String>> map = new HashMap<>();//key=排好序的字符 value = 相同字母的list集合String temp = "";for(int i=0;i<len;i++){temp = strs[i];char[] arr = temp.toCharArray();//转数组排序Arrays.sort(arr);temp = new String(arr);//排好序的字符,keyif(map.containsKey(temp)){map.get(temp).add(strs[i]);}else{List<String> l = new ArrayList<>();l.add(strs[i]);map.put(temp,l);}}for(String key:map.keySet()){list.add(map.get(key));}return list;}
}

参考:https://blog.csdn.net/xushiyu1996818/article/details/83617934

补充:

1  Java的Arrays类中有一个sort()方法,该方法是Arrays类的静态方法,在需要对数组进行排序时,非常的好用。

但是sort()的参数有好几种,基本上是大同小异,下面是以int型数组为例的Arrays.sort()的典型用法

增序排列:
int[] ints=new int[]{2,324,4,57,1};
Arrays.sort(ints);
降序排列:(依赖Comparator)
Integer[] integers=new Integer[]{2,324,4,4,6,1};
Arrays.sort(integers, new Comparator<Integer>(){public int compare(Integer o1, Integer o2){return o2-o1;}}});
数组的指定位排序:
int[] ints2=new int[]{212,43,2,324,4,4,57,1};
//对数组的[2,6)位进行排序
Arrays.sort(ints2,2,6);

2

采用HashMap<String,List<String>>,每次将读入的字符串在map中查找(这里需将读入的字符串转化成数组后用sort()来排列好)。

用sort后的字符串作为key,把sort后相同的字符存到字符串集合的list作为value。

最后遍历hashmap,把value添加到结果集中

leetcode探索数组(一)相关推荐

  1. leetcode 探索 数组和字符串 验证回文串

    问题 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, ...

  2. leetcode探索专题中的初级算法练习题(python代码+解题思路)

    本文记录leetcode探索专题中的初级算法练习题,附python实现代码&解题思路,做题过程不免查阅网络资料,侵删~如有错误,欢迎指正交流! 目录 专题一:数组: 26.从排序数组中删除重复 ...

  3. 3、leetcode 697 数组的度

    leetcode 697 数组的度 给定一个非空且只包含非负数的整数数组 nums,数组的度的定义是指数组里任一元素出现频数的最大值. 你的任务是在 nums 中找到与 nums 拥有相同大小的度的最 ...

  4. angularjs 元素重复指定次数_[LeetCode] 442. 数组中重复的数据

    [LeetCode] 442. 数组中重复的数据 题目链接: https://leetcode-cn.com/problems/find-all-duplicates-in-an-array 难度:中 ...

  5. 弟中弟的Leetcode总结——数组类(十一)

    弟中弟的Leetcode总结--数组类(十一) 题目描述 Single Number Given a non-empty array of integers, every element appear ...

  6. 弟中弟的Leetcode总结——数组类(十)

    弟中弟的Leetcode总结--数组类(十) 题目描述 Climbing Stairs You are climbing a stair case. It takes n steps to reach ...

  7. 弟中弟的Leetcode总结——数组类(五)

    弟中弟的Leetcode总结--数组类(五) 题目描述 Remove Duplicates from Sorted Array Given a sorted array nums, remove th ...

  8. 弟中弟的Leetcode总结——数组类(二)

    弟中弟的Leetcode总结--数组类(二) 题目描述 Container With Most Water Given n non-negative integers a1, a2, -, an , ...

  9. 弟中弟的Leetcode总结——数组类(四)

    弟中弟的Leetcode总结--数组类(四) 题目描述 Two Sum Given an array of integers, return indices of the two numbers su ...

  10. 弟中弟的Leetcode总结——数组类(八)

    弟中弟的Leetcode总结--数组类(八) 题目描述 Combination Sum Given a set of candidate numbers (candidates) (without d ...

最新文章

  1. PhoenixGo战胜绝艺,腾讯包揽AI围棋大赛前两名
  2. 从一个男人身上看出他的修养和抱负
  3. 什么是浏览器指纹识别?浏览器指纹识别如何预防
  4. 【很久之前的一篇老文章】一位程序员工作10年总结的13个忠告
  5. Linux时间操作(time、gettimeofday)
  6. 计算机网络课设之TCP通讯录
  7. vb mysql 实例,vb数据库(vb编辑access数据库实例)
  8. 前端使用xlsx导出数据生成Excel文件
  9. ValueError: operands could not be broadcast together with shapes (204,111104
  10. Pandas 库之 DataFrame
  11. 扫地阿姨看完都学会了!写给即将正在找工作的Java攻城狮,砥砺前行!
  12. 外国程序员求助:快 40 了,不知道以后该怎么办
  13. 摩托罗拉Android系统,摩托罗拉Droid官方Android 2.2系统详细功能展示
  14. 技巧心得:在国家核心期刊发表文章应注意(三)
  15. 云标签,关键字图排版 html5 canvas版
  16. Ubuntu系统修改用户名
  17. Axure交互之部件行为
  18. 艾永亮:超级产品战略实现企业超级增长,五个案例给出最佳答案!
  19. 把文件夹名字全部变成大写
  20. Unity手游实战:从0开始SLG——本地化篇(二)TMP的优势与缺点

热门文章

  1. 如何选择最佳云托管服务提供商
  2. nginx开启密码认证
  3. IIS------IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决办法
  4. Scrapy和MongoDB的应用---爬取
  5. uva 436(floyd变形)
  6. CS API 测试3
  7. OEA ORM 框架中的冗余属性设计
  8. 我的FPA性格色彩测试结果
  9. 4.SOA架构:服务和微服务分析及设计--- Web服务及微服务的分析与建模
  10. 6. PHP bcompiler