Anagrams

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

Note: All inputs will be in lower-case.

我的解题思路是这种:1.事实上所谓的anagrams就是字母同样就ok了

2.把每一个数组里面的数字以字典序进行又一次排序形成fingerprint

3.推断排序过得串是否在hashmap里面,假设在里面就加一,说明这个fingerprint 有了

有的话表明这个fingerprint相应的String是anagrams

4.用strs里面的string 扫一下map ,假设fingerprint相应的数目大于1,

则表示这个String就是所要的了,记录结果就好。

用到的函数

String.toCharArray();

Arrays.toString();

Hashmap.containsKey();

Hashmap.put(String,Integer);

public class Solution {
public List<String> anagrams(String[] strs) {
List<String> result = new ArrayList<String>();
if (strs == null || strs.length == 0 ) {
return result;
}
HashMap<String,Integer> hmaps = new HashMap<String,Integer>(); for (int i = 0; i < strs.length; i++) {
String curSort = sortString(strs[i]);
if (hmaps.containsKey(curSort)) {
hmaps.put(curSort, hmaps.get(curSort) + 1);
} else {
hmaps.put(curSort, 1);
}
}
for(int i = 0; i < strs.length; i++) {
if (hmaps.containsKey(sortString(strs[i])) && hmaps.get(sortString(strs[i])) > 1) {
result.add(strs[i]);
}
}
return result;
}
String sortString(String str) {
char [] charArr = str.toCharArray();
Arrays.sort(charArr);
return Arrays.toString(charArr);
}
}

LeetCode Anagrams My solution的更多相关文章

  1. LeetCode ---Anagrams() 详解

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

  2. [LeetCode] Anagrams 错位词

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

  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

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

  7. LeetCode: Anagrams 解题报告

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

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

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

  9. LeetCode——Anagrams

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

随机推荐

  1. VMware Workstation Pro 12 桥接联网(物理主机:Windows 7,虚拟机:CentOS 6.8)

    物理主机:Windows 7,虚拟机:CentOS 6.8 1.设置虚拟机的 默认路径:编辑 -> 首选项 -> 设置“虚拟机的默认位置” 2.设置 虚拟网络:编辑 -> 虚拟网络编 ...

  2. x86 TargetPlatform with XBAPs

    I've got a XAML Browser Hosted Application (XBAP) project that has a dependency on another project t ...

  3. pthread_join与pthread_detach细节问题

    http://www.360doc.com/content/13/0106/09/9171956_258497083.shtml pthread_t    pthr; pthread_create(& ...

  4. dotnetty 心跳

    IdleStateHandler 需要提供三个参数: readerIdleTimeSeconds, 读超时. 即当在指定的事件间隔内没有从 Channel 读取到数据时, 会触发一个 READER_I ...

  5. java对象的六大原则

    对象的六大原则: 1.单一职责原则(Single Responsibility Principle  SRP) 2.开闭原则(Open Close Principle OCP) 3.里氏替换原则(Li ...

  6. SVN更新的问题

    更新的时候一直遇到"Base checksum mismatch on"或者"Checksum mismatch while updating",其它文件可以提 ...

  7. C#零基础入门04:打老鼠初级之枚举、重构、事件处理器

    一:为界面加入"开始"."暂停"."停止" 经过上节课程我们的交互的过程,我们的程序增加了用户友好度,同时也可以记录更为详尽的成绩了.但是我 ...

  8. GDSL 1.7 发布,C语言通用数据结构库

    GDSL 1.7 修复了 interval-heap 模块的一个小 bug. GDSL (通用数据结构库) 包含一组程序用于操作各种数据结构.这是一个可移植的库,完全由 ANSI C 编写.为 C 开 ...

  9. ProDinner

    ylbtech-dbs:ProDinner A, 数据库关系图 返回顶部 4, 点餐关系图 3, 留言图 1, 用户角色关系图 0, B,SQL脚本返回顶部 2, use master go --ki ...

  10. dcm4chee 修改默认(0002,0013) ImplementationVersionName

    dcm4chee-2.17.3-psql\server\default\lib\dcm4che.jar ----org\dcm4che\Implementation.properties dcm4ch ...