【LeetCode-面試演算法經典-Java實現】【054-Spiral Matrix(螺旋矩陣)】,leetcode--java

來源:互聯網
上載者:User

【LeetCode-面試演算法經典-Java實現】【054-Spiral Matrix(螺旋矩陣)】,leetcode--java
【054-Spiral Matrix(螺旋矩陣)】【LeetCode-面試演算法經典-Java實現】【所有題目目錄索引】原題

  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的矩陣,輸入所有元素的螺旋順序。

解題思路

  使用計算輸出的方法,先處理上面一行,再處理右邊一列,再處理下面一行,再處理左邊一列,一直這樣操作,直到所有的元素都處理完。

代碼實現

演算法實作類別

import java.util.ArrayList;import java.util.List;public class Solution {    public List<Integer> spiralOrder(int[][] matrix) {        List<Integer> result = new ArrayList<>(50);        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {            return result;        }        // 只有一行的情況        if (matrix.length == 1) {            for (int i : matrix[0]) {                result.add(i);            }            return result;        }        // 只有一列的情況        if (matrix[0].length == 1) {            for (int i = 0; i < matrix.length; i++) {                result.add(matrix[i][0]);            }            return result;        }        // 計算有多少圈        int row = matrix.length;        int col = matrix[0].length;        int cycle = row < col ? row : col;        cycle = (cycle + 1) / 2;        int round = 0; // 記錄當前是第幾圈        int left = 0;        int right = matrix[0].length - 1;        int top = 0;        int down = matrix.length - 1;        int total = col*row;        int count = 0;        while (round < cycle) {            // 上面一行            for (int i = left; i <= right && count < total; i++) {                count++;                result.add(matrix[round][i]);            }            top++; //            // 右邊一列            for (int i = top; i <= down && count < total; i++) {                count++;                result.add(matrix[i][col - round - 1]);            }            right--;            // 底下一行            for (int i = right; i >= left && count < total; i--) {                count++;                result.add(matrix[row - round - 1][i]);            }            down--;            // 左邊一列            for (int i = down; i >= top && count < total; i--) {                count++;                result.add(matrix[i][round]);            }            left++;            round++;        }        return result;    }}
評測結果

  點擊圖片,滑鼠不釋放,拖動一段位置,釋放後在新的視窗中查看完整圖片。

特別說明 歡迎轉載,轉載請註明出處【http://blog.csdn.net/derrantcm/article/details/47120491】

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.