Skip to content

1313. Decompress Run-Length Encoded List 👎

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
 public:
  vector<int> decompressRLElist(vector<int>& nums) {
    vector<int> ans;

    for (int i = 0; i < nums.size(); i += 2)
      ans.insert(end(ans), nums[i], nums[i + 1]);

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
  public int[] decompressRLElist(int[] nums) {
    List<Integer> ans = new ArrayList<>();

    for (int i = 0; i < nums.length; i += 2)
      for (int freq = 0; freq < nums[i]; ++freq)
        ans.add(nums[i + 1]);

    return ans.stream().mapToInt(i -> i).toArray();
  }
}
1
2
3
4
5
6
7
8
class Solution:
  def decompressRLElist(self, nums: List[int]) -> List[int]:
    ans = []

    for i in range(0, len(nums), 2):
      ans += [nums[i + 1]] * nums[i]

    return ans