반응형
짝수개의 숫자로 이뤄진 배열이 있다.
배열의 숫자들을 앞에서부터 두 개씩 짝지어서
뒤의 숫자를 앞 숫자만큼 추가한다.
class Solution {
public:
vector<int> decompressRLElist(vector<int>& nums) {
vector<int>ans;
for(int i = 0; i<nums.size()-1; i+=2)
for(int j = 0;j<nums[i]; j++)
ans.push_back(nums[i+1]);
return ans;
}
};
두 개씩 짝지어 있기 때문에 첫 번째 for문의 i값은 2개씩 증가시킨다.
반응형
'IT > 알고리즘' 카테고리의 다른 글
10] Leetcode 1389. Create Target Array in the Given Order (0) | 2020.06.23 |
---|---|
9] Leetcode 1281. Subtract the Product and Sum of Digits of an Integer (0) | 2020.06.22 |
7] Leetcode 1365. How Many Numbers Are Smaller Than the Current Number (0) | 2020.06.21 |
6] Leetcode 771. Jewels and Stones (0) | 2020.06.21 |
5] Leetcode 1342. Number of Steps to Reduce a Number to Zero (0) | 2020.06.21 |
댓글