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

标签(空格分隔): LeetCode


题目地址: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 B1, and P1 = 4 because the 1st 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].

Ways

就是找到A中每个元素在B中的位置即可,如果出现了重复的元素,可以返回任意一种次序即可。

方法一:

  1. class Solution:
  2. def anagramMappings(self, A, B):
  3. """
  4. :type A: List[int]
  5. :type B: List[int]
  6. :rtype: List[int]
  7. """
  8. answer = []
  9. for a in A:
  10. for i,b in enumerate(B):
  11. if a == b:
  12. answer.append(i)
  13. break
  14. return answer

方法二:

  1. class Solution:
  2. def anagramMappings(self, A, B):
  3. """
  4. :type A: List[int]
  5. :type B: List[int]
  6. :rtype: List[int]
  7. """
  8. return [B.index(a) for a in A]

方法三:

  1. class Solution:
  2. def anagramMappings(self, A, B):
  3. """
  4. :type A: List[int]
  5. :type B: List[int]
  6. :rtype: List[int]
  7. """
  8. d ={}
  9. for i,b in enumerate(B):
  10. d[b] = i
  11. return [d[a] for a in A]

Date

2018 年 1 月 13 日

【LeetCode】760. Find Anagram Mappings 解题报告的更多相关文章

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

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

  2. LeetCode 760. Find Anagram Mappings

    原题链接在这里:https://leetcode.com/problems/find-anagram-mappings/description/ 题目: Given two lists Aand B, ...

  3. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  4. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  5. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  6. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  7. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  8. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  9. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

随机推荐

  1. 数据分析体系 — 用户粘性的两个计算指标(DAU/MAU和月人均活跃天数)

    很多运营都了解DAU(日活跃用户数)和MAU(月活跃用户数)的重要性,但在某些情况下这两个数值本身并不能反映出太多问题,这个时候就要引用到[DAU/MAU]的概念,即[日活/月活] 用户粘性的两个计算 ...

  2. SQL-用到的数据库语句总结

    0.SELECT * FROM  CHARACTER_SETS LIMIT 0,10   #从CHARACTER_SETS表中,从第1行开始,提取10行[包含第1行] 1.SELECT * FROM  ...

  3. 使用GitHub Action进行打包并自动推送至OSS

    GitHub Action 是 GitHub 于 2018 年 10 月推出的一个 CI\CD 服务. 官方文档:https://docs.github.com/cn/actions CI\CD 持续 ...

  4. C#表格GridView显示2位百分比

    <asp:BoundField HeaderText="占比" DataField="number" DataFormatString="{0: ...

  5. Dos窗口下中文乱码问题

    最近用Datax工具进行数据同步时,在DOS窗口下出现了中文乱码问题,导致一些错误只能到Log中查看,在网上找了一些方法,记录使用成功的方法. Dos命令:chcp 通过cmd进入Dos命令窗口,执行 ...

  6. mysql数据库备份脚本一例

    例子,mysql数据库备份脚本.vim mysql.sh #!/bin/bash DAY=`date +%Y-%m-%d` //日期以年月日显示并赋予DAY变量 SIZE=`du -sh /var/l ...

  7. maven项目install时忽略执行test

    1.在项目所在文件夹根目录使用maven命令打包时: <!-- 不执行单元测试,也不编译测试类 --> mvn install -Dmaven.test.skip=true 或 <! ...

  8. 【Java基础】transient关键字

    1. transient的作用及使用方法 我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的过 ...

  9. 搭建mybatis开发环境

    1.创建工程 <groupId>com.hope</groupId>     <artifactId>day01_eesy_01mybatis</artifa ...

  10. 拉格朗日乘子法(Lagrange Multiplier) 和KKT条件

    参考文献:https://www.cnblogs.com/sddai/p/5728195.html 在求解最优化问题中,拉格朗日乘子法(Lagrange Multiplier)和KKT(Karush ...