โจทย์ทั้งหมด
ปานกลาง 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Flatten Arbitrarily Nested Lists (Recursion)

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

โจทย์

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.

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก