- 浏览: 136988 次
文章分类
- 全部博客 (189)
- Tree (14)
- Dynamic Programming (34)
- Array (20)
- Search (1)
- Hash (12)
- Backtracking (22)
- Divide and Conque (8)
- Greedy (6)
- Stack (12)
- software (0)
- List (7)
- Math (22)
- Two pointers (16)
- String (20)
- Linux (1)
- Sliding Window (4)
- Finite State Machine (1)
- Breadth-first Search (7)
- Graph (4)
- DFS (6)
- BFS (3)
- Sort (9)
- 基础概念 (2)
- 沟通表达 (0)
- Heap (2)
- Binary Search (15)
- 小结 (1)
- Bit Manipulation (8)
- Union Find (4)
- Topological Sort (1)
- PriorityQueue (1)
- Design Pattern (1)
- Design (1)
- Iterator (1)
- Queue (1)
最新评论
-
likesky3:
看了数据结构书得知并不是迭代和递归的区别,yb君的写法的效果是 ...
Leetcode - Graph Valid Tree -
likesky3:
迭代和递归的区别吧~
Leetcode - Graph Valid Tree -
qb_2008:
还有一种find写法:int find(int p) { i ...
Leetcode - Graph Valid Tree -
qb_2008:
要看懂这些技巧的代码确实比较困难。我是这么看懂的:1. 明白这 ...
Leetcode - Single Num II -
qb_2008:
public int singleNumber2(int[] ...
Leetcode - Single Num II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
[分析]
实现2参考https://leetcode.com/discuss/14079/my-super-simple-solution-can-used-for-both-spiral-matrix-and,代码更简洁。
For example,
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
[分析]
实现2参考https://leetcode.com/discuss/14079/my-super-simple-solution-can-used-for-both-spiral-matrix-and,代码更简洁。
public class Solution { // Method 1 public int[][] generateMatrix1(int n) { if (n < 0) return null; int[][] board = new int[n][n]; int rowStart = 0, colStart = 0; int radius = n; int num = 1; while (radius > 0) { int i = rowStart, j = colStart; // move right int upRight = colStart + radius; while (j < upRight) { board[i][j++] = num++; } j--; if (radius == 1) break; // move down i++; int downRight = rowStart + radius; while (i < downRight) { board[i++][j] = num++; } i--; // move left j--; while (j >= colStart) { board[i][j--] = num; num++; } j++; // move up --i; while (i > rowStart) { board[i--][j] = num; num++; } // update info to start next spiral rowStart++; colStart++; radius -= 2; } return board; } // Method 2 public int[][] generateMatrix(int n) { if (n < 0) return null; int[][] matrix = new int[n][n]; int rowStart = 0, rowEnd = n - 1; int colStart = 0, colEnd = n - 1; int num = 1; while (rowStart <= rowEnd) { for (int j = colStart; j <= colEnd; j++) {//move right matrix[rowStart][j] = num++; } rowStart++; for (int i = rowStart; i <= rowEnd; i++) {//move down matrix[i][colEnd] = num++; } colEnd--; if (rowEnd >= rowStart) { for (int j = colEnd; j >= colStart; j--) {//move left matrix[rowEnd][j] = num++; } rowEnd--; } if (colEnd >= colStart) { for (int i = rowEnd; i >= rowStart; i--) {//move up matrix[i][colStart] = num++; } colStart++; } } return matrix; } }
发表评论
-
Leetcode - H-Index
2015-09-06 09:08 1118Given an array of citations (ea ... -
Leetcode - Add Binary
2015-08-21 09:28 467[分析] 从低位往高位逐位相加,就是这么一个简单的题却花了我一 ... -
Leetcode - Longest Consecutive Sequence
2015-08-20 21:20 485[分析] base version说几句: 数组题一定要考虑重 ... -
Leetcode - First Missing Positive
2015-08-20 07:45 652[分析] 将各个正数放入相应下标处,使之满足nums[nums ... -
Leetcode - Set Matrix Zeros
2015-08-19 20:55 548Given a m x n matrix, if an ele ... -
Leetcode - Rotate Image
2015-08-19 19:51 497[分析] 自己的思路:从外到内一圈圈顺时针旋转90度,坐标映射 ... -
Missing Ranges
2015-08-19 09:48 513[分析] 此题若不考虑极大值极小值相关的corner case ... -
Leetcode - 3Sum Smaller
2015-08-18 22:12 1484Given an array of n integers nu ... -
Leetcode - Spiral Matrix
2015-08-18 09:50 426Given a matrix of m x n element ... -
Leetcode - Contains Duplicate II
2015-08-18 07:57 557Given an array of integers and ... -
Leetcode - Shortest Word Distance II
2015-08-18 07:25 1359This is a follow up of Shortest ... -
Leetcode - Shortest Word Distance III
2015-08-17 22:05 1663This is a follow up of Shortest ... -
Leetcode - Shortest Word Distance
2015-08-17 22:01 917Given a list of words and two w ... -
Leetcode - Insert Interval
2015-08-14 10:12 626[分析] 这道题思路直接,但bug free需要费些心力。第一 ... -
Leetcode - Valid Sudoku
2015-07-30 21:57 471Determine if a Sudoku is valid, ... -
Leetcode -Rotate Array
2015-07-21 08:16 541Rotate an array of n elements t ... -
Leetcode - House Robber II
2015-05-20 22:34 767Note: This is an extension of ... -
Leetcode - Largest Rectangle in Histogram
2015-05-20 07:53 491Given n non-negative integers ... -
MockInterview-IPRangeData Find&Insert
2015-04-12 08:09 0[题目] Finding the IP address w ... -
Leetcode - MissingRange
2015-03-31 09:25 449Given a sorted integer array ...
相关推荐
js js_leetcode题解之59-spiral-matrix-II.js
js js_leetcode题解之54-spiral-matrix.js
- Spiral Matrix: 给定一个m×n矩阵,以螺旋方式遍历矩阵中的所有元素一次,并且只遍历一次。 - Merge Intervals: 给定一组区间,请合并所有重叠的区间。 - Insert Interval: 在一组已经排序的区间中,插入一个新的...
"Spiral Matrix"则需要理解矩阵旋转的数学原理。 5. **递归与函数**:递归是解决许多算法问题的有效手段,如"Binary Tree Level Order Traversal"使用层次遍历(广度优先搜索)求解。而"Factorial Trailing Zeroes...
Spiral Matrix com.leetcode.list Linked List Cycle Linked List Cycle II Remove Duplicates from Sorted List com.leetcode.string Single Number com.leetcode.tree Balanced Binary Tree Maximum Depth of ...
多线程 leetcode 前言 ...Spiral Matrix Path Sum II Copy List with Random Pointer Building H2O Fizz Buzz Multithreaded hard Merge k Sorted Lists Reverse Nodes in k-Group Trapping Rain Water
leetcode LeetCode Solutions 算法 - Algorithms 排序算法:快速排序、归并排序、计数排序 搜索算法:回溯、递归、剪枝技巧 图论:最短路、最小生成树、网络流建模 动态规划:背包问题、最长子序列、计数问题 基础...
leetcode 答案螺旋矩阵 返回二维数组中整数元素的顺时针螺旋列表 [答案击败 100% Java LeetCode 运行时提交] [答案击败 100% Java LeetCode 内存使用提交] 大(O)= O(N)
leetcode category other hot keywords:Palindrome(mic), Subsequence Array 螺旋矩阵Spiral Matrix 顺时针打印矩阵 Next Permutation Product of Array Except Self 189.rotate-array 283.move-zero Range Sum ...
其中,问题59,名为"Spiral Matrix II"(螺旋矩阵II),是中级难度的一个挑战,主要考察的是矩阵遍历的技巧。该问题的核心目标是生成一个给定大小的螺旋矩阵,从中心开始,以螺旋形状填充数字。 首先,让我们明确...
Spiral Matrix medium O 66 Plus One easy O O 118 Pascal's Triangle easy O O 119 Pascal's Triangle II easy O 要满足只用一个array大小空间O(k) k为input大小来完成,须具备backtracking概念 151 Reverse Words ...
137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [C++](./C++/single-number-ii.cpp) [Python](./Python/single-number-ii.py) | _O(n)_ | _O(1)_ | Medium ||| 190 | [Reverse Bits]...
第 338 章leetcode_cpp leetcode 的 C++ 代码 包含的问题 1 两和容易2 加两个数中5 最长回文子串中6 ZigZag 转换介质7 反转整数简单8 ...II ...Spiral Matrix II 培养基61 轮播列表中第62话第63话64 最小路径和中66
第59题"螺旋矩阵II"(Spiral Matrix II)是LeetCode中的一个经典问题,它涉及到矩阵操作和迭代。在这个问题中,我们需要生成一个特定大小的螺旋矩阵,从中心向外螺旋式填充数字。 螺旋矩阵是一种特殊的二维数组,其...
leetcode 530 力码 全部的: 易(173/237+x) 中(144/437+x) 硬(4/x) 问题 1.Two Sum(dict) 7.(跳过)(数学) 9.(跳过)(串串技巧) 11.盛水最多的容器 12.(跳过)(问题不好) 13.(跳过)(蛮力) 14.(跳过)...
标题中的“python-leetcode面试题解之第54题螺旋矩阵-题解.zip”表明这是一个关于Python编程语言的LeetCode面试题解答,具体是针对第54题——螺旋矩阵(Spiral Matrix)的解题代码和分析。LeetCode是一个在线平台,...
function spiralOrder($matrix) { if (empty($matrix)) return []; $result = []; $left = 0; $right = count($matrix[0]) - 1; $top = 0; $bottom = count($matrix) - 1; while ($left $right && $top $...
在本压缩包中,主题聚焦于C++编程基础与LeetCode题目的结合,特别是针对第54题“螺旋矩阵”(Spiral Matrix)的解法。LeetCode是一个在线平台,提供了一系列编程挑战,旨在帮助程序员提升算法技能和解决实际问题的...
\n\n以下是一个简单的C语言代码示例,展示了如何生成螺旋矩阵:\n```c\n#include <stdio.h>\n\nvoid spiralOrder(int m, int n, int* matrix, int* nums) {\n int i, j, left = 0, right = m - 1, top = 0, bottom =...
35. Spiral Matrix:螺旋遍历矩阵。 36. Integer to Roman:整数转换成罗马数字。 37. Roman to Integer:罗马数字转换成整数。 38. Clone Graph:深度复制一个图。 【栈】 39. Min Stack:设计一个栈,支持 push、...