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

Note: All inputs will be in lower-case.

他的意思就是回文构词法,即单词里的字母的种类和数目没有改变,仅仅是改变了字母的排列顺序。
input= ["abc", "bca", "bac", "bbb", "bbca", "abcb"]
output=["abc", "bca", "bac", "bbca", "abcb"]
我们仅仅须要一个HashMap就能够。

public class Solution {
public List<String> anagrams(String[] strs) {
ArrayList<String> result = new ArrayList<String>();
HashMap<String, String> hm = new HashMap<String, String>();
for (String str : strs) {
char[] c = str.toCharArray();
Arrays.sort(c);
if (!hm.containsKey(String.valueOf(c))) {
hm.put(String.valueOf(c), str);
} else {
String s = hm.get(String.valueOf(c));
if (!result.contains(s))//某个回文序列第一次出现的单词,
//我们并没有加入,如今补上
result.add(s);
result.add(str);
}
}
return result;
}
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

LeetCode 48 Anagrams的更多相关文章

  1. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  2. 前端与算法 leetcode 48. 旋转图像

    目录 # 前端与算法 leetcode 48. 旋转图像 题目描述 概要 提示 解析 解法一:转置加翻转 解法二:在单次循环中旋转 4 个矩形 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...

  3. [LeetCode] Group Anagrams 群组错位词

    Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...

  4. [LeetCode] 48. Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  5. 【leetcode】Anagrams

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

  6. 【leetcode】Anagrams (middle)

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

  7. Java for LeetCode 049 Anagrams

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

  8. Leetcode#49 Anagrams

    原题地址 Anagram:变位词.两个单词是变位词关系的条件是:组成单词的字符相同,只是顺序不同 第一次看这道题看了半天没明白要干嘛,丫就不能给个样例输入输出么..后来还是看网上其他人的总结知道是怎么 ...

  9. LeetCode 48. Rotate Image(旋转图像)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

随机推荐

  1. uva10635 LCS映射转LIS

    题目给定 2个序列,要我们求LCS,但是序列的长度最长是250*250, LCS的时间复杂度是O(N*N),所以无法解决 我们可以第一个序列的数字,按位置,映射为1.2.3.4.5.6.7.8.9 那 ...

  2. NET的可运行于树莓派

    Raspkate - 基于.NET的可运行于树莓派的轻量型Web服务器   最近在业余时间玩玩树莓派,刚开始的时候在树莓派里写一些基于wiringPi库的C语言程序来控制树莓派的GPIO引脚,从而控制 ...

  3. HDU4144:Bacon's Cipher

    Problem Description Bacon's cipher or the Baconian cipher is a method of steganography (a method of ...

  4. PL/SQL 游标的使用

     游标的使用 ①游标概念 为了处理SQL 语句,ORACLE 必须分配一片叫上下文( context area )的区域来处理所必需的信息, 当中包含要处理的行的数目.一个指向语句被分析以后的表示 ...

  5. HttpClient使用具体解释

    Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,添加�了易用性和灵活性(详细差别,日后我们再讨论),它不仅是client发送Http请求变得e ...

  6. HDU 3681 BFS&amp;像缩进DP&amp;二分法

    N*M矩阵.从F出发点.走完全部Y点.每个人格开支1电源点,去G点,电池充满,D无法访问.最小的开始问什么时候满负荷可以去完全部Y.Y和G总共高达15一 第一BFS所有的F.Y.G之间的最短距离. 然 ...

  7. 10令人惊叹的模型的影响HTML5应用程序及源代码

    HTML5已经越来越流行起来了.尤其是移动互联网的发展,更是带动了HTML5的迅猛发展,我们也是时候学习HTML5了,以防到时候落伍.今天给大家介绍10款效果惊艳的HTML5应用.方便大家学习,也将应 ...

  8. js缓冲运动

    缓冲运动 现象:逐渐变慢,最后停止 原理:距离越远,速度越大 速度的计算方式: 1,速度由距离决定 2,速度=(目标值-当前值)/缩放系数 说明:速度为正负数时,也决定了物体移动的方向 示例:div缓 ...

  9. DHCP Option 60 认识

    原文地址: http://blog.163.com/chenqioulin_1983/blog/static/83216232010109104430251/   首先还是看看RFC咋说的吧.DHCP ...

  10. WPF技术触屏上的应用系列(四): 3D效果图片播放器(图片立体轮放、图片立体轮播、图片倒影立体滚动)效果实现

    原文:WPF技术触屏上的应用系列(四): 3D效果图片播放器(图片立体轮放.图片立体轮播.图片倒影立体滚动)效果实现 去年某客户单位要做个大屏触屏应用,要对档案资源进行展示之用.客户端是Window7 ...