close

題目: 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. 解題: 將nums,index兩個list使用zip打包然後循序插入即可。 Python Code:

class Solution:
    def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
        ans=[]
        for a,b in zip(nums,index):
            ans.insert(b,a)
        return ans

arrow
arrow
    文章標籤
    leetcode python python3
    全站熱搜
    創作者介紹
    創作者 低階ㄇㄋ 的頭像
    低階ㄇㄋ

    蟲匯聚之所

    低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()