[코테 풀이] XOR Operation in an Array

시내·2024년 6월 12일

Q_1486) XOR Operation in an Array

출처 : https://leetcode.com/problems/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.

class Solution {
    public int[] createTargetArray(int[] nums, int[] index) {
        List<Integer> l = new ArrayList<>();
        int ind = 0;
        int[] target = new int[nums.length];
        for (int a = 0; a < nums.length; a++) {
            l.add(index[a], nums[a]);
            System.out.println(target[index[a]]);
        }
        for (int ll : l) {
            target[ind++] = ll;
        }
        return target;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글