Leetcode 760. Find Anagram Mappings, java解法

亚麻 OA 2019 题目 Most Common Word Prison Cells After N Days K Closest Points to Origin Reorder Log Files Partition Labels Min Cost to Add New Roads Min distance to remove the obstacle Roll Dice Min Cost to Con... 阅读详情

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

--------------------------------------------------------------------------------------------------------------------------------------

Approach #1 Brute Force, 我的解法

class Solution {
    public int[] anagramMappings(int[] A, int[] B) {
        
        int len = A.length;
        int[] ans = new int[len];

        for(int i=0; i<len; i++){
            for(int j=0;j<len;j++){
                if(A[i]==B[j]){
                    ans[i]=j;
                }
            }
        }
            
        return ans;
    }
}

分析: 

虽然通过了,但是没有处理可能存在的A, B有重复元素的情况。不过题目说如有多解,允许返回任意解,可以通过。

时间复杂度: O(A.length ^2)

空间复杂度:O(A.length)

知识点:

java语言使用new 来创建数组。

dataType[] arrayRefVar = new dataType[arraySize];

或者

dataType[] arrayRefVar = {value0, value1, ..., valuek};

 

Approach #2 Hash Map

class Solution {
    public int[] anagramMappings(int[] A, int[] B) {
        Map<Integer, Integer> D = new HashMap();
        for (int i = 0; i < B.length; ++i)
            D.put(B[i], i);

        int[] ans = new int[A.length];
        int t = 0;
        for (int x: A)
            ans[t++] = D.get(x);
        return ans;
    }
}

分析:

时间复杂度:O(A.length)

空间复杂度: O(A.length)

知识点:

1. HashMap

Type Parameters:

K - the type of keys maintained by this map

V - the type of mapped values

Map<K,V> nameOfMap = new HashMap();

2. Method

nameOfMap.get(): Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

nameOfMap.put(K Key, V Value) : Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.

Leetcode 250. Count Univalue Subtrees 谷歌面试, Java解法详解 这是一道谷歌会考的题目 Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of the subtree have the same value. Example : Input: root = [5,1,5,5,5,null,5] ... 阅读详情

相关推荐

运算放大器测试方法-输入失调电压

由于运放输入回路的不对称性,在输入端将产生一定的误差信号,从而限制运放信号灵敏度。通常用输入失调电压(VOS)、输入失调电流(Ios)和输入偏置电流(IB)表示。运放有四个主要特征:输入失调特征、开环特征、共模特征及输出特征。其放大倍数较大,由于外界的影响,即使运放的输入端产生较小的输入信号,经过放大,在输出端也会产生较大的误差。为能精确测量其性能,一般采用辅助运放进行测试。下面着重讲了运放的主要参数及如何精确测量这些参数。文中DUT代表被测运放,A代表辅助运放。

weixin_46135931的博客 1万+

【LeetCode】760. Find Anagram Mappings

Python版本:Python3.6.2760. Find Anagram MappingsGiven 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 in

sunflower_kris的博客 401

【Azure 架构师学习笔记】- Azure Private Endpoint

公有云的其中一个特点是默认允许公网访问, 这就对企业环境带来风险,也是很多年前企业对公有云抵触的其中一个原因,现在这类问题已经很少,因为有了很多技术来确保云上的资源被安全地访问。其中Private endpoint(PE)就起到了很重要的作用。云上的某个资源如VM会创建在特定的网络(VNet/Subnet)上, 而其他如Storage Account , Azure SQL等PaaS服务则没有。如果你需要用VM 来访问这些PaaS资源,VM 就会通过资源的公网IP 来访问。

MVP黄钊吉(發糞塗牆) 1787

LWC 66: 760. Find Anagram Mappings

LWC 66: 760. Find Anagram Mappings 传送门:760. Find Anagram Mappings Problem: 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

Demon-初来驾到 857

LwIP在国产MCU的落地实践:从协议栈优化到网络稳定性实战

本文详细介绍了LwIP协议栈在国产GD32F407 MCU上的移植与优化实践,涵盖硬件平台选择、驱动适配、协议栈调优和网络稳定性提升策略。通过内存管理优化、实时操作系统集成和性能测试,为工业物联网应用提供高可靠性网络通信解决方案,显著提升数据传输效率和系统稳定性。

m2n3b4v5c6的博客 268

WinCC与PLC在西门子逻辑赛项中的通信配置避坑指南

本文针对西门子逻辑赛项中WinCC与PLC通信配置的常见问题,提供了一套实战避坑指南。重点解析了WinCC、PLC与EET仿真平台的三角通信架构,强调了IP地址规划、防火墙设置等关键环节的注意事项,并给出了系统化的故障诊断流程与排查技巧,帮助选手和技术人员快速定位并解决通信故障,确保监控系统稳定运行。

weixin_29165725的博客 107

【Leetcode】760. Find Anagram Mappings

