【LeetCode】760. Find Anagram Mappings 解题报告
【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中的位置即可,如果出现了重复的元素,可以返回任意一种次序即可。
方法一:
class Solution:
def anagramMappings(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: List[int]
"""
answer = []
for a in A:
for i,b in enumerate(B):
if a == b:
answer.append(i)
break
return answer
方法二:
class Solution:
def anagramMappings(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: List[int]
"""
return [B.index(a) for a in A]
方法三:
class Solution:
def anagramMappings(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: List[int]
"""
d ={}
for i,b in enumerate(B):
d[b] = i
return [d[a] for a in A]
Date
2018 年 1 月 13 日
【LeetCode】760. Find Anagram Mappings 解题报告的更多相关文章
- 【LeetCode】760. Find Anagram Mappings 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- LeetCode 760. Find Anagram Mappings
原题链接在这里:https://leetcode.com/problems/find-anagram-mappings/description/ 题目: Given two lists Aand B, ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
随机推荐
- 数据分析体系 — 用户粘性的两个计算指标(DAU/MAU和月人均活跃天数)
很多运营都了解DAU(日活跃用户数)和MAU(月活跃用户数)的重要性,但在某些情况下这两个数值本身并不能反映出太多问题,这个时候就要引用到[DAU/MAU]的概念,即[日活/月活] 用户粘性的两个计算 ...
- SQL-用到的数据库语句总结
0.SELECT * FROM CHARACTER_SETS LIMIT 0,10 #从CHARACTER_SETS表中,从第1行开始,提取10行[包含第1行] 1.SELECT * FROM ...
- 使用GitHub Action进行打包并自动推送至OSS
GitHub Action 是 GitHub 于 2018 年 10 月推出的一个 CI\CD 服务. 官方文档:https://docs.github.com/cn/actions CI\CD 持续 ...
- C#表格GridView显示2位百分比
<asp:BoundField HeaderText="占比" DataField="number" DataFormatString="{0: ...
- Dos窗口下中文乱码问题
最近用Datax工具进行数据同步时,在DOS窗口下出现了中文乱码问题,导致一些错误只能到Log中查看,在网上找了一些方法,记录使用成功的方法. Dos命令:chcp 通过cmd进入Dos命令窗口,执行 ...
- mysql数据库备份脚本一例
例子,mysql数据库备份脚本.vim mysql.sh #!/bin/bash DAY=`date +%Y-%m-%d` //日期以年月日显示并赋予DAY变量 SIZE=`du -sh /var/l ...
- maven项目install时忽略执行test
1.在项目所在文件夹根目录使用maven命令打包时: <!-- 不执行单元测试,也不编译测试类 --> mvn install -Dmaven.test.skip=true 或 <! ...
- 【Java基础】transient关键字
1. transient的作用及使用方法 我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的过 ...
- 搭建mybatis开发环境
1.创建工程 <groupId>com.hope</groupId> <artifactId>day01_eesy_01mybatis</artifa ...
- 拉格朗日乘子法(Lagrange Multiplier) 和KKT条件
参考文献:https://www.cnblogs.com/sddai/p/5728195.html 在求解最优化问题中,拉格朗日乘子法(Lagrange Multiplier)和KKT(Karush ...