- 浏览: 173214 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
给定一个m*n个矩阵,按照顺时针的方向,输出矩阵中的元素。设定四个变量,top, bottom, left, right, 从第一行第一个元素开始,想添加最顶上一行的元素,然后让top加1; 然后添加右边的一列元素,让right减1;接下来添加最下面一行的元素,让bottom减1;最后添加最左边一列,然后让left加1,如果出现left > right 或者top > bottom时就停止。代码如下:
For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
给定一个m*n个矩阵,按照顺时针的方向,输出矩阵中的元素。设定四个变量,top, bottom, left, right, 从第一行第一个元素开始,想添加最顶上一行的元素,然后让top加1; 然后添加右边的一列元素,让right减1;接下来添加最下面一行的元素,让bottom减1;最后添加最左边一列,然后让left加1,如果出现left > right 或者top > bottom时就停止。代码如下:
public class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> list = new ArrayList<Integer>(); if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return list; int left = 0; int right = matrix[0].length - 1; int top = 0; int bottom = matrix.length - 1; while(left <= right && top <= bottom) { // 添加顶上的元素 if(left <= right && top <= bottom) for(int i = left; i <= right; i++) { list.add(matrix[top][i]); } top ++; // 添加右边的元素 if(left <= right && top <= bottom) for(int j = top; j <= bottom; j++) { list.add(matrix[j][right]); } right --; //添加底部的元素 if(left <= right && top <= bottom) for(int m = right; m >= left; m--) { list.add(matrix[bottom][m]); } bottom --; // 添加左边的元素 if(left <= right && top <= bottom) for(int n = bottom; n >= top; n--) { list.add(matrix[n][left]); } left ++; } return list; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 224Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 223You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 339Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 330Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 448Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 508Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 427Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 617Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 426The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 384Given an unsorted integer array ... -
Trapping Rain Water
2016-03-04 02:54 528Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 366All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 858Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 879Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 552Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 599Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 761Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 732You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 626For a undirected graph with tre ... -
Bulb Switcher
2016-02-28 12:12 347There are n bulbs that are init ...
相关推荐
Spiral Matrix II ZigZag Conversion Divide Two Integers Text Justification Max Points on a Line Community QQ 群: 237669375 Github: https://www.github.com/soulmachine/algorithm-essentials 微博: @...
螺旋矩阵的输出 C++实现源程序 Spiral matrix output C++ achieve source
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...
螺旋矩阵Spiral Matrix 顺时针打印矩阵 Next Permutation Product of Array Except Self 189.rotate-array 283.move-zero Range Sum Query - Immutables 66.Plus One 快手-跳格子 Intersection of Two Arrays 17.10....
Done for programming class. Generates a spiral matrix.
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 ...
第 338 章leetcode_cpp leetcode 的 C++ 代码 包含的问题 1 两和容易2 加两个数中5 最长回文子串中6 ZigZag 转换介质7 反转整数简单8 ...Spiral Matrix II 培养基61 轮播列表中第62话第63话64 最小路径和中66
leetcode 530 力码 全部的: 易(173/237+x) 中(144/437+x) 硬(4/x) 问题 1.Two Sum(dict) 7.(跳过)(数学) 9.(跳过)(串串技巧...59.Spiral Matrix 2(蛮力) 62.独特的路径 63/62.Unique Paths 2 64.最小路径和
螺旋矩阵螺旋矩阵项目介绍从矩阵的中间开始,按顺时针方向向右...要求必需的阿帕奇Maven Java JDK 7建造mvn clean install package && cd target && java -jar spin-matrix-1.0.jar && cd ..常问问题执照 Copyright
螺旋矩阵给你一个正整数n,生成一个包含1到n2所有元素,且元素按顺时针顺序螺旋排列的nxn正方形矩阵。
java lru leetcode LeetCode 记录数据结构与算法/LeetCode练习过程,将...Spiral Matrix Mergesort [Algorithm Swap](Mergesort/Algorithm swap.md) Numbers Queue Stack Toposort Trie Tree Two Pointers Union Find
对于每一道算法题会总结代码、时间复杂度以及一些好的blog排序(sort)...Spiral Matrix IILeetCode 53 Maximum SubarrayLeetCode 152 Maximum Product SubarrayLintCode 138 Subarray SumLintCode 139 Subarray Sum ...
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
任务 : 编写一个程序,该程序以顺时针螺旋顺序打印/返回2D矩阵的元素。抽象的: 就性能而言,编写短代码并不总是意味着它是高质量的代码。 同样,编写更长的代码并不意味着它是不好的代码! Numpy数组由于其同质...
leetcode 答案螺旋矩阵 返回二维数组中整数元素的顺时针螺旋列表 [答案击败 100% Java LeetCode 运行时提交] [答案击败 100% Java LeetCode 内存使用提交] 大(O)= O(N)
java lru leetcode LeetCode Solutions 算法 - Algorithms 排序算法:快速排序、归并排序、计数排序 搜索算法:回溯、递归、剪枝技巧 图论:最短路、最小生成树、网络流建模 动态规划:背包问题、最长子序列、计数...
兼容性------------- Matlab 7.2 及更高版本笔记----------- 此函数利用函数 matrix_perimeter.m 例子------------ >> A=魔法(5) 一 = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 >> ...
这是我准备面试时建议的个人问题清单。...https://leetcode.com/problems/spiral-matrix/ https://leetcode.com/problems/search-a-2d-matrix/ https://leetcode.com/problems/set-matrix-zeroes/ 弦乐 ...
def spiralOrder(self, matrix: List[List[int]]) -> List[int]: if not matrix:return [] m,n = len(matrix),len(matrix[0]) x = y = di = 0 dx = [0,1,0,-1] dy = [1,0,-1,0] res = [] visite