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 ...
随机推荐
- spring mvc:练习 @RequestParam(参数绑定到控制器)和@PathVariable(参数绑定到url模板变量)
spring mvc:练习 @RequestParam和@PathVariable @RequestParam: 注解将请求参数绑定到你的控制器方法参数 @PathVariable: 注释将一个方法参 ...
- 在.net中运用HTMLParser解析网页的原理和方法
本文介绍了.net 版的一个HTMLParser网页解析开源类库(Winista.HTMLParser)的功能特性.工作原理和使用方法.对于使用.net进行Web信息提取的开发人员进行了一次HTMLP ...
- centos 7网速监控脚本
#!/bin/bashif [ $# -ne 1 ];thendev="eth0"elsedev=$1fi while :doRX1=`/sbin/ifconfig $dev |a ...
- layui弹窗宽度固定高度自适应界面
//默认上下15px的边距高度,可根据需要修改function layuiDialogFitScreen(){ var layui_title_height=$(window.parent.docum ...
- Attribute 'items' must be an array, a Collection or a Map错误解决!
唉!真的要说一句话叫做论一串代码的重要性!就是如此的气人!气的牙根痒痒! 前几天刚刚写过SpringMVC之ModelAndView的 jsp值在浏览页面不显示的问题!也是因为这一串代码,但是这一次一 ...
- 转:在Eclipse的Debug页签中设置虚拟机参数
http://blog.csdn.net/decorator2015/article/details/50914479 在Eclipse的Debug页签中设置虚拟机参数 步骤 1,Run->De ...
- int argc,char *argv[]
main()函数是操作系统调用的,argc表示传入参数的个数,argv[]表示传入的参数. argv[0]表示exe文件所在的路径,所以argc默认为1. main函数的参数是可以通过dos窗口传入, ...
- Tencent tinker 出现pre-verified crash
异常类型:app运行时异常 手机型号:sumsung N9008 手机系统版本:android4.4.2 tinker版本: 1.8.1 gradle版本::2.3.3 是否使用热更新SDK: Tin ...
- 《Effective C++》第4章 设计与声明(2)-读书笔记
章节回顾: <Effective C++>第1章 让自己习惯C++-读书笔记 <Effective C++>第2章 构造/析构/赋值运算(1)-读书笔记 <Effecti ...
- 进程与网络监控和ssh简单使用
进程的概念和管理 进程: 正在执行的程序 线程: 轻量级的进程 进程有独立的地址空间,线程没有 线程不能独立存在,它是由进程创建.Thread1.在linux中,每个执行的程序都称为一个进程.每一个进 ...