LeetCode 760. Find Anagram Mappings
原题链接在这里:https://leetcode.com/problems/find-anagram-mappings/description/
题目:
Given two lists A
and 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 i
th 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 0
th element of A
appears at B[1]
, and P[1] = 4
because the 1
st element of A
appears at B[4]
, and so on.
Note:
A, B
have 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 ...
随机推荐
- PHP libevent函数基本介绍
3.2 主要函数介绍 按照使用libevnet库顺序,看一下相关函数做什么操作. 3.2.1 event_init 调用event_base_new,初始化struct event_base对象 ...
- 五、Google Code Prettify:实现代码高亮的JS库
介绍 code prettify 解释为 “代码修饰”. 他由JS代码和CSS代码构成,用来高亮显示HTML页面中的代码. 支持:C, Java, Python, Bash, HTML, XML, J ...
- CAJ2PDF
该项目不成熟,很容易遇到转换失败的例子. https://github.com/JeziL/caj2pdf https://github.com/JeziL/caj2pdf/wiki caj2pdf ...
- hdu4305生成树计数
先预处理出距离,然后判断是否可行,要注意判断是否在一条直线上时判断是在两侧还是一边(wa了四次) double型数据 #include<map> #include<set> # ...
- UVA-11865 Stream My Contest (朱-刘 算法+二分)
题目大意:有一张n个顶点,m条边的有向图,根节点为0.每条边有两个权值,一个是费用c,一个是长度b.问在总费用不超过cost的情况下选出若干条边,使得n个点连通时的边的最短长度的最大值是多少. 题目分 ...
- 最近玩了一下qt5.2.1,顺着写点东西,关于这个版本设置程序主窗口居中
#include <QtGui/QGuiApplication> #include <QDebug> #include <QScreen> #include &qu ...
- Dubbo + Zookeeper 简单配置
Dubbo + Zookeeper Zookeeper 下载及配置 下载到本机/usr/local目录 wget https://mirrors.tuna.tsinghua.edu.cn/apache ...
- day15 web框架和Django基础
参考博客: http://www.cnblogs.com/yuanchenqi/articles/6788872.html http://www.cnblogs.com/yuanchenqi/arti ...
- angularJS---初识指令
angularJS 什么是angularJS AngularJS 诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款 ...
- 每周荐书:机器学习、Java虚拟机、微信开发(评论送书)
每周荐书:机器学习.Java虚拟机.微信开发(评论送书) 感谢大家对每周荐书栏目的支持,先公布下上周中奖名单 年精心雕琢,难得的"理论 + 实战案例 + 趟坑经验"总结 从需求分析 ...