LeetCode:用HashMap解决问题

Find Anagram Mappings

 class Solution {
public int[] anagramMappings(int[] A, int[] B) {
Map<Integer, Integer> D = new HashMap();
for (int i = 0; i < B.length; ++i)
D.put(B[i], i); int[] ans = new int[A.length];
int t = 0;
for (int x: A)
ans[t++] = D.get(x);
return ans;
}
}

思考:用字典来解决问题,巧妙将数组编号转换为页码,然后快速查表

771.Jewels and Stones

class Solution {
public int numJewelsInStones(String J, String S) {
int result = 0;
HashMap<Character,Integer> jMap = new HashMap<>();
for(int i=0;i<J.length();i++)
jMap.put(J.charAt(i),i);
for(int i=0;i<S.length();i++)
{
if(jMap.containsKey(S.charAt(i)))
result++;
}
return result;
}
}

  

LeetCode:用HashMap解决问题的更多相关文章

  1. [LeetCode] Design HashMap 设计HashMap

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

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

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

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

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

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

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

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

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

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

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

  7. LeetCode 706 Design HashMap 解题报告

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

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

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

  9. leetcode hashmap

    187. Repeated DNA Sequences 求重复的DNA序列 public List<String> findRepeatedDnaSequences(String s) { ...

随机推荐

  1. Archlinux风扇设置

    在笔记本(ThinkPad T440)连续两天因过热而死机后, 对内核的风扇控制算法果断失去信心. 风扇的用户层控制接口是 /proc/acpi/ibm/fan, 但为防止用户控制不当烧坏机器, 默认 ...

  2. 自定义ListView和GridView

    1 http://blog.chengyunfeng.com/?p=465 2

  3. 12个高效的VS调试技巧

    介绍 调试是软件开发周期中的一个很重要的部分,有时很有挑战性,有时候则让程序员迷惑,有时候让程序员发疯,但是.可以肯定的是,对于任何不是太那个微不足道的程序来说,调试是不可避免的.近年来,调试工具的发 ...

  4. SQL Server中LIKE和PATINDEX的用法

    在SQL Server中,能使用通配符的只有2个:LIKE.PATINDEX. 不过LIKE支持2种通配符转义,无限制最全面:而PATINDEX只支持最简单的通配符转义([]转义),限制较多. LIK ...

  5. 介绍C#结构体与类区别

    1. 结构体与类定义方式 结构体定义使用struct类定义使用class 结构体: struct testDemo{ int num; void action(){ } } 类: class test ...

  6. PHP array_walk() 函数

    定义和用法 array_walk() 函数对数组中的每个元素应用用户自定义函数.在函数中,数组的键名和键值是参数. <?php function myfunction($value,$key,$ ...

  7. 安装wamp后 异常Exception Exception in module wampmanager.exe at 000F15A0

    系统环境:Windows 2008 R2 64bit 安装环境:wampserver2.4-x64 按照正常windows安装程序,完成WAMP Server程序安装,安装完成启动WAMP Serve ...

  8. Expression Tree上手指南 (一)

    大家可能都知道Expression Tree是.NET 3.5引入的新增功能.不少朋友们已经听说过这一特性,但还没来得及了解.看看博客园里的老赵等诸多牛人,将Expression Tree玩得眼花缭乱 ...

  9. IP地址、子网掩码、网关的关系

    网络管理中的IP地址.子网掩码和网关是每个网管必须要掌握的基础知识,只有掌握它,才能够真正理解TCP/IP协议的设置.以下我们就来深入浅出地讲解什么是子网掩码. IP地址的结构 要想理解什么是子网掩码 ...

  10. 7月份计划-----dream

    梦想还是要有的,万一实现了呢? 数学 150[total] 专业课 150[total] 英语 100[total] 政治 100[total] 第一轮复习计划开始执行 1.专业课: 通过课件把所有的 ...