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. SQL 怎样 远程备份数据库到本地

    SQL 怎样 远程备份数据库到本地 --1.启用xp_cmdshell USE master EXEC sp_configure 'show advanced options', 1 RECONFIG ...

  2. block知识点

    1.block引用局部变量的时候,该变量会作为常量编码到block中,在block中不能被修改. 2.使用 __block修饰的局部变量,不会作为常量被编码到block中,故而在block中可以被修改 ...

  3. xgboost 特征选择,筛选特征的正要性

    import pandas as pd import xgboost as xgb import operator from matplotlib import pylab as plt def ce ...

  4. 通过小书匠编辑器让印象笔记和evernote支持markdown编辑

    a:focus { outline: thin dotted #333; outline: 5px auto -webkit-focus-ring-color; outline-offset: -2p ...

  5. JS 手势长按代码

    同时支持长按和点击事件,无依赖版 <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  6. IPython introduction

    转载:http://blog.csdn.net/gavin_john/article/details/53086766 1. IPython介绍 ipython是一个python的交互式shell,比 ...

  7. kafka 小案例【一】---设置但个消息集群

    启动kafka服务 [ bin/kafka-server-start.sh config/server.properties ] [root@zhangxs kafka_2.]# bin/kafka- ...

  8. iOS 解决TableView reloadData时cell中图片会闪的问题

    tableView调用reloaddata的时候发现有个小问题,每次刷新图片都会抖动闪烁一下,看着很难受,也影响体验.造成这个问题的主要原因是因为刷新时候切换图片导致.要解决这个问题也很好解决,使用S ...

  9. 2018,从AI看安卓生态的变革

    AI的发展与影响 与传统技术不同的是,AI技术算法清晰,优化目标明确,基础技术成熟,使得一众中小创企也看到了市场的机会.2017年中国企业动作频频,在自动驾驶,智能安防,智慧城市等领域都取得了不俗的成 ...

  10. 【SoapUI、Postman、WebServiceStudio、Jmeter】接口测试工具结合测试webservice接口(发送XML格式参数)

    目录: SoapUI测试webservice接口,发送XML格式参数 Postman测试webservice接口,发送XML格式参数 WebServiceStudio.exe测试webservice接 ...