문제
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.
예시
입력:
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.
입력:
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
입력은 두 단계만 중첩되므로 재귀적으로 펼칠 필요는 없습니다.
예상 복잡도
시간 O(n), 추가 공간 O(n)
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.