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) { ...
随机推荐
- JavaScript数组归并方法reduce
示例代码: <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF ...
- 通过mysql show processlist 命令检查mysql锁的方法
作者: 字体:[增加 减小] 类型:转载 时间:2010-03-07 show processlist 命令非常实用,有时候mysql经常跑到50%以上或更多,就需要用这个命令看哪个sql语句占用资源 ...
- Docker iptables failed: iptables -t nat -A DOCKER -p tcp
Dokcer网络问题 因为操作或修该过iptables导致docker容器出现如下错误: [root@mysqlserver ~]# docker restart cvnavi-centos-tomc ...
- 工作总结 用, 隔开数据 后台不可以用 List<string> 接收 get请求直接通过浏览器发请求传数组或者list到后台
旁边的 css js 为项目的 加载 css js 地址 只加载引用的样式 js http://localhost:8736/LinInFoKPI/ExcelPrint?line=&start ...
- ssh不检查server变化
嵌入式linux开发时经常需要远程登录到板上,但由于开发过程还经常会重新下载内核和文件系统,导致登录时总提示host变了,blablabla,解决方案是在.ssh/config对应的Host项下面加上 ...
- [译]GLUT教程 - 位图字体
Lighthouse3d.com >> GLUT Tutorial >> Fonts >> Bitmap Fonts 位图字体一般是二维字体.虽然我们会把它放到三维 ...
- Linux安装php-7.0.16,完成php和apache的配置
Linux安装php-7.0.16,完成php和apache的配置 版本:php-7.0.16.tar.gz,libxml2-2.9.2.tar.gz(php需要它的支持,首先安装它) 说明 ...
- RabbitMQ集群安装配置+HAproxy+Keepalived高可用
RabbitMQ集群安装配置+HAproxy+Keepalived高可用 转自:https://www.linuxidc.com/Linux/2016-10/136492.htm rabbitmq 集 ...
- SourceTree超前一个版本,落后N个版本
SourceTree超前一个版本,落后N个版本 在使用SourceTree的时候经常会遇见超前一个版本,落后N个版本的情况,遇见这种情况应该怎么办呢? 首先打开终端,最好是从SourceTree里 ...
- 将navigationbar的translucent属性设为No后,子控制器视图整体下移问题
如果不将navigationbar.translucent = YES 会觉得颜色很浅,因为这是半透明状态 若navigationbar.translucent = NO,颜色问题解决,但是子控制器视 ...