LeetCode Anagrams My solution
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的更多相关文章
- LeetCode ---Anagrams() 详解
Notice: Given 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 ...
- [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
import java.util.*; /** * * Source : https://oj.leetcode.com/problems/anagrams/ * * Created by lverp ...
- 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 ...
- LeetCode——Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
随机推荐
- [廖雪峰] Git 分支管理(3):分支管理策略
通常,合并分支时,如果可能,Git 会用 Fast forward 模式,但这种模式下,删除分支后,会丢掉分支信息. 如果要强制 禁用 Fast forward 模式,Git 就会在 merge 时生 ...
- SAP BAPI一览 史上最全
全BADI一览 List of BAPI's BAPI WG Component Function module name Description Description Obj. Ty ...
- MongoDB C# 驱动的各种版本下载地址
https://github.com/mongodb/mongo-csharp-driver/releases
- 在Brackets中使用Emmet
当在Brackets中安装上Emmet插件后,就可以使用Emmet的语法来加速前端编写. 有关html ● 子关系> div>ul>li ● 相邻+ div+p+bq ● 上一级^ ...
- indy10的idhttpServer应答字符串
indy10的idhttpServer应答字符串 先看应答字符串的代码: procedure TIdIOHandler.Write(const AOut: string; AByteEncoding: ...
- Linux 用户和用户操作
1,创建组 groupadd test 增加一个test组 2,修改组 groupmod -n test2 test 将test组的名子改成test2 3,删除组 groupdel test2 删除 ...
- [开源]Google code Android开源项目(一)
[Android分享] [开源]Google code Android开源项目(一) [复制链接] 449122717 2 主题 2 好友 816 积分 No.4 中级开发者 升级 19.3 ...
- 神奇女侠Wonder Woman迅雷下载
亚马逊公主戴安娜·普林斯(盖尔·加朵 Gal Gadot 饰),经过在家乡天堂岛的训练,取得上帝赐予的武器 与装备,化身神奇女侠,与空军上尉史蒂夫·特雷弗(克里斯·派恩 Chris Pine 饰)一同 ...
- ios之申请后台延时执行和做一个假后台的方法
转自:http://sis hu ok.com/forum/blogCategory/showByCategory.html?categories_id=138&user_id=10385 ...
- Pandas 快速入门(二)
本文的例子需要一些特殊设置,具体可以参考 Pandas快速入门(一) 数据清理和转换 我们在进行数据处理时,拿到的数据可能不符合我们的要求.有很多种情况,包括部分数据缺失,一些数据的格式不正确,一些数 ...