1424. Diagonal Traverse II
Văn bản bài toán được dịch từ tiếng Nga theo ngôn ngữ giao diện. Mã không thay đổi.
Дан двумерный số nguyên mảng nums, return все elementы nums в диагональном порядке.
Ví dụ:
Input: nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
Output: [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]
C# lời giải
đã khớp/gốcpublic class Solution {
public int[] FindDiagonalOrder(IList<IList<int>> nums) {
var queue = new Queue<(int row, int col)>();
queue.Enqueue((0, 0));
var ans = new List<int>();
while (queue.Count > 0) {
var (row, col) = queue.Dequeue();
ans.Add(nums[row][col]);
if (col == 0 && row + 1 < nums.Count) {
queue.Enqueue((row + 1, col));
}
if (col + 1 < nums[row].Count) {
queue.Enqueue((row, col + 1));
}
}
return ans.ToArray();
}
}
C++ lời giải
bản nháp tự động, xem lại trước khi gửi#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
public vector<int>& FindDiagonalOrder(IList<vector<int>> nums) {
var queue = new queue<(int row, int col)>();
queue.Enqueue((0, 0));
var ans = new List<int>();
while (queue.size() > 0) {
var (row, col) = queue.Dequeue();
ans.push_back(nums[row][col]);
if (col == 0 && row + 1 < nums.size()) {
queue.Enqueue((row + 1, col));
}
if (col + 1 < nums[row].size()) {
queue.Enqueue((row, col + 1));
}
}
return ans.ToArray();
}
}
Java lời giải
đã khớp/gốcclass Solution {
public int[] findDiagonalOrder(List<List<Integer>> nums) {
Queue<Pair<Integer, Integer>> queue = new LinkedList();
queue.offer(new Pair(0, 0));
List<Integer> ans = new ArrayList();
while (!queue.isEmpty()) {
Pair<Integer, Integer> p = queue.poll();
int row = p.getKey();
int col = p.getValue();
ans.add(nums.get(row).get(col));
if (col == 0 && row + 1 < nums.size()) {
queue.offer(new Pair(row + 1, col));
}
if (col + 1 < nums.get(row).size()) {
queue.offer(new Pair(row, col + 1));
}
}
int[] result = new int[ans.size()];
int i = 0;
for (int num : ans) {
result[i] = num;
i++;
}
return result;
}
}
JavaScript lời giải
đã khớp/gốcclass Solution {
findDiagonalOrder(nums) {
const queue = [[0, 0]];
const ans = [];
while (queue.length > 0) {
const [row, col] = queue.shift();
ans.push(nums[row][col]);
if (col === 0 && row + 1 < nums.length) {
queue.push([row + 1, col]);
}
if (col + 1 < nums[row].length) {
queue.push([row, col + 1]);
}
}
return ans;
}
}
Python lời giải
đã khớp/gốcfrom collections import deque
class Solution:
def findDiagonalOrder(self, nums: List[List[int]]) -> List[int]:
queue = deque([(0, 0)])
ans = []
while queue:
row, col = queue.popleft()
ans.append(nums[row][col])
if col == 0 and row + 1 < len(nums):
queue.append((row + 1, col))
if col + 1 < len(nums[row]):
queue.append((row, col + 1))
return ans
Go lời giải
đã khớp/gốcfunc findDiagonalOrder(nums [][]int) []int {
queue := []struct{ row, col int }{{0, 0}}
ans := []int{}
for len(queue) > 0 {
row, col := queue[0].row, queue[0].col
queue = queue[1:]
ans = append(ans, nums[row][col])
if col == 0 && row + 1 < len(nums) {
queue = append(queue, struct{ row, col int }{row + 1, col})
}
if col + 1 < len(nums[row]) {
queue = append(queue, struct{ row, col int }{row, col + 1})
}
}
return ans
}
Algorithm
Инициализируйте очередь с (0, 0) и список ответов ans.
Пока очередь не пуста:
Извлеките (row, col) из очереди.
Добавьте nums[row][col] в ans.
Если col == 0 и row + 1 в пределах mảngа, добавьте (row + 1, col) в очередь.
Если col + 1 в пределах текущей строки, добавьте (row, col + 1) в очередь.
return ans.
😎
Vacancies for this task
việc làm đang hoạt động with overlapping task tags are đã hiển thị.
Chưa có việc làm đang hoạt động.