zoukankan      html  css  js  c++  java
  • 1640. Check Array Formation Through Concatenation (E)

    Check Array Formation Through Concatenation (E)

    题目

    You are given an array of distinct integers arr and an array of integer arrays pieces, where the integers in pieces are distinct. Your goal is to form arr by concatenating the arrays in pieces in any order. However, you are not allowed to reorder the integers in each array pieces[i].

    Return true if it is possible to form the array arr from pieces. Otherwise, return false.

    Example 1:

    Input: arr = [85], pieces = [[85]]
    Output: true
    

    Example 2:

    Input: arr = [15,88], pieces = [[88],[15]]
    Output: true
    Explanation: Concatenate [15] then [88]
    

    Example 3:

    Input: arr = [49,18,16], pieces = [[16,18,49]]
    Output: false
    Explanation: Even though the numbers match, we cannot reorder pieces[0].
    

    Example 4:

    Input: arr = [91,4,64,78], pieces = [[78],[4,64],[91]]
    Output: true
    Explanation: Concatenate [91] then [4,64] then [78]
    

    Example 5:

    Input: arr = [1,3,5,7], pieces = [[2,4,6,8]]
    Output: false
    

    Constraints:

    • 1 <= pieces.length <= arr.length <= 100
    • sum(pieces[i].length) == arr.length
    • 1 <= pieces[i].length <= arr.length
    • 1 <= arr[i], pieces[i][j] <= 100
    • The integers in arr are distinct.
    • The integers in pieces are distinct (i.e., If we flatten pieces in a 1D array, all the integers in this array are distinct).

    题意

    判断一个一维数组能不能由一系列一维数组拼接而成。

    思路

    先建立一个HashMap,key为pieces中每个一维数组的第一个数字,value为该一维数组在pieces中的下标。遍历arr,如果能拼接成,那么arr的第一个数字一定对应于pieces中某个数组x的第一个数字,通过HashMap找到对应的数组x并记录其下标,让遍历的i跳过x的长度,重复步骤,最终得到一组下标序列,按照该序列进行拼接并与arr比较,最后得出结论。


    代码实现

    Java

    class Solution {
        public boolean canFormArray(int[] arr, int[][] pieces) {
            Map<Integer, Integer> hash = new HashMap<>();
            int[] compare = new int[arr.length];
            int p = 0, q = 0;
          
          	for (int i = 0; i < pieces.length; i++) {
                hash.put(pieces[i][0], i);
            }
          
            while (p < arr.length) {
                if (!hash.containsKey(arr[p])) {
                    return false;
                }
    
                int index = hash.get(arr[p]);
                for (int i = 0; i < pieces[index].length; i++) {
                    compare[q++] = pieces[index][i];
                }
                p += pieces[index].length;
            }
    
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] != compare[i]) {
                    return false;
                }
            }
          
            return true;
        }
    }
    
  • 相关阅读:
    IfcFurnishingElementType
    IfcRelAssociatesClassification
    IfcRelAssociatesDocument
    IfcContext
    IfcRelAssociatesMaterial
    我是高敏感的人,你呢?
    介绍一本红色的书
    矫枉必须过正
    大家都在说的民法典,与我有何关系?
    线上Kafka突发rebalance异常,如何快速解决?
  • 原文地址:https://www.cnblogs.com/mapoos/p/14220776.html
Copyright © 2011-2022 走看看