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. w3cschool javascript学习

    name与Id属性区别 1.我们知道在网页做Post提交时,是以Form(即表单域)为单位进行提交的,一个Form里有若干个表单对象(如<input type="text" ...

  2. [BZOJ2661][BeiJing wc2012]连连看 费用流

    2661: [BeiJing wc2012]连连看 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1349  Solved: 577[Submit][ ...

  3. 山东省第八届省赛 A:Return of the Nim(尼姆+威佐夫)

    Problem Description Sherlock and Watson are playing the following modified version of Nim game: Ther ...

  4. PowerDesigner用例图展示设置

    powerdesigner用例图显示设置 powerdesigner中的绘图功能真是不敢恭维,折线半天弄不直,直线半天弄不弯. 1.修改显示设置 tools-->display preferen ...

  5. sqlmap的小试牛刀

    这次算是一次用sqlmap的例子,写的很水. 目的:通过工具扫描到了后台的数据库的地址(如下图),想通过sqlmap找到数据库的用户和密码进入到数据库(首先可以尝试一下root:root一般都是这个情 ...

  6. HSV做通道分离是出现的Vector内存越界错误

    vector<Mat> hsvSplit; //因为我们读取的是彩色图,直方图均衡化需要在HSV空间做 split(imgHSV, hsvSplit); equalizeHist(hsvS ...

  7. [BZOJ1095]捉迷藏

    点了动态点分治的科技树,这道题是树形态不变的动态点分治,形态变化的话...待会补 考虑点分治过程中的这样一种结构:按递归层次把当前层的重心与上层重心互相连接,这就是点分治树,容易看出它的树高只有$O( ...

  8. 【Treap模板详细注释】BZOJ3224-普通平衡树

    模板题:D错因见注释 #include<iostream> #include<cstdio> #include<cstring> #include<algor ...

  9. 8.4(Java学习笔记)java脚本引擎(Rhino)

    一.java脚本引擎 java脚本引擎是沟通java和脚本语句之间的桥梁,可以通过对应的脚本引擎在java中调用各种脚本语言. 二.脚本引擎执行脚本代码 ScriptEngineManager:为Sc ...

  10. HDU 4606 Occupy Cities (计算几何+最短路+二分+最小路径覆盖)

    Occupy Cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...