亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[LintCode] Spiral Matrix I & Spiral Matrix II

tuantuan / 2428人閱讀

摘要:如果不在前兩個(gè)循環(huán)之后的話,那么那多余的一行或一列就會(huì)被加入數(shù)組兩次,造成錯(cuò)誤的結(jié)果。解法和一樣,只是簡化了,甚至可以用一樣的方法去做,只要把也換成。使用,以及最后討論是否為奇數(shù)以判斷中間是否有一個(gè)未循環(huán)的點(diǎn),是這道題的兩個(gè)有趣的地方。

Spiral Matrix I Problem

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

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].

Note

解法很形象,先從左上角開始,從左向右添加第一行的元素,然后到了右上角,從最后一列從上到下,到了右下角,在最后一行從右到左,到了左下角,從第一列從下到上,如此循環(huán)。每一圈循環(huán)count加一,直到count到了最中間的一行或者最中間的一列,只需進(jìn)行從左到右或從上到下的操作,然后break。
為什么要break?這樣做的理由其實(shí)很簡單,不論行數(shù)m還是列數(shù)n更大,最后一個(gè)循環(huán)發(fā)生在count*2 == m or n的時(shí)候,此時(shí)一定只剩下一行或一列要走。也就是說,四個(gè)for循環(huán),只走一次。如果不在前兩個(gè)for循環(huán)之后break的話,那么那多余的一行或一列就會(huì)被加入res數(shù)組兩次,造成錯(cuò)誤的結(jié)果。
要注意,在第一次之后的for循環(huán)要避開之前遍歷過的點(diǎn)。

Solution
public class Solution {
    public List spiralOrder(int[][] matrix) {
        List res = new ArrayList ();
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return res;
        int m = matrix.length;
        int n = matrix[0].length;
        int count = 0;
        while (2*count < m && 2*count < n) {
            for (int i = count; i <= n-1-count; i++) {
                res.add(matrix[count][i]);
            }
            for (int i = count+1; i <= m-1-count; i++) {
                res.add(matrix[i][n-1-count]);
            }
            if (2*count+1 == m || 2*count+1 == n) break;
            for (int i = n-2-count; i >= count; i--) {
                res.add(matrix[m-1-count][i]);
            }
            for (int i = m-2-count; i >= count+1; i--) {
                res.add(matrix[i][count]);
            }
            count++;
        }
        return res;
    }
}
Spiral Matrix II Problem

Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order.

Example

Given n = 3,

You should return the following matrix:

[
  [ 1, 2, 3 ],
  [ 8, 9, 4 ],
  [ 7, 6, 5 ]
]
Note

解法和Spiral Matrix I一樣,只是簡化了,甚至可以用一樣的方法去做,只要把m也換成n。
使用num++,以及最后討論n是否為奇數(shù)以判斷中間是否有一個(gè)未循環(huán)的點(diǎn),是這道題的兩個(gè)有趣的地方。

Solution
public class Solution {
    public int[][] generateMatrix(int n) {
        int num = 1;
        int[][] res = new int[n][n];
        for (int cur = 0; cur < n/2; cur++) {
            for (int j = cur; j < n-1-cur; j++) {
                res[cur][j] = num++;
            }
            for (int i = cur; i < n-1-cur; i++) {
                res[i][n-1-cur] = num++;
            }
            for (int j = n-1-cur; j > cur; j--) {
                res[n-1-cur][j] = num++;
            }
            for (int i = n-1-cur; i > cur; i--) {
                res[i][cur] = num++;
            }
        }
        if (n % 2 != 0) res[n/2][n/2] = num;
        return res;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/65613.html

相關(guān)文章

  • leetcode59 Spiral Matrix II

    摘要:題目要求也就是將遞加的數(shù)字按照順時(shí)針的順序依次填入數(shù)組之中這道題目聯(lián)系到,其實(shí)就相當(dāng)好解決了。 題目要求 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...

    QLQ 評(píng)論0 收藏0
  • [Leetcode] Spiral Matrix 螺旋矩陣

    摘要:代碼添加該圈第一行添加最后一列添加最后一行添加第一列如果是奇數(shù),加上中間那個(gè)點(diǎn)后續(xù)如果在中,給出的是和來代表行數(shù)和列數(shù),該如何解決和的本質(zhì)區(qū)別就是一個(gè)是任意長方形,一個(gè)是正方形,所以中不需要判斷最后一行或者最后一列。 Spiral Matrix I Given a matrix of m x n elements (m rows, n columns), return all ele...

    waruqi 評(píng)論0 收藏0
  • LeetCode[54] Spiral Matrix

    摘要:復(fù)雜度思路注意循環(huán)條件。代碼注意循環(huán)條件,要用而不是除以,因?yàn)榫葴?zhǔn)換問題只有一行或者一列的時(shí)候,就不要再繼續(xù)搜索了 LeetCode[54] Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Fo...

    YFan 評(píng)論0 收藏0
  • Leetcode 54:Spiral Matrix 螺旋矩陣

    摘要:螺旋矩陣給定一個(gè)包含個(gè)元素的矩陣行列,請(qǐng)按照順時(shí)針螺旋順序,返回矩陣中的所有元素。每次轉(zhuǎn)向或都會(huì)自減。循環(huán)可操作性很高,可以直接操作索引坐標(biāo)改變遍歷方式,不再贅述。 54:Spiral Matrix 螺旋矩陣 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix i...

    venmos 評(píng)論0 收藏0
  • Leetcode 54:Spiral Matrix 螺旋矩陣

    摘要:螺旋矩陣給定一個(gè)包含個(gè)元素的矩陣行列,請(qǐng)按照順時(shí)針螺旋順序,返回矩陣中的所有元素。每次轉(zhuǎn)向或都會(huì)自減。循環(huán)可操作性很高,可以直接操作索引坐標(biāo)改變遍歷方式,不再贅述。 54:Spiral Matrix 螺旋矩陣 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix i...

    mochixuan 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<