문제풀이 2021 02 07
2021.02.07 연습
- 사용언어 : java
class Solution {
public int[] createTargetArray(int[] nums, int[] index) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
list.add(index[i], nums[i]);
}
int j =0;
int[] target = new int[list.size()];
for (int v : list) {
target[j++] = v;
}
return target;
}
}
Runtime: 1 ms, faster than 43.76% of Java online submissions for Create Target Array in the Given Order. Memory Usage: 37.8 MB, less than 34.13% of Java online submissions for Create Target Array in the Given Order.
- 다른 코드 참고
public int[] createTargetArray(int[] nums, int[] index) {
List<Integer> list = new LinkedList<>();
IntStream.range(0, index.length)
.forEach(i -> list.add(index[i], nums[i]));
// convert the LL to an array
return list.stream()
.mapToInt(i -> i)
.toArray();
}
class Solution {
public int[] createTargetArray(int[] nums, int[] index) {
List<Integer> x = new ArrayList<>();
for(int i=0; i<nums.length; i++){
x.add(index[i], nums[i]);
}
for(int i=0; i<nums.length; i++)
nums[i] = x.get(i);
return nums;
}
}