0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

566 解答

Posted at

参考:
Nick White

解答

	public int[][] matrixReshape(int[][] mat, int r, int c) {

		int m = mat.length;
		int n = mat[0].length;

		if (m * n != r * c)
			return mat;// もし引数のサイズと出力するサイズが合わないとき

		int[][] ans = new int[r][c];
		int row_ans = 0; // 出力用の行
		int col_ans = 0; // 出力用の列
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				ans[row_ans][col_ans] = mat[i][j];

				col_ans++;

				if (col_ans == c) { //ココがやややこしい
					col_ans = 0;
					row_ans++;
				}

			}

		}

		return ans;

	}

Array の出力方法

1D Array

System.out.println(Arrays.toString(1DArray));

2D Array

System.out.println(Arrays.deepToString(2DArray));
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?