leetcode — anagrams
import java.util.*;
/**
*
* Source : https://oj.leetcode.com/problems/anagrams/
*
* Created by lverpeng on 2017/7/18.
*
* Given an array of strings, group anagrams together.
*
* For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
* Return:
*
* [
* ["ate", "eat","tea"],
* ["nat","tan"],
* ["bat"]
* ]
*
* Note:
*
* For the return value, each inner list's elements must follow the lexicographic order.
* All inputs will be in lower-case.
*
* Update (2015-08-09):
* The signature of the function had been updated to return list<list<string>> instead
* of list<string>, as suggested here. If you still see your function signature return
* a list<string>, please click the reload button to reset your code definition.
*
*/
public class GroupAnagram {
public List<String[]> anagram (String[] strArr) {
List<String[]> result = new ArrayList<String[]>();
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
for (int i = 0; i < strArr.length; i++) {
char[] charArr = strArr[i].toCharArray();
Arrays.sort(charArr);
String str = new String(charArr);
if (map.keySet().contains(str)) {
map.get(str).add(i);
} else {
List<Integer> list = new ArrayList<Integer>();
list.add(i);
map.put(str, list);
}
}
for (Map.Entry<String, List<Integer>> entry : map.entrySet()) {
String[] strs = new String[entry.getValue().size()];
int index = 0;
for (Integer i : entry.getValue()) {
strs[index] = strArr[entry.getValue().get(index)];
index ++;
}
Arrays.sort(strs);
result.add(strs);
}
return result;
}
public static void printList (List<String[]> list) {
for (String[] strs : list) {
System.out.println(Arrays.toString(strs));
}
System.out.println();
}
public static void main(String[] args) {
GroupAnagram groupAnagram = new GroupAnagram();
printList(groupAnagram.anagram(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}));
}
}
leetcode — anagrams的更多相关文章
- [LeetCode] Anagrams 错位词
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- [leetcode]Anagrams @ Python
原题地址:https://oj.leetcode.com/problems/anagrams/ 题意: Given an array of strings, return all groups of ...
- Leetcode: Anagrams(颠倒字母而成的字)
题目 Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...
- LeetCode——Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- Leetcode Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- LeetCode ---Anagrams() 详解
Notice: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...
- LeetCode Anagrams My solution
Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...
- LeetCode: Anagrams 解题报告
AnagramsGiven an array of strings, return all groups of strings that are anagrams. Note: All inputs ...
- [Leetcode] Anagrams 颠倒字母构成词
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
随机推荐
- strchr和strstr 函数
函数原型:extern char *strchr(char *str,char character) 参数说明:str为一个字符串的指针,character为一个待查找字符. 所在库名: ...
- Maven之pom.xml配置文件详解
此文非原创,摘自:https://www.baidu.com/link?url=GlGgW21nijIiULDZj0RfPH8ofqGMqEnAzXiym7O3hfrZM5nFH2enukemBNTX ...
- MySQL PROFILE 跟踪语句各阶段性能开销
PROFILE 可以跟踪查询语句各个阶段 Time,IO,CPU,MEMORY 等资源使用情况,比较详细.所以系统一般不会记录太多.启用是全局的,所以每个连接都保持语句的资源使用情况. 查看 PRO ...
- PHP与Excel 笔记
一: PHP将数据导出Excel表中(投机型) 二: PHPExcel: Github上可以下载此插件包,用法如下: 前端: //上传阅卷员Excel文件 $("#upload_memb ...
- Unity3D协程yield的理解
Unity3D的协程概括地将就是:对于一段程序,你可以加上yield标明哪里需要暂停,然后在下一帧或者一段时间后,系统会继续执行这段代码.协程的作用:①延迟一段时间执行代码.②等某个操作完成之后再执行 ...
- Filter的使用(web作业)
一:什么是过滤器 Filter:Servlet过滤器Fileter是一个小型的web组件,它们通过拦截请求和响应,以便查看.提取或以某种方式操作客户端和服务器之间交换的数据,实现“过滤”的功能.Fil ...
- Note | Git
目录 1. 出发 A. 安装 B. 设置机器身份 C. 创建版本(仓)库 repository D. 可管理文件 2. 基础操作 A. 添加文件至仓库 B. 修改文件并查看修改 C. 查看历史变动 D ...
- python2 python3区别
Python开发团队将在2020年1月1日停止对Python2.7的技术支持,但python2的库仍然比较强大(在 pip 官方下载源 pypi 搜索 Python2.7 和 Python3.5 的第 ...
- OpenCV Mat格式存储YUV图像
YUV图像用的比较多,而且YUV图像的格式众多(YUV格式可以参考YUV pixel formats),如何用OpenCV的Mat类型来存储YUV图像也是经常遇到的问题. 对于YUV444图像来说,就 ...
- Python 基础整理(未完)
数据类型和变量: 整数:Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样,例如:1,100,-8080,0,等等. 计算机由于使用二进制,所以,有时候用十六 ...