首页 > 其他分享 >leetcode-1389-easy

leetcode-1389-easy

时间:2023-03-11 13:45:32浏览次数:35  
标签:index target nums int 1389 easy Input array leetcode

Create Target Array in the Given Order

Given two arrays of integers nums and index. Your task is to create target array under the following rules:

Initially target array is empty.
From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array.
Repeat the previous step until there are no elements to read in nums and index.
Return the target array.

It is guaranteed that the insertion operations will be valid.

Example 1:

Input: nums = [0,1,2,3,4], index = [0,1,2,2,1]
Output: [0,4,1,3,2]
Explanation:
nums       index     target
0            0        [0]
1            1        [0,1]
2            2        [0,1,2]
3            2        [0,1,3,2]
4            1        [0,4,1,3,2]
Example 2:

Input: nums = [1,2,3,4,0], index = [0,1,2,3,0]
Output: [0,1,2,3,4]
Explanation:
nums       index     target
1            0        [1]
2            1        [1,2]
3            2        [1,2,3]
4            3        [1,2,3,4]
0            0        [0,1,2,3,4]
Example 3:

Input: nums = [1], index = [0]
Output: [1]
Constraints:

1 <= nums.length, index.length <= 100
nums.length == index.length
0 <= nums[i] <= 100
0 <= index[i] <= i

思路一:用 List 实现

    public int[] createTargetArray(int[] nums, int[] index) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            list.add(index[i], nums[i]);
        }

        return list.stream().mapToInt(Integer::intValue).toArray();
    }

标签:index,target,nums,int,1389,easy,Input,array,leetcode
From: https://www.cnblogs.com/iyiluo/p/17205774.html

相关文章

  • leetcode-1544-easy
    MakeTheStringGreatGivenastringsofloweranduppercaseEnglishletters.Agoodstringisastringwhichdoesn'thavetwoadjacentcharacterss[i]and......
  • leetcode-977-easy
    SquaresofaSortedArrayGivenanintegerarraynumssortedinnon-decreasingorder,returnanarrayofthesquaresofeachnumbersortedinnon-decreasingor......
  • leetcode-1021-easy
    RemoveOutermostParenthesesAvalidparenthesesstringiseitherempty"","("+A+")",orA+B,whereAandBarevalidparenthesesstrings,and+represe......
  • leetcode-1496-easy
    PathCrossingGivenastringpath,wherepath[i]='N','S','E'or'W',eachrepresentingmovingoneunitnorth,south,east,orwest,respectively.Youstart......
  • leetcode-1523-easy
    CountOddNumbersinanIntervalRangeGiventwonon-negativeintegerslowandhigh.Returnthecountofoddnumbersbetweenlowandhigh(inclusive).Example......
  • LeetCode|2457. 美丽整数的最小增量
    题目链接:2457.美丽整数的最小增量给你两个正整数n和target。如果某个整数每一位上的数字相加小于或等于target,则认为这个整数是一个美丽整数。找出并返回满......
  • 【LeetCode回溯算法#07】子集问题I+II,巩固解题模板并详解回溯算法中的去重问题
    子集力扣题目链接给你一个整数数组nums,数组中的元素互不相同。返回该数组所有可能的子集(幂集)。解集不能包含重复的子集。你可以按任意顺序返回解集。示例1:输......
  • eNSP的使用4  NAT网络地址转换 easyip
    静态转换:一对一绑定 (双向通信)路由器靠地址区分内部主机easy ip :多对一  (单向通信)路由器靠端口(临时端口)区分内部主机easy ip的配置:1.配置aclacl  2000rule perm......
  • easyexcel填坑-校验表头为空,或者不符合预期
    背景:easyexcelv3.1.5实体类已经使用注解@ExcelProperty标注需要导入的属性正文开始关闭忽略空行,防止第一行是空跳过校验 ignoreEmptyRow(false)。此处如果未关闭,第......
  • Leetcode24. 两两交换链表中的节点
    题目描述:给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。 思路:使用虚拟头结点,这样会......