[LeetCode] Concatenation of Array

아르당·2026년 7월 19일

LeetCode

목록 보기
413/565
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

길이가 n인 정수 배열 nums가 주어졌을때, 길이가 2n인 배열 ans를 만들고 싶다. ans[i] == nums[i]이고 ans[i + n] == nums[i]에 대해 0 <= i < n(0부터 시작)이다.
구체적으로, ans는 2개의 배열 nums를 이어 붙인 것이다.
배열 ans를 반환해라.

Example

#1
Input: nums = [1, 2, 1]
Output: [1, 2, 1, 2, 1]

#2
Input: nums = [1,3, 2, 1]
Output: [1, 3, 2, 1, 3, 2, 1]

Constraints

  • n == nums.length
  • 1 <= n <= 1000
  • 1 <= nums[i] <= 1000

Solved

class Solution {
    public int[] getConcatenation(int[] nums) {
        int n = nums.length;
        int[] ans = new int[n * 2];

        for(int i = 0; i < n * 2; i++){
            ans[i] = nums[i % n];
        }

        return ans;
    }
}
profile
내 마음대로 코드 작성하는 세상

0개의 댓글