Tất cả thử thách
Trung bình 1 người đã giải Tỷ lệ chấp nhận 100.0%

Flatten Arbitrarily Nested Lists (Recursion)

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

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.

Ví dụ

Ví dụ 1
Đầu vào:
values = [
    1,
    [
        2,
        None,
        [3]
    ],
    [],
    4
]

Kết quả mong đợi:
expected = [1, 2, 3, 4]

Sử dụng kiểu NestedValue có sẵn trong code khởi đầu. Các trường gồm kind (0 = null, 1 = số nguyên, 2 = danh sách), value (số nguyên), và items (danh sách lồng nhau).

Đầu vào:
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, {}}
};

Kết quả mong đợi:
std::vector<long long> expected = {1, 2, 3, 4};

Sử dụng kiểu NestedValue có sẵn trong code khởi đầu. Các trường gồm kind (0 = null, 1 = số nguyên, 2 = danh sách), value (số nguyên), và items (danh sách lồng nhau).

Đầu vào:
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 }
};

Kết quả mong đợi:
long[] expected = new long[] {1, 2, 3, 4};

Giải thích

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

Ví dụ 2
Đầu vào:
values = [
    None,
    [],
    [None]
]

Kết quả mong đợi:
expected = []

Sử dụng kiểu NestedValue có sẵn trong code khởi đầu. Các trường gồm kind (0 = null, 1 = số nguyên, 2 = danh sách), value (số nguyên), và items (danh sách lồng nhau).

Đầu vào:
std::vector<NestedValue> values = {
    NestedValue{0, 0, {}},
    NestedValue{2, 0, {}},
    NestedValue{2, 0, {
        NestedValue{0, 0, {}}
    }}
};

Kết quả mong đợi:
std::vector<long long> expected = {};

Sử dụng kiểu NestedValue có sẵn trong code khởi đầu. Các trường gồm kind (0 = null, 1 = số nguyên, 2 = danh sách), value (số nguyên), và items (danh sách lồng nhau).

Đầu vào:
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 }
    } }
};

Kết quả mong đợi:
long[] expected = new long[] {};

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 Н"Никита "Programmer" Python 800 ms - 5/5 Xem code

Học hỏi từ người khác

Lời giải cộng đồng

Cảnh báo: Có 1 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem code của người khác và nhận xét của AI. Đăng nhập và giải thử thách để tham gia thảo luận.