Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode笔记:760. Find Anagram Mappings

LeetCode笔记:760. Find Anagram Mappings

作者头像
Cloudox
发布于 2021-11-23 08:42:03
发布于 2021-11-23 08:42:03
38000
代码可运行
举报
文章被收录于专栏:月亮与二进制月亮与二进制
运行总次数:0
代码可运行

问题(Easy):

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:

  1. A, B have equal lengths in range [1, 100].
  2. A[i], B[i] are integers in range [0, 10^5].

大意:

给出两个列表A和B,B是A的异构体,所谓异构体是指B是由A中元素随机顺序组成。 我们想要找到一个从A到B的序号映射P,映射P[i] = j表示A中的第i个元素在B中是序号j。 这些列表A和B可能包含重复的,如果有多个答案,输出任何一个。 比如,给出 A = [12, 28, 46, 32, 50] B = [50, 12, 32, 46, 28] 我们应该返回 [1, 4, 3, 2, 0] P[0] = 1,因为A中第0个元素在B[1]出现,P[1] = 4,因为A中第1个元素在B[4]出现,等等。 注意:

  1. A、B有着相等的长度,范围在[1,100]。
  2. A[i],B[i]都是范围在[0,10^5]的整数。

思路:

题目说了出现重复的随便取一个位置即可,测试了一下,即使两个元素都取一个位置也行,那就简单了,因为要确定序号,所以顺序也不能变,直接调用find函数,即可找到元素在B中的位置。

代码(C++):

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
public:
    vector<int> anagramMappings(vector<int>& A, vector<int>& B) {
        vector<int> res;
        auto iter = A.begin();
        while (iter != A.end()) {
            auto findB = find(B.begin(), B.end(), *iter);
            res.push_back(findB-B.begin());
            iter++;
        }
        return res;
    }
};

他山之石:

可以用map存储B中每个元素的位置,然后遍历A,利用map找到其在B中的位置即可,速度比上面的方法要快。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
public:
    vector<int> anagramMappings(vector<int>& A, vector<int>& B) {
        unordered_map<int, int> m;    //<values of b, index in b>
        for(int i=0; i<B.size(); i++)
            m[B[i]]=i;    
        
        vector<int> ans(A.size());
        for(int i=0; i<A.size(); i++) {
            auto loc = m.find(A[i]);
            ans[i]=loc->second;
        }
        
        return ans;
    }
};

合集:https://github.com/Cloudox/LeetCode-Record


