Легко Решено: 0 Процент решений: 0%

Flatten a 2D Array

Combine the rows of a 2D integer array into one array.

Чему вы научитесь

  • Перебирать строки и столбцы
  • Добавлять значения в один список
  • Сохранять исходный порядок

Условие

Combine the rows of a 2D integer array into one array. Read rows from top to bottom and integers within each row from left to right. Skip empty rows. Preserve repeated integers and their order. Rows may have different lengths. There are exactly two array levels: rows contain only integers, with no null values or deeper lists.

Implement flatten_nested_list(values).

Input

values: The integer rows to combine, in order.

Output

Return all integers in row order. Return an empty array if there are no integers.

Ограничения

0 ≤ number of rows ≤ 10,000. At most 10,000 integers in total. Each integer is between −1,000,000,000 and 1,000,000,000. Rows may be empty or have different lengths.

Примеры

Пример 1
Ввод:
values = [
    [1, 2],
    [],
    [3, 4]
]

Ожидаемый вывод:
expected = [1, 2, 3, 4]
Ввод:
std::vector<std::vector<long long>> values = {
    {1, 2},
    {},
    {3, 4}
};

Ожидаемый вывод:
std::vector<long long> expected = {1, 2, 3, 4};
Ввод:
long[][] values = new long[][] {
    new long[] {1, 2},
    new long[] {},
    new long[] {3, 4}
};

Ожидаемый вывод:
long[] expected = new long[] {1, 2, 3, 4};

Пояснение

Read 1 and 2 from the first row. Skip the empty second row. Append 3 and 4 from the last row.

Пример 2
Ввод:
values = [
    [],
    []
]

Ожидаемый вывод:
expected = []
Ввод:
std::vector<std::vector<long long>> values = {
    {},
    {}
};

Ожидаемый вывод:
std::vector<long long> expected = {};
Ввод:
long[][] values = new long[][] {
    new long[] {},
    new long[] {}
};

Ожидаемый вывод:
long[] expected = new long[] {};

Пояснение

Both rows are empty, so there are no integers to append.

Как подступиться

Сначала попробуйте сами, затем открывайте подсказки по одной.

Подсказка 1

Обойди строки по порядку и добавляй каждое встреченное число.

Подсказка 2

Пустые строки ничего не добавляют, но порядок всех значений из непустых строк нужно сохранить.

Подсказка 3

Вход только двух уровней глубины, так что рекурсивное flattening не нужно.

Ожидаемая сложность

время O(n), доп. память O(n)

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.