187. Repeated DNA Sequences

求重复的DNA序列

  1. public List<String> findRepeatedDnaSequences(String s) {
  2. Set seen = new HashSet(), repeated = new HashSet();
  3. for (int i = 0; i + 9 < s.length(); i++) {
  4. String ten = s.substring(i, i + 10);
  5. if (!seen.add(ten))
  6. repeated.add(ten);
  7. }
  8. return new ArrayList(repeated);
  9. }

hashset   !seen.add(ten) 添加失败返回false

299 Bulls and Cows

有一个四位数字,你猜一个结果,然后根据你猜的结果和真实结果做对比,提示有多少个数字和位置都正确的叫做bulls,还提示有多少数字正确但位置不对的叫做cows,根据这些信息来引导我们继续猜测正确的数字。这道题并没有让我们实现整个游戏,而只用实现一次比较即可。给出两个字符串,让我们找出分别几个bulls和cows。这题需要用哈希表,来建立数字和其出现次数的映射。我最开始想的方法是用两次遍历,第一次遍历找出所有位置相同且值相同的数字,即bulls,并且记录secret中不是bulls的数字出现的次数。然后第二次遍历我们针对guess中不是bulls的位置,如果在哈希表中存在,cows自增1,然后映射值减1,参见如下代码:

  1. class Solution {
  2. public:
  3. string getHint(string secret, string guess) {
  4. int m[256] = {0}, bulls = 0, cows = 0;
  5. for (int i = 0; i < secret.size(); ++i) {
  6. if (secret[i] == guess[i]) ++bulls;
  7. else ++m[secret[i]];
  8. }
  9. for (int i = 0; i < secret.size(); ++i) {
  10. if (secret[i] != guess[i] && m[guess[i]]) {
  11. ++cows;
  12. --m[guess[i]];
  13. }
  14. }
  15. return to_string(bulls) + "A" + to_string(cows) + "B";
  16. }
  17. };

我们其实可以用一次循环就搞定的,在处理不是bulls的位置时,我们看如果secret当前位置数字的映射值小于0,则表示其在guess中出现过,cows自增1,然后映射值加1,如果guess当前位置的数字的映射值大于0,则表示其在secret中出现过,cows自增1,然后映射值减1,参见代码如下:

  1. public String getHint(String secret, String guess) {
  2. int bulls = 0;
  3. int cows = 0;
  4. int[] numbers = new int[10];
  5. for (int i = 0; i<secret.length(); i++) {
  6. int s = Character.getNumericValue(secret.charAt(i));
  7. int g = Character.getNumericValue(guess.charAt(i));
  8. if (s == g) bulls++;
  9. else {
  10. if (numbers[s] < 0) cows++;
  11. if (numbers[g] > 0) cows++;
  12. numbers[s] ++;
  13. numbers[g] --;
  14. }
  15. }
  16. return bulls + "A" + cows + "B";
  17. }

49. Group Anagrams

hashmap操作

  1. public class Solution {
  2. public List<List<String>> groupAnagrams(String[] strs) {
  3. if (strs == null || strs.length == 0) return new ArrayList<List<String>>();
  4. Map<String, List<String>> map = new HashMap<String, List<String>>();
  5. for (String s : strs) {
  6. char[] ca = s.toCharArray();
  7. Arrays.sort(ca);
  8. String keyStr = String.valueOf(ca);
  9. if (!map.containsKey(keyStr)) map.put(keyStr, new ArrayList<String>());
  10. map.get(keyStr).add(s);
  11. }
  12. return new ArrayList<List<String>>(map.values());
  13. }
  14. }

