LeetCode 760. Find Anagram Mappings
原题链接在这里:https://leetcode.com/problems/find-anagram-mappings/description/
题目:
Given two lists Aand B, and B is an anagram of A. B is an anagram of A means B is made by randomizing the order of the elements in A.
We want to find an index mapping P, from A to B. A mapping P[i] = j means the ith element in A appears in B at index j.
These lists A and B may contain duplicates. If there are multiple answers, output any of them.
For example, given
A = [12, 28, 46, 32, 50]
B = [50, 12, 32, 46, 28]
We should return
[1, 4, 3, 2, 0]
as P[0] = 1 because the 0th element of A appears at B[1], and P[1] = 4 because the 1st element of A appears at B[4], and so on.
Note:
A, Bhave equal lengths in range[1, 100].A[i], B[i]are integers in range[0, 10^5].
题解:
用map保存B的元素与对应位置. 再用A的元素在map中找对应位置.
Time Complexity: O(A.length).
Space: O(1), regardless res.
AC Java:
class Solution {
public int[] anagramMappings(int[] A, int[] B) {
if(A == null || B == null || A.length != B.length){
throw new IllegalArgumentException("Invalid input.");
}
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int i = 0; i<B.length; i++){
hm.put(B[i], i);
}
int [] res = new int[A.length];
for(int i = 0; i<A.length; i++){
res[i] = hm.get(A[i]);
}
return res;
}
}
LeetCode 760. Find Anagram Mappings的更多相关文章
- 【LeetCode】760. Find Anagram Mappings 解题报告
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...
- 【LeetCode】760. Find Anagram Mappings 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 760. Find Anagram Mappings
Given two lists Aand B, and B is an anagram of A. B is an anagram of A means B is made by randomizin ...
- 760. Find Anagram Mappings乱序字符串的坐标位置
[抄题]: Given two lists Aand B, and B is an anagram of A. B is an anagram of A means B is made by rand ...
- [LeetCode] Find Anagram Mappings 寻找异构映射
Given two lists A and B, and B is an anagram of A. B is an anagram of A means B is made by randomizi ...
- LeetCode 242. Valid Anagram (验证变位词)
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- [LeetCode] 242. Valid Anagram 验证变位词
Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...
- LeetCode 242 Valid Anagram
Problem: Given two strings s and t, write a function to determine if t is an anagram of s. For examp ...
- (easy)LeetCode 242.Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
随机推荐
- NPOI2.2.0.0实例详解(八)—设置EXCEL单元格【数字格式】
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 二十三 Python分布式爬虫打造搜索引擎Scrapy精讲—craw母版l创建自动爬虫文件—以及 scrapy item loader机制
用命令创建自动爬虫文件 创建爬虫文件是根据scrapy的母版来创建爬虫文件的 scrapy genspider -l 查看scrapy创建爬虫文件可用的母版 Available templates: ...
- Linux 忘记密码解决方法,Linux 远程登录
一.Linux 忘记密码解决方法 很多朋友经常会忘记Linux系统的root密码,linux系统忘记root密码的情况该怎么办呢?重新安装系统吗?当然不用!进入单用户模式更改一下root密码即可. 步 ...
- ansible 调用playbook api执行(一)
一 调用ansible playbook api执行playbook 1 准备好hosts文件 root@ansible:~/ansible/playbooks# cat hosts [all:var ...
- 使用IScroll5实现滚动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Cscope 安装和使用
CScope 安装和使用 一.安装 1. 软件下载 https://sourceforge.net/projects/cscope/files/ 2. 解压 3. 安装 ./configure --p ...
- 2018.11.13 N4010A 通信设置
设置电脑之IP地址及Subnet mask. IP address: 192.168.1.2 Subnet mask: 255.255.255.0, 其它选项为默认. 然后点击OK ...
- Django 知识点补充
Django 知识点补充 1 Django如何在Model保存前做一定的固定操作,比如写一条日志 (1)利用Django的Model的Signal Dispatcher, 通过django.db.mo ...
- mssqlserver SQL注释快捷键
注释快捷键 选中语句(快捷键:光标定位到需要注释块的最顶行,按住shift+home选中行,放开再按下shift+向下键,选中块) 按住Ctrl然后依次按K,C取消注释快捷键 选中语句 按住Ctrl然 ...
- JFinal源码详解
JFinal的框架我24号的一篇博文写到过,它优秀的地方在精简代码上,那么有两处源码是我觉得是值得我们要好好解析一下,一处是初始化加载—servlet跳转,另一处是DB+ActiveRecord的映射 ...