Create Target Array in the Given Order 〜leetcode〜
〜問題文〜
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
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
以下、上記回答の分解分析
1. メソッドの定義
def create_target_array(nums, index)
〜省略〜
end
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」を返却して終了。
Author And Source
この問題について(Create Target Array in the Given Order 〜leetcode〜), 我々は、より多くの情報をここで見つけました https://qiita.com/Tech_geologist/items/702e6c5aba55f7e26c95著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .