Jan 18 - Spiral Matrix; 2D Array;

代码:

public class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> resultList = new ArrayList<>();
        int row = matrix.length;
        if(row == 0) return resultList;
        int col = matrix[0].length;
        if(col == 0) return resultList;
        int n = row < col? row : col;

        for(int i = 0; i <= n/2; i++){
            int width = col - 1 - 2*i;
            int height = row - 1 - 2*i;
            if(width < 0 || height < 0) break;
            if(height == 0 && width == 0){
                resultList.add(matrix[i][i]);
                break;
            }
            if(height == 0) {
                for(int j = 0; j <= width; j++) resultList.add(matrix[i][i+j]);
                break;
            }
            if(width == 0){
                for(int j = 0; j <= height; j++) resultList.add(matrix[i+j][i]);
                break;
            }
            for(int j = 0; j < (width+height)*2; j++){
                if(j < width) resultList.add(matrix[i][i+j]);
                else if(j < width+height && j >= width) resultList.add(matrix[i+j-width][i+width]);
                else if(j < 2*width+height && j >= width+height) resultList.add(matrix[i+height][i+width-(j-width-height)]);
                else resultList.add(matrix[i+height-(j-2*width-height)][i]);
                /*
                int top_left = matrix[i][i];
                int top_right = matrix[i][i+num];
                int bottom_right = matrix[i+num][i+num];
                int bottom_left = matrix[i+num][i];
                */
            }
        }
        return resultList;
    }
}

  

时间: 2024-11-19 07:20:34

Jan 18 - Spiral Matrix; 2D Array;的相关文章

Jan 20 - Set Matrix Zeros; Array;

Use additional O(m+n) space, simple improvement. Two array, one is of size row, to store the status of each row, whether there is a 0 element in the row. Similarly, the other array of size col, storing the status of each column that whether there is

Jan 17 - Rotate Image; 2D Array; xPos and yPos;

代码: public class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for(int i = 0; i < n/2; i++){ int num = n-i*2; for(int j = 0; j < num-1; j++){ int temp = matrix[i][i+j]; matrix[i][i+j] = matrix[i+num-1-j][i]; matrix[i+num-1-j

sklearn中报错ValueError: Expected 2D array, got 1D array instead:

1 from sklearn.linear_model import LinearRegression 2 lr = LinearRegression() 3 print(tr_x.shape,tr_y.shape) 4 lr.fit(tr_x,tr_y) 5 6 7 # 报错 8 (64,) (64,) 9 Traceback (most recent call last): 10 File "F:/Python_Project/sklearn2_2/zong_fu_xi/A_02.py&qu

LeetCode[Array]: Spiral Matrix 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 ] ] 这个题目跟LeetCode[Array]: Spiral Matrix不同的是:这个题目并不

LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

Spiral Matrix IIGiven 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 ]] SOLUTION 1: 还是与上一题Spiral Matrix类似

Leetcode 54. Spiral Matrix &amp; 59. Spiral Matrix II

54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input:

Python与线性代数——Numpy中的matrix()和array()的区别

Numpy中matrix必须是2维的,但是 numpy中array可以是多维的(1D,2D,3D····ND).matrix是array的一个小的分支,包含于array.所以matrix 拥有array的所有特性. matrix() 和 array() 的区别,主要从以下方面说起: 矩阵生成方式不同 import numpy as np a1 = np.array([[1, 2], [3, 4]]) b1 = np.mat([[1, 2], [3, 4]]) a2 = np.array(([1,

Spiral Matrix(LintCode)

Spiral Matrix 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]. 难得的一次AC! 虽然感觉题

【leetcode】Spiral Matrix

Spiral Matrix 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]. 如果下一步会遇到访问