LeetCode:用HashMap解决问题
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解决问题的更多相关文章
- [LeetCode] Design HashMap 设计HashMap
Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...
- leetcode@ [336] Palindrome Pairs (HashMap)
https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of dist ...
- LeetCode算法题-Design HashMap(Java实现)
这是悦乐书的第299次更新,第318篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第167题(顺位题号是706).在不使用任何内置哈希表库的情况下设计HashMap.具体 ...
- LeetCode 706. Design HashMap (设计哈希映射)
题目标签:HashMap 题目让我们设计一个 hashmap, 有put, get, remove 功能. 建立一个 int array, index 是key, 值是 value,具体看code. ...
- 【LeetCode】缺失的第一个正数【原地HashMap】
给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8,9,11 ...
- 【LeetCode】706. Design HashMap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 706 Design HashMap 解题报告
题目要求 Design a HashMap without using any built-in hash table libraries. To be specific, your design s ...
- [LeetCode&Python] Problem 706. Design HashMap
Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...
- leetcode hashmap
187. Repeated DNA Sequences 求重复的DNA序列 public List<String> findRepeatedDnaSequences(String s) { ...
随机推荐
- OpenStack 网络总结之:openstack中网络的基本概念
原文:openstack-install-guide-yum-icehouse.pdf/7. Add a networking service/Networking concepts OpenStac ...
- rabbitmq 用户和授权
官方文档 https://my.oschina.net/hncscwc/blog/262246?p=
- Eclipse 经常使用快捷键
一.File 二.Edit Ctrl + 1 有益写错,让编辑器提醒改动 三.Refactor 抽取为全局变量 Refactor - Convert Local Variable to Field ...
- Atitit.软件硕士 博士课程 一览表 attilax 总结
Atitit.软件硕士 博士课程 一览表 attilax 总结 1. Attilax聚焦的领域1 2. 研究生硕士博士课程汇总表1 3. 博士课程3 4. Attilax额外的4 5. 参考4 1. ...
- 【转】Lua 操作系统库
转老帖子备份 转自:http://www.cnblogs.com/whiteyun/archive/2009/08/10/1542913.html os.clock () 功能:返回一个程序使用C ...
- JSON解析工具-org.json使用教程
转自:http://www.open-open.com/lib/view/open1381566882614.html 一.简介 org.json是Java常用的Json解析工具,主要提供JSONO ...
- MBA人物俞洪敏:亿万富翁的生活表
我的智商非常一般,就是比别人勤奋.我的脑袋不属于特别笨的那种,但肯定也不是顶尖聪明的类型.在北大的50个同学当中,我的智商应该属于中下水平,这说明我不是顶尖高智商. 我的勤奋一般人跟不上.我平均每天工 ...
- java中双向链表的增、删、查操作
import java.util.NoSuchElementException; public class DoublyLinkedListImpl<E> { private Node h ...
- Pentaho 免费版本下载地址列表
Pentaho CE(Community Edition) 免费版本下载地址列表 http://sourceforge.net/projects/pentaho/files/Pentaho 插件名称 ...
- Ocelot --API网关简单使用
最近几个月一直在忙一个项目(感觉像是与世隔绝了),本来想好的是要写一些asp.net core 的一些简单使用上东西,也就放下了. 好在忙完了,也就重新开始写吧.写点什么呢?网上也有不少dotnet ...