Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.

原题链接:https://oj.leetcode.com/problems/anagrams/

易位构词游戏的英文词汇是 anagram,这个词来源于有“反向”或“再次”的含义的希腊语字根ana-和有“书写”、“写下”的意思的词根grahpein。易位构词是一类

title=%E6%96%87%E5%AD%97%E6%B8%B8%E6%88%8F&action=edit&redlink=1" class="new" title="文字游戏(页面不存在)" style="text-decoration:none; color:rgb(165,88,88); font-family:sans-serif; font-size:14px; line-height:22.399999618530273px">文字游戏(更准确地说是一类“词语游戏”)。是将组成一个词或短句的字母又一次排列顺序,原文中全部字母的每次出现都被使用一次,这样构造出另外一些新的词或短句。

http://zh.wikipedia.org/wiki/%E6%98%93%E4%BD%8D%E6%9E%84%E8%AF%8D%E6%B8%B8%E6%88%8F

	public List<String> anagrams(String[] strs) {
List<String> list = new ArrayList<String>();
Map<String,List<String>> map = new HashMap<String,List<String>>();
for(String str : strs){
char[] ch = str.toCharArray();
Arrays.sort(ch);
String s = new String(ch);
if(map.containsKey(s))
map.get(s).add(str);
else{
List<String> li = new ArrayList<String>();
li.add(str);
map.put(s,li);
}
}
for(List<String> ls : map.values()){
if(ls.size() > 1)
list.addAll(ls);
}
return list;
}

LeetCode——Anagrams的更多相关文章

  1. [LeetCode] Anagrams 错位词

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  2. leetcode — anagrams

    import java.util.*; /** * * Source : https://oj.leetcode.com/problems/anagrams/ * * Created by lverp ...

  3. [leetcode]Anagrams @ Python

    原题地址:https://oj.leetcode.com/problems/anagrams/ 题意: Given an array of strings, return all groups of ...

  4. Leetcode: Anagrams(颠倒字母而成的字)

    题目 Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...

  5. Leetcode Anagrams

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  6. LeetCode ---Anagrams() 详解

    Notice: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...

  7. LeetCode Anagrams My solution

    Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...

  8. LeetCode: Anagrams 解题报告

    AnagramsGiven an array of strings, return all groups of strings that are anagrams. Note: All inputs ...

  9. [Leetcode] Anagrams 颠倒字母构成词

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

随机推荐

  1. hdu 5167(dfs)

    Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  2. Android xmlpull 方式解析xml文件

    1.新建一个xml文件,放在res/xml目录下 <?xml version="1.0" encoding="utf-8"?> <citys& ...

  3. java 获取当前系统时间

    Java的Date获取时间函数都是deprecated 可以使用: https://stackoverflow.com/questions/5175728/how-to-get-the-current ...

  4. Python 进阶 之 enumerate()函数

    enumerate()是Python的内置函数,无需依赖包,enumerate()作用是可以将生成器包装成生成器,类似于range,但enumerate()可以自动生成索引. enumerate(pa ...

  5. thinkphp函数学习(0)——开篇

    因为新公司都使用thinkphp,所以就想通读一遍源码,可是在读的过程中,时常半路杀出个自定义函数,然后又要跳到函数定义的地方先看具体的函数定义,感觉特别的难受,好几次都是看到runtime.php就 ...

  6. IActionResult

  7. FZU 2150 Fire Game 【两点BFS】

    Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns) ...

  8. 【KM】POJ2195/HDU1533-Going home

    //最近没什么时间quq据说长得帅的人都在切八中,然而长得丑的人只能水水裸题 [题目大意] 给出一张地图及人和房屋的位置,求出每个人回到不同房屋所具有的最小代价和. [思路] 最小权匹配,先O(n^2 ...

  9. Mybatis添加&&删除&&更新

    mapper <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC & ...

  10. Spring.NET的IoC容器(The IoC container)——简介(Introduction)

    简介 这个章节介绍了Spring Framework的控制反转(Inversion of Control ,IoC)的实现原理. Spring.Core 程序集是Spring.NET的 IoC 容器实 ...