743. Network Delay Time

  1. Use Map<Integer, Map<Integer, Integer>> to store the source node, target node and the distance between them.
  2. Offer the node K to a PriorityQueue.
  3. Then keep getting the closest nodes to the current node and calculate the distance from the source (K) to this node (absolute distance). Use a Map to store the shortest absolute distance of each node.
  4. Return the node with the largest absolute distance.
  1. public int networkDelayTime(int[][] times, int N, int K) {
  2. if(times == null || times.length == 0){
  3. return -1;
  4. }
  5. // store the source node as key. The value is another map of the neighbor nodes and distance.
  6. Map<Integer, Map<Integer, Integer>> path = new HashMap<>();
  7. for(int[] time : times){
  8. Map<Integer, Integer> sourceMap = path.get(time[0]);
  9. if(sourceMap == null){
  10. sourceMap = new HashMap<>();
  11. path.put(time[0], sourceMap);
  12. }
  13. Integer dis = sourceMap.get(time[1]);
  14. if(dis == null || dis > time[2]){
  15. sourceMap.put(time[1], time[2]);
  16. }
  17.  
  18. }
  19.  
  20. //Use PriorityQueue to get the node with shortest absolute distance
  21. //and calculate the absolute distance of its neighbor nodes.
  22. Map<Integer, Integer> distanceMap = new HashMap<>();
  23. distanceMap.put(K, 0);
  24. PriorityQueue<int[]> pq = new PriorityQueue<>((i1, i2) -> {return i1[1] - i2[1];});
  25. pq.offer(new int[]{K, 0});
  26. int max = -1;
  27. while(!pq.isEmpty()){
  28. int[] cur = pq.poll();
  29. int node = cur[0];
  30. int distance = cur[1];
  31.  
  32. // Ignore processed nodes
  33. if(distanceMap.containsKey(node) && distanceMap.get(node) < distance){
  34. continue;
  35. }
  36.  
  37. Map<Integer, Integer> sourceMap = path.get(node);
  38. if(sourceMap == null){
  39. continue;
  40. }
  41. for(Map.Entry<Integer, Integer> entry : sourceMap.entrySet()){
  42. int absoluteDistence = distance + entry.getValue();
  43. int targetNode = entry.getKey();
  44. if(distanceMap.containsKey(targetNode) && distanceMap.get(targetNode) <= absoluteDistence){
  45. continue;
  46. }
  47. distanceMap.put(targetNode, absoluteDistence);
  48. pq.offer(new int[]{targetNode, absoluteDistence});
  49. }
  50. }
  51. // get the largest absolute distance.
  52. for(int val : distanceMap.values()){
  53. if(val > max){
  54. max = val;
  55. }
  56. }
  57. return distanceMap.size() == N ? max : -1;
  58. }

leetcode hashmap的更多相关文章

  1. leetcode@ [336] Palindrome Pairs (HashMap)

    https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of dist ...

  2. LeetCode算法题-Design HashMap(Java实现)

    这是悦乐书的第299次更新,第318篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第167题(顺位题号是706).在不使用任何内置哈希表库的情况下设计HashMap.具体 ...

  3. [LeetCode] Design HashMap 设计HashMap

    Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...

  4. LeetCode:用HashMap解决问题

    LeetCode:用HashMap解决问题 Find Anagram Mappings class Solution { public int[] anagramMappings(int[] A, i ...

  5. LeetCode 706. Design HashMap (设计哈希映射)

    题目标签:HashMap 题目让我们设计一个 hashmap, 有put, get, remove 功能. 建立一个 int array, index 是key, 值是 value,具体看code. ...

  6. 【LeetCode】缺失的第一个正数【原地HashMap】

    给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8,9,11 ...

  7. 【LeetCode】706. Design HashMap 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. LeetCode 706 Design HashMap 解题报告

    题目要求 Design a HashMap without using any built-in hash table libraries. To be specific, your design s ...

  9. [LeetCode&Python] Problem 706. Design HashMap

    Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...

随机推荐

  1. chrome 55 zepto tap事件出错?

    刚才升级chrome后发现的,在54.0.2840.98上点击没有问题,在新升级的55.0.2883.75 上点击后会报错Cannot read property 'trigger' of undef ...

  2. GitHub https链接中输入账户和密码

    /********************************************************************** * GitHub https链接中输入账户和密码 * 说 ...

  3. 文件的copy

    def mycopy(src_filename, dst_filename): try: fr = open(src_filename, "rb") try: try: fw = ...

  4. debian 8.1 安装idempiere 2.1 X64 笔记

    接上文.当虚拟服务器和虚拟机搭建完成后.登陆debian 8.1 X64. 进入虚拟服务器控制台.打开虚拟机.root登陆.(留好初始状态系统快照.以便系统恢复.) 由于之前debian8.1X64默 ...

  5. 使用Session防止表单重复提交(不考虑多服务器)

    在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 原理:  1, ...

  6. hadoop入门手册2:hadoop【2.7.1】【多节点】集群配置【必知配置知识2】

    问题导读 1.如何实现检测NodeManagers健康?2.配置ssh互信的作用是什么?3.启动.停止hdfs有哪些方式? 上篇: hadoop[2.7.1][多节点]集群配置[必知配置知识1]htt ...

  7. 洛谷P1876开灯

    题目描述 有n盏灯,一开始全是关闭的.来n个人, 第一个人把一的倍数的灯开着的关上,关上的打开. 第二个人把二的倍数的灯开着的关上,关上的打开. 第三个人把三的倍数的灯开着的关上,关上的打开. ... ...

  8. gradle 配置java 项目maven 依赖

     1. 内置的 repositories { mavenCentral() } 2. maven 私服 repositories { maven { url "http://maven.al ...

  9. Linux之 手动释放内存

    我们在进程中要怎样去描述一个文件呢?我们用目录项(dentry)和索引节点(inode).它们的定义如下: 所谓"文件", 就是按一定的形式存储在介质上的信息,所以一个文件其实包含 ...

  10. 让C# Excel导入导出,支持不同版本的Office(转)

    问题:最近在项目中遇到,不同客户机安装不同Office版本,在导出Excel时,发生错误. 找不到Excel Com组件,错误信息如下. 未能加载文件或程序集“Microsoft.Office.Int ...