и посмотреть медиа
Java | LeetCode
и посмотреть медиа
LeetCode tasks and solutions in Java.
AI bot for text-to-speech and voice cloning in Telegram. Create audio using neural network quickly and easily.
LeetCode task solutions in Java with emphasis on clean code and optimization. Topics: arrays, trees, graphs, dynamic programming. Ideal for technical preparation.
Задача: 1424. Diagonal Traverse II Сложность: medium Дан двумерный целочисленный массив nums, верните все элементы nums в диагональном порядке. Пример: 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] 👨💻 Алгоритм: 1⃣Инициализируйте очередь с (0, 0) и список ответов ans. 2⃣Пока очередь не пуста: Извлеките (row, col) из очереди. Добавьте nums[row][col] в ans. Если col == 0 и row + 1 в пределах массива, добавьте (row + 1, col) в очередь. Если col + 1 в пределах текущей строки, добавьте (row, col + 1) в очередь. 3⃣Верните ans. 😎 Решение: class Solution { public int[] findDiagonalOrder(List • > nums) { Queue queue = new LinkedList(); queue.offer(new Pair(0, 0)); List ans = new ArrayList(); while (!queue.isEmpty()) { Pair 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; } } Ставь 👍 и забирай 📚 Базу знаний
Открыть канал и посмотреть медиаOnly registered users can share their opinion.
Be the first to share your impression of this resource!