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 ...
随机推荐
- [Go] 如何正确地 抛出 错误 和 异常(error/panic/recover)?
序言 错误 和 异常 是两个不同的概念,非常容易混淆.很多程序员习惯将一切非正常情况都看做错误,而不区分错误和异常,即使程序中可能有异常抛出,也将异常及时捕获并转换成错误.从表面上看,一切皆错误的思路 ...
- TVB三个台
翡翠台http://token.tvb.com/stream/live/hls/mobilehd_jade.smil 高清翡翠,http://token.tvb.com/stream/live/hls ...
- 高速排序C++实现
//高速排序 #include<iostream> #include<functional> #include<Windows.h> using namespace ...
- 线程中sleep方法和wait方法有什么区别?(转)
本文转自https://www.cnblogs.com/linkstar/p/6043846.html 线程中sleep方法和wait方法有什么区别? 如果你没有接触过java的多线程,那么多对于 ...
- c++ 实现atoi()函数
1. 问题描写叙述 实现c++函数库中atoi()函数,要考虑到各种特殊情况: 空字符串. +和-号. 字符串前中后n个空格. 溢出. 非数字字符. 2. 解决方式 转换过程并不复杂.复杂的是要考虑到 ...
- IP视频通信中的"丢包恢复技术”(LPR)
转自:http://blog.csdn.net/blade2001/article/details/9094709 在IP视频通话中,即使是在丢包率很小的情况下也会对使用效果造成较为明显的影响.正是由 ...
- ArrayList 排序方法的性能对比
20000=>ZXP 二分法 getSeriesMinSort2(list) Time is 67000 20000=>循环 getSeriesMinSortFor(list) Time ...
- HikariCP 脑火Failed to obtain JDBC Connection: You need to run the CLI build and you need target/classes in your classpath to run.
测试了一下 HikariCP 连接池报错,无解 十一月 16, 2017 5:31:59 下午 org.apache.catalina.core.StandardContext loadOnStart ...
- putty adb
putty.exe -adb -P 5037 transport-usb 网络调试也是可以的 先connect 再执行上面的命令 http://files.cnblogs.com/files/ahuo ...
- JDK(Java SE Development Kit)的安装与环境变量的配置
本文参考于:http://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html 感谢作者的贡献~ 首先,进入网址下载JDK:http://ww ...