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;
        }
    }
    
  • 相关阅读:
    SQL SERVER 2005生成带数据的脚本文件 [work around]
    VB.NET窗体关闭事件
    Code::Blocks The open source, cross platform, free C++ IDE.
    VB Twips And Pixels 缇和像素
    JQuery 鼠标点击其它地方隐藏层
    Asp.net 基于Form的权限方法备忘
    JQuery Highcharts图表控件多样式显示多组数据
    ASP.NET中动态获取数据使用Highcharts图表控件
    使用windows服务和.NET FileSystemWatcher对象来监控磁盘文件目录的改变
    【JQuery插件】Select选择框的华丽变身
  • 原文地址:https://www.cnblogs.com/mapoos/p/14220776.html
Copyright © 2011-2022 走看看