문제풀이 2021 02 04
2021.02.04 연습
- 사용언어 : java
class Solution {
public int[] decompressRLElist(int[] nums) {
int length = 0;
for (int i = 0 ; i < nums.length; i +=2 ) {
length += nums[i];
}
int[] result = new int[length];
int idx = 0;
for (int n = 0; n < nums.length; n += 2) {
int cnt = nums[n];
while (cnt > 0) {
result[idx] = nums[n+1];
idx ++ ;
cnt --;
}
}
return result;
}
}
Runtime: 1 ms, faster than 91.40% of Java online submissions for Decompress Run-Length Encoded List. Memory Usage: 39 MB, less than 99.12% of Java online submissions for Decompress Run-Length Encoded List.
- 다른 코드 참고
class Solution {
public int[] decompressRLElist(int[] nums) {
int size = 0;
for(int i = 0; i < nums.length; i += 2) {
size += nums[i];
}
int[] r = new int[size];
int index = 0;
for(int i = 0; i < nums.length; i += 2) {
Arrays.fill(r, index, index + nums[i], nums[i + 1]); //This is fast
index += nums[i];
}
return r;
}
}