모든 챌린지
보통 1명 해결 정답률 100.0%

Flatten Arbitrarily Nested Lists (Recursion)

Practice recursion by flattening arbitrarily nested lists while skipping null entries.

배우게 될 내용

  • Flatten lists of any depth with recursion
  • Skip null entries
  • Define a base case and a recursive case

문제

번역본 보기

This is the advanced follow-up to Flatten a 2D Array. Practice that challenge first. Here lists may contain more lists at any depth. In C++ and C#, use the NestedValue type in the starter code. Read kind first: 0 means skip this entry, 1 means append value, and 2 means visit items recursively.

Turn a nested list into a flat list of integers. Read from left to right; whenever you reach another list, read its contents before continuing. Ignore null values (shown as None in examples) and empty lists. Preserve repeated integers and their order.

Python receives ordinary nested lists containing integers and None. C++ and C# receive a list of NestedValue objects supplied by the starter: kind 0 means null, kind 1 means integer in value, and kind 2 means a list in items. Only the field selected by kind matters. Return a plain integer list in every language.

Implement flatten_arbitrarily_nested_list(values).

Input

values: A list whose entries can be integers, null, or further lists.

Output

Return the integers in depth-first, left-to-right order. Return an empty list when there are no integers.

제약 조건

The outer value is a list. At most 10,000 entries across all lists; maximum nesting depth 50. Integer leaves fit signed 64-bit. Leaves are integers or null, never strings or booleans.

예시

예시 1
입력:
values = [
    1,
    [
        2,
        None,
        [3]
    ],
    [],
    4
]

예상 출력:
expected = [1, 2, 3, 4]

시작 코드에 제공된 NestedValue 타입을 사용하세요. 필드는 kind (0 = null, 1 = integer, 2 = list), value (정수), items (중첩 리스트)입니다.

입력:
std::vector<NestedValue> values = {
    NestedValue{1, 1, {}},
    NestedValue{2, 0, {
        NestedValue{1, 2, {}},
        NestedValue{0, 0, {}},
        NestedValue{2, 0, {
            NestedValue{1, 3, {}}
        }}
    }},
    NestedValue{2, 0, {}},
    NestedValue{1, 4, {}}
};

예상 출력:
std::vector<long long> expected = {1, 2, 3, 4};

시작 코드에 제공된 NestedValue 타입을 사용하세요. 필드는 kind (0 = null, 1 = integer, 2 = list), value (정수), items (중첩 리스트)입니다.

입력:
NestedValue[] values = new NestedValue[] {
    new NestedValue { kind = 1, value = 1 },
    new NestedValue { kind = 2, items = new NestedValue[] {
        new NestedValue { kind = 1, value = 2 },
        new NestedValue { kind = 0 },
        new NestedValue { kind = 2, items = new NestedValue[] {
            new NestedValue { kind = 1, value = 3 }
        } }
    } },
    new NestedValue { kind = 2, items = new NestedValue[] {} },
    new NestedValue { kind = 1, value = 4 }
};

예상 출력:
long[] expected = new long[] {1, 2, 3, 4};

설명

Read 1, then enter the nested list and read 2 and 3, skipping null. The empty list adds nothing; finish with 4.

예시 2
입력:
values = [
    None,
    [],
    [None]
]

예상 출력:
expected = []

시작 코드에 제공된 NestedValue 타입을 사용하세요. 필드는 kind (0 = null, 1 = integer, 2 = list), value (정수), items (중첩 리스트)입니다.

입력:
std::vector<NestedValue> values = {
    NestedValue{0, 0, {}},
    NestedValue{2, 0, {}},
    NestedValue{2, 0, {
        NestedValue{0, 0, {}}
    }}
};

예상 출력:
std::vector<long long> expected = {};

시작 코드에 제공된 NestedValue 타입을 사용하세요. 필드는 kind (0 = null, 1 = integer, 2 = list), value (정수), items (중첩 리스트)입니다.

입력:
NestedValue[] values = new NestedValue[] {
    new NestedValue { kind = 0 },
    new NestedValue { kind = 2, items = new NestedValue[] {} },
    new NestedValue { kind = 2, items = new NestedValue[] {
        new NestedValue { kind = 0 }
    } }
};

예상 출력:
long[] expected = new long[] {};

설명

Every entry is null or an empty container. No integer remains.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Think of each list as something to enter, finish, and then return from later.

힌트 2

Use depth-first traversal, either with recursion or an explicit stack, to preserve left-to-right order.

힌트 3

Ignore null entries and empty lists, and make sure deeply nested values still appear in the correct sequence.

예상 복잡도

O(n) time, O(d) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제약 조건을 기준으로 풀어 주세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 ННикита Python 800 ms - 5/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 풀이

스포일러: 아래에 전체 풀이 1개가 있습니다 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.