leetCode 문제 풀이 1389번 Create Target Array in the Given Order (JS)

devmomo·2021년 3월 11일
0

알고리즘

목록 보기
20/52
post-thumbnail

1389. Create Target Array in the Given Order

문제
정수를 원소로 하는 nums와 index 두 배열이 주어졌을 때, 조건을 만족하는 함수 만들기

조건
1. 리턴할 배열 target은 빈 배열로 주어짐
2. nums[i] 와 index[i]를 읽되, index[i]를 인덱스로하는 값 nums[i]를 target에 삽입
3. nums와 index 배열을 전부 순회하면 종료

가정
1. nums와 index의 배열 길이는 1이상 100이하
2. nums와 index의 배열 길이는 동일
3. nums[i]와 index[i]는 0이상 100이하의 정수

풀이

var createTargetArray = function(nums, index) {
const target = [];
const result = nums.forEach((el,elindex)=>{
    let temp = index[elindex];
    target.splice(temp,0,el);
})
return target;
};
profile
FE engineer

0개의 댓글