原题链接在这里:https://leetcode.com/problems/find-anagram-mappings/description/

题目:

Given two lists Aand B, and B is an anagram of AB 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:

  1. A, B have equal lengths in range [1, 100].
  2. 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的更多相关文章

  1. 【LeetCode】760. Find Anagram Mappings 解题报告

    [LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...

  2. 【LeetCode】760. Find Anagram Mappings 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. [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: ...

  8. 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 ...

  9. (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 ...

随机推荐

  1. 使用 Vs 2015 快速上手 Angular2

    Visual Studio 2015 快速上手(使用Angular2)https://angular.cn/guide/visual-studio-2015 使用 Vs 2015 快速上手 Angul ...

  2. SSM 框架搭建 idea环境

    参考: https://www.cnblogs.com/toutou/p/ssm_springmvc.html https://www.cnblogs.com/toutou/p/ssm_springm ...

  3. Python实现H5页面

    from selenium import webdriver mobile_emulation = {'deviceName':'iPhone X'} options = webdriver.Chro ...

  4. Daper返回DataTable

    using (IDbConnection conn = OpenConnection()) { string sql = "SELECT TOP 1 * FROM dbo.Students& ...

  5. SQLServer 2005 和自增长主键identity说再见——NEWSEQUENTIALID()

    ``code 在SQLServer2005环境下,表的主键应该怎样设计. 目前主要用到的主键方案共三种 自动增长主键 手动增长主键 UNIQUEIDENTIFIER主键 1.先说自动增长主键,它的优点 ...

  6. centos网卡配置

    DEVICE=物理设备名 IPADDR=IP地址 NETMASK=掩码值 NETWORK=网络地址 BROADCAST=广播地址 GATEWAY=网关地址 TYPE=Ethernet (网络类型)ON ...

  7. 为什么需要超出48K的音频采样率,以及PCM到DSD的演进

    网上很多观点说,根据采样定理,48K的音频采样率即可无损的表示音频模拟信号(人耳最多可以听到20K的音频),为何还需要96K, 192K等更高的采样率呢?最先我也有这样的疑问,毕竟采样定理是经过数学家 ...

  8. 创建自定义JSR303的验证约束(Creating custom constraints)

    转载:http://clongjava.iteye.com/blog/1317649 由于输入验证在软件开发中是必须的一件事情,特别是与用户交互的软件产品,验证用户的潜在输入错误是必不可少的一件事情, ...

  9. 【第三方类库】underscore.js源码---each forEach 每次迭代跟{}比较的疑惑

    var each = _.each = _.forEach = function(obj, iterator, context) { if (obj == null) return; //首先判断是否 ...

  10. Annotation方式实现AOP

    1.添加其他jar包 2.配置applicationContext.xml文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?xml version=&quo ...