题目地址: https://leetcode.com/problems/find-anagram-mappings/ 给定两个数组AAA和BBB,它们数字构成一样,题目保证AAA里数字各不相同。要求返回一个数组CCC,使得B[C[i]]=A[i]B[C[i]]=A[i]B[C[i]]=A[i]。 代码如下: import java.util.HashMap; import java.util.Map; public class Solution { public int[] anagramMapp

数学、算法爱好者的博客 195

Couldn‘t find device with uuid 恢复LVM元数据 linux

Couldn't find device with uuid 恢复LVM元数据 linux欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 欢迎使用Markdown编辑器 你好! 这是你第一次使

m0_57559579的博客 7166

【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

负雪明烛 1368

从零抓包分析AVDTP:用Wireshark解密蓝牙音频传输全流程

本文详细介绍了如何使用Wireshark抓包工具分析AVDTP协议,解密蓝牙音频传输的全流程。从实验环境搭建到信令交互解析,再到媒体流传输深度分析,逐步揭示蓝牙音频传输的技术本质,帮助开发者深入理解AVDTP协议的工作机制。

sun99的博客 204

EPICS pythonSoftIOC教程

EPICS pythonSoftIOC教程

yuyuyuliang00的博客 1521

LeetCode 760. Find Anagram Mappings

原题链接在这里:https://leetcode.com/problems/find-anagram-mappings/description/ 题目: Given two listsAandB, andBis an anagram ofA.Bis an anagram ofAmeansBis made by randomizing the order of the el...

weixin_30767835的博客 122

论文研读 Corundum: An Open-Source 100-Gbps NIC

Corundum 通过在 FPGA 上构建并行 pipeline、高效 DMA 通道、无阻塞队列结构、低延迟调度器,实现了在 PCIe Gen3 x16 接口上稳定跑满 100Gbps 吞吐量。

Double Galaxy 489

[LeetCode] 760. Find Anagram Mappings

Problem 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 map...

weixin_34244102的博客 105

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

[Leetcode] 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 randomizing the order of the elements in A. We want to find an index mapping P, from A to B.

魔豆(Magicbean)的博客 298

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

LeetCode 760 Find Anagram Mappings 查找字谜映射

Giventwo lists Aand B, and B is  an anagram of A. B is an anagram of A means B is  made by randomizing theorder of the elements in A. Wewant to find an index mapping P, from A to B.  A mapping P[i] = ...

qq_34501451的博客 217

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

Leetcode 1086. High Five Java 解法

先吐槽一下这个难度,咋也不能是easy啊,和其他easy的不在一个量级。 Given a list of scores of different students, return the average score of each student'stop five scoresinthe order of each student's id. Each entryitems[i]h...

YuanTheCoder的博客 1473

Leetcode 1051. Height Checker, Java解法

Students are asked to stand in non-decreasing order of heights for an annual photo. Return the minimum number of students not standing in the right positions. (This is the number of students that mu...

YuanTheCoder的博客 649

Leetcode 1108. Defanging an IP Address, Java解法

Given a valid (IPv4) IPaddress, return a defanged version of that IP address. AdefangedIP addressreplaces every period"."with"[.]". Example 1: Input: address = "1.1.1.1" Output: "1[.]1[....

YuanTheCoder的博客 574

Java中 Queue队列的用法及例子

1. Usage 用法 // "static void main" must be defined in a public class. public class Main { public static void main(String[] args) { // 1. Initialize a queue. Queue<Integer> q...

YuanTheCoder的博客 557

Leetcode 509. Fibonacci Number , Java 解法

TheFibonacci numbers, commonly denotedF(n)form a sequence, called theFibonacci sequence, such that each number is the sum of the two preceding ones, starting from0and1. That is, F(0) = 0, F(...

YuanTheCoder的博客 503

Leetcode 977. Squares of a Sorted Array, Java解法

Given an array of integersAsorted in non-decreasing order,return an array of the squares of each number,also in sorted non-decreasing order. Example 1: Input: [-4,-1,0,3,10] Output: [0,1,9,16,1...

YuanTheCoder的博客 497

狗家 Online Assessment

License Key Formatting Unique Email Addresses Fruit Into Baskets Odd Even Jump Maximum Area Serving Cake Min Days to Bloom Fill Matrix Compare Strings Largest Subarray Length K Decreasing Sub...

YuanTheCoder的博客 470

Leetcode 922. Sort Array By Parity II , Java解法

Given an arrayAof non-negative integers, half of the integers in A are odd, and half of the integers are even. Sort the array so that wheneverA[i]is odd,iis odd; and wheneverA[i]is even,iis...

YuanTheCoder的博客 444

Leetcode [141, 142] Linked List Cycle,java解法

141 Linked List Cycle Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integerposwhich represents the position (0-indexed)in the l...

YuanTheCoder的博客 419
上一篇: Leetcode 1108. Defanging an IP Address, Java解法
下一篇: Leetcode 1064. Fixed Point, Java解法
YuanTheCoder
博客等级 码龄9年 2粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值