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 ...
随机推荐
- Mybatis配置返回为修改影响条数
mybatis配置返回为修改影响条数,修改jdbc连接如下即可:添加useAffectedRows=true配置. jdbc:mysql://jdbc.host/{jdbc.db}?useAffect ...
- redis实现异步任务队列
redis实现异步任务队列 先说思路: 将任务对象序列为JSON字符串,然后推入REDIS缓存,这叫入队. 通过独立的工作线程从REDIS拉出一个任务,这叫出队,工作线程将JSON字符串还原为任务对象 ...
- C#编程(四十八)----------列表
C#中的List C#中deList怎么样?List<T>类是ArrayList类的泛型等效类,该类使用大小可按需动态增长的数组实现List<T>泛型接口. 泛型的好处:它为使 ...
- Winfrom固定Label宽度,根据文本动态改变Label的高度 z
Label,要固定住宽度,然后根据文本的长度来动态改变高度,一开始去网上找解决方案,各种根据字体大小啊,字数啊来动态改变,但是效果却不怎么好.最后灵机一动,想起了偶尔用过一次的FlowLayoutPa ...
- WordPress主题开发:style.css主题信息标记
在最简单的情况下,一个WordPress主题由两个文件构成: index.php ------------------主模版 style.css -------------------主样式表 而且s ...
- .NET:强签名程序集的加载问题 之 版本重定向
背景 多数解决方案会包含多个项目,某些支持插件架构的解决方案中,更是包含多个插件项目,这些项目会使用一些第三方NuGet Packages,如果管理不慎,解决方案中会出现多个版本的引用,这在编译期间不 ...
- Android 百度地图开发(三)
实现比例尺功能和替换自带的缩放组件 ScaleView是比例尺控件.ZoomControlView是缩放控件,MainActivity就是我们的主界面了 先看下ZoomControlView类.代码例 ...
- Spring Websocket实现文本、图片、声音、文件下载及推送、接收及显示(集群模式)
相关环境 Nginx,Spring5.x当前(要选择4.0+),tomcat8.x,Quartz 2.x集群(实际运用是Quartz的集群模式和单机模式共存的) 测试面页:http://sms.rey ...
- Redis Nosql数据库
Redis是一个key-value存储系统.和Memcached类似.可是攻克了断电后数据全然丢失的情况.并且她支持很多其它无化的value类型.除了和string外,还支持lis ...
- 用SwipeBackLayout实现滑动关闭当前Activity
说起SwipeBackLayout,我对它还是有一定怨念的.当时就希望能实现关闭当前Activity的效果,但完全搜不当相关的东西,最后好不容易搜到了这个SwipeBackLayout,觉得可以实现滑 ...