原题链接在这里: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. python 中str format 格式化数字补0方法

      >>> "{0:03d}".format(1)'001'>>> "{0:03d}".format(10)'010'> ...

  2. Linux命令详解-mv

    mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] 源文件或目 ...

  3. 【Demo】jQuery 轮播图简单动画效果

    功能实现: (1)设定图片称号的鼠标悬停事件: (2)在事件中利用自定义动画函数调整显示图片,并修改对应标号样式: (3)为图片显示区域设定鼠标悬停事件: (4)当鼠标停在该区域时,清除图片切换动画定 ...

  4. JAVA经典总结

    Java经典实例(第二版) 1. 获取环境变量 Java代码 1. System.getenv("PATH"); 2. System.getenv("JAVA_HOME& ...

  5. sqlserver数据库标注为可疑的解决办法

    前几天客户那边的服务器死机了,然后客户强制关机,重新启动服务器后,系统就没法正常使用,连接不上服务器,我远程操作后,看到数据库标注为可疑,由于客户之前没备份数据库,看来只能是修复了: 1:停止数据库服 ...

  6. H3C Huawei 交换机 IPv6环境配置

    # 使能IPv6报文转发功能. <Sysname> system-view [Sysname] ipv6 # 使能DHCPv6服务器功能. <Sysname> system-v ...

  7. javascript打开新窗体

    open -- 打开(弹出)一个新的窗体 open,中文"打开"的意思 引用网址:http://www.dreamdu.com/javascript/window.open Jav ...

  8. qml 带时钟效果

    qt 自带源码样例:在样例中输入clock,找到名叫Qt Quick Demo Clocks. 这个样例写的不错,还是带时差的.不过我们其实不需要这么复杂的功能. timer启动这个就不用说了: Ti ...

  9. 在Linux下设置定时任务(每分钟执行一次特定的shell脚本)

    在当前用户下,开始编辑定时任务 crontab -e 按键 i 进入编辑模式 输入下述命令 */ * * * * sh /***/*****.sh 然后按键 Esc 退出编辑模式,再输入 wq 保存退 ...

  10. vue.js 源代码学习笔记 ----- instance state

    /* @flow */ import Dep from '../observer/dep' import Watcher from '../observer/watcher' import { set ...