Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

1929. Concatenation of Array

Given array nums, return array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n.

Approach: allocate result of double size upfront, fill by indexing source with modulo.

#![allow(unused)]
fn main() {
// time O(2n), space O(2n)
pub fn get_concatenation(nums: Vec<i32>) -> Vec<i32> {
    let original_size = nums.len();
    let size = original_size * 2;
    let mut ans: Vec<i32> = Vec::with_capacity(size);
    for i in 0..size {
        ans.push(nums[i % original_size]);
    }

    ans
}
}