IT/알고리즘
8] Leetcode 1313. Decompress Run-Length Encoded List
깻잎쌈
2020. 6. 21. 17:06
반응형
Decompress Run-Length Encoded List - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
짝수개의 숫자로 이뤄진 배열이 있다.
배열의 숫자들을 앞에서부터 두 개씩 짝지어서
뒤의 숫자를 앞 숫자만큼 추가한다.
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개씩 증가시킨다.
반응형