"Leetcode-Interview algorithm classic-java implementation" "054-spiral Matrix (Spiral matrix)"

Source: Internet
Author: User

054-spiral matrix (spiral matrix) "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

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:

123456789 ]]

You should return [1,2,3,6,9,8,7,4,5] .

Main Topic

Given a m*n matrix, enter the spiral order of all the elements.

Thinking of solving problems

Using the computed output method, the first line is processed, the right column is processed, the following line is processed, and the left column is processed until all the elements have been processed.

Code Implementation

Algorithm implementation class

Import Java.util.arraylist;import Java.util.list;public class Solution {public list<integer> Spiralorder (int[][]Matrix) {list<integer> result = new Arraylist<> ( -);if(Matrix= = NULL | |Matrix. length = =0||Matrix[0].length = =0) {returnResult }//Only one row in the case        if(Matrix. length = =1) { for(intIMatrix[0]) {result.add (i); }returnResult }//Only one column of the situation        if(Matrix[0].length = =1) { for(inti =0; I <Matrix. length; i++) {Result.add (MatrixI [0]); }returnResult }//Calculate how many laps        introw =Matrix. length;intCol =Matrix[0].length;intCycle = row < col?        Row:col; Cycle = (cycle +1) /2;intRound =0;//record is currently the first few laps        intleft =0;intright =Matrix[0].length-1;inttop =0;intDown =Matrix. Length-1;intTotal = col*row;intCount =0; while(Round < cycle) {//Top row             for(inti = left; I <= right && count < total;                i++) {count++; Result.add (Matrix[Round]            [i]); } top++;//            //Right column             for(inti = top; I <= && count < total;                i++) {count++; Result.add (MatrixI [Col-round-1]); } right--;//Bottom row             for(inti = right; I >= left && count < total;                i--) {count++; Result.add (Matrix[Row-round-1][i]); } down--;//left column             for(inti = down; I >= top && count < total;                i--) {count++; Result.add (MatrixI            [round]);            } left++;        round++; }returnResult }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47120491"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java implementation" "054-spiral Matrix (Spiral matrix)"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.