Skip to content

Instantly share code, notes, and snippets.

@claytonjwong
Last active May 6, 2022 22:51
Show Gist options
  • Save claytonjwong/59b9cd1dd0d51d389f1788e47298e210 to your computer and use it in GitHub Desktop.
Save claytonjwong/59b9cd1dd0d51d389f1788e47298e210 to your computer and use it in GitHub Desktop.
2-dimensional arrays

2-Dimensional Arrays

How to create and initialize 2D arrays of integers with:

  • M rows
  • N columns
  • X initial value

C

int dp[M][N] = { [0 ... M-1] = { [0 ... N-1] = X } };

C++

vector<vector<int>> dp(M, vector<int>(N, X));
using VI = vector<int>;
using VVI = vector<VI>;
VVI dp(M, VI(N, X));

Javascript

let dp = Array(M).fill().map(() => Array(N).fill(X));
let dp = [...Array(M)].map(() => Array(N).fill(X));

Python

dp = [[X for j in range(N)] for i in range(M)]

Kotlin

var dp = Array(M){ Array(N){ X } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment