0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Create Target Array in the Given Order 〜leetcode〜

Posted at

#〜問題文〜

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

引用元: https://leetcode.com/problems/create-target-array-in-the-given-order/
  

〜回答例〜


def create_target_array(nums, index)
    target = []
    (0...nums.size).each do |indx|
        x = nums[indx]
        i = index[indx]
        if 0 == i
            target = [x] + target
        else
            target = target[0...i] + [x] + target[i..-1]
        end
    end
    return target
end

回答例引用元(一部改変):
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/852650/Ruby%3A-On-insertion-divide-into-tow-parts-and-put-an-element-in-the-gap.
#以下、上記回答の分解分析

1. メソッドの定義


def create_target_array(nums, index)
         
         〜省略〜

end

「create_target_array」というメソッドを定義。
第一仮引数=「nums」と第二仮引数= 「index」と定義する。

2. メソッドの処理


    target = []
  

空の配列「target」を定義する。


    
    (0...nums.size).each do |indx|
          
            〜省略〜

    end
   

 「0〜nums.size(=配列numsの要素数)」 ← 配列
 ブロック変数を「idx」として、繰り替えじ処理を行う。


    
        x = nums[indx]
        i = index[indx]
      

「 x 」に対して「nums[配列の添字(=idx)]」代入する。
同様に、
「 i 」に対して「index[配列の添字(=idx)]」代入する。


   
        if 0 == i
            target = [x] + target
        else
            target = target[0...i] + [x] + target[i..-1]
        end
   

【if】
もし、0 == i であったら、配列 「target」 に 「x(=nums[indx])」を加算していく。

【else】
そうでなければ「target[0...i(=index[indx])]」 + [x(= nums[indx])] + 「target[i(=index[indx])..-1]」


まずい、「...」と「..」が説明できない。。。
調べておきます。。。」

処理後再びループへ。



   return target

   

nums.sizeまでの分の繰り返し処理を終えたら、最終的な「target」を返却して終了。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?