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 ...
随机推荐
- (25)Teach girls bravery, not perfection
https://www.ted.com/talks/reshma_saujani_teach_girls_bravery_not_perfection/transcript00:12So a few ...
- C#项目学习记录
1, Visual Studio Code 添加VS 2017的开发人员命令提示符---C#编译环境 2, C#编译器和CLI的安装 注意:自己的电脑上配置环境变量时,配置在系统变量的Path中 ...
- jQuery源码框架fn解读
(function( window, undefined ){ var jQuery = (function(){ var jQuery = function( selector, context ) ...
- C++ Thrift服务端记录调用者IP和被调接口方法
Apache开源的Thrift(http://thrift.apache.org)有着广泛的使用,有时候需要知道谁调用了指定的函数,比如在下线一起老的接口之前,需要确保对这些老接口的访问已全部迁移到新 ...
- iptables简单用法
iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 &l ...
- Metasploit Framework(3)Meterpreter
文章的格式也许不是很好看,也没有什么合理的顺序 完全是想到什么写一些什么,但各个方面都涵盖到了 能耐下心看的朋友欢迎一起学习,大牛和杠精们请绕道 Meterpreter:是一种高级的,动态的,可拓展的 ...
- nginx配置tp5的pathinfo模式并隐藏后台入口文件
server { listen 2223; server_name manage; access_log /data/wwwlogs/access_manage.log combined; root ...
- 数字(Number)类型(一)
多行语句 Python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠(\)来实现多行语句,例如: total = item_one + \ item_two + \ item_three ...
- 封装手风琴!使用jQuery!
//封装手风琴 /** * * * */ $.fn.accordion = function (colors, width) { var width=width||0; var colors= col ...
- NotSupportedError Only secure origins are allowed
今天在写H5调用手机摄像头时提示一个错误信息如下: NotSupportedError Only secure origins are allowed (see: https://goo.gl/Y0Z ...