本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/1/12 下,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
LeetCode 760. 找出变位映射(哈希)
给定两个列表 A and B,并且 B 是 A 的变位(即 B 是由 A 中的元素随机排列后组成的新列表)。
Michael阿明
2020/07/13
4490
Leetcode 242. Valid Anagram
版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/83031717
Tyan
2019/05/25
3680
LWC 66: 760. Find Anagram Mappings
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/78994977
用户1147447
2019/05/25
3070
LeetCode 870. 优势洗牌(贪心 & 二分查找)
给定两个大小相等的数组 A 和 B,A 相对于 B 的优势可以用满足 A[i] > B[i] 的索引 i 的数目来描述。
Michael阿明
2020/07/13
7530
LeetCode 870. 优势洗牌(贪心 & 二分查找)
数字问题-LeetCode 435、436、441、442、443、445、448(数字)
示例 1: 输入: [ [1,2], [2,3], [3,4], [1,3] ] 输出: 1 解释: 移除 [1,3] 后,剩下的区间没有重叠。
算法工程师之路
2020/02/13
5930
LeetCode笔记:136. Single Number
我能想到的最直接的思路就是先排序然后从头到尾比较后找出那个唯一的数字,但是这种思路很简单粗暴,感觉一定不是出题人想要的答案,而且并不太能满足注意事项,但想不出别的我只能先这样实现了,然后我看了看别人的高级做法顿时觉得思路清奇实在是精妙无比。
Cloudox
2021/11/23
2580
算法学习笔记-回溯
一、排列问题 1、leetcode第46题:https://leetcode-cn.com/problems/permutations/ //这就是一个单纯的排列问题,不要求前面的数必须在前面,要求就是每个数只能出现一次:无重复数字 class Solution { private: vector <int> tmp; vector <vector <int>> res; int vis[10] = {0}; public:
买唯送忧
2021/06/15
4930
数字问题-LeetCode 452、453、454、455、456、459(KMP算法)
在二维空间中有许多球形的气球。对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标。由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了。开始坐标总是小于结束坐标。平面内最多存在104个气球。
算法工程师之路
2020/02/13
7890
leetcode-169-Majority Element
题目描述: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. 要完成的函数: int majorityElement(
chenjx85
2018/05/21
5720
算法学习笔记-无题
一、各种算法题 1、leetcode第二题:https://leetcode-cn.com/problems/add-two-numbers/ //你可以新创建一个链表,也可以在原有的链表上操作,这里选新创建的链表 //(1)两数相加,最重要的就是考虑进位,每次赋值,记得加上进位; //(2)最后一个数是否为1,取决于最高位是否有进位; //(3)循环条件,只要有一个链表不为空就可以继续进行 //(4)代码如下: ListNode* addTwoNumbers(ListNode* l1, ListNode*
买唯送忧
2021/05/17
3490
校内选拔赛补题
只能说我的水平还是太低了,这种题都没法场上过掉。或者说我和 CF 终究是八字不合。
Clouder0
2022/10/31
3300
leetcode-438-Find All Anagrams in a String
题目描述: Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter.
chenjx85
2018/05/22
7850
LeetCode 721. 账户合并(并查集)
给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name),其余元素是 emails 表示该帐户的邮箱地址。
Michael阿明
2021/02/19
3290
STL中find的七大姑八大姨
且 find_first_of() 可以在第二个序列中搜索指定范围内可以使第 5 个参数指定的二元谓词返回 true 的元素。
用户9831583
2022/06/16
2650
STL中find的七大姑八大姨
C++primer笔记之关联容器
在这一章中,有以下的几点收获: 1、pair类型的使用相当频繁,如果需要定义多个相同的pair类型对象,可考虑利用typedef简化其声明: typedef pair<string, string> A;这样,在后面的使用中就可以直接用A来代替前面繁琐的书写。 2、三种方法创建pair对象: (1)第一种方法:使用函数make_pair() pair<string, string> spair; string first, last; while(cin >> first >> last) {   spai
Linux云计算网络
2018/01/10
7190
C++primer笔记之关联容器
字符串问题-LeetCode 397、398、401、404、405、406(蓄水池抽样)
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/integer-replacement
算法工程师之路
2019/12/27
4750
Leetcode | 第B节:数组综合题(2)
抱歉这一节相对隔得时间长了一些再发出来,因为这几天基本上主要时间都在关注东京奥运会的比赛现场。在发表这篇文章的时候,也恰好知道名将苏炳添以9‘83’‘的时间晋级决赛,我认为他可以以这个成绩再拿一次金牌,希望我的预言成真2333
学弱猹
2021/08/06
4350
LeetCode Contest 177
给你一些点和边,判断是否是一颗二叉树。只需要判断所有点的入度<=1 ,并且入度为0的点只有一个,就可以了。
ShenduCC
2020/02/25
2910
leetcode刷题系列/2
4.罗马数字转整数 定义见代码,示例: 输入 输出 IV 4(1在5的左边,大数减小数) LVIII 58(小数在左,大数在右,50+5+3) III 3 代码: #include <iostream> #include <unordered_map> #include <string> #include <memory> using namespace std; // int main() // { // string input="XII"; // unordered_map<c
用户9831583
2022/06/16
2200
学好这13种数据结构,应对各种编程语言(C++版)
学了这么长时间数据结构和算法,有必要来个总结了,顺便回顾一下我们这段时间的学习成果。以 C++ 语言本身提供的数据结构为例。如果能掌握这 13 种数据结构,相信在学习其它语言的时候就不费劲了。
五分钟学算法
2019/07/14
1.5K0
相关推荐
LeetCode 760. 找出变位映射(哈希)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验