Lecture 05 - Array Part 2

2 min read

Array
& → Address of
eg - & Myarray[0] = Myarray = 100
* → Value at
eg - * (Myarray) = 5
* (Myarray + 1) = 10
Let ‘a’ be the name of the array -
*a = *(a+0) = a[0] = 5;
*(a+2) = a[2] = 15;
(a+i) = Base address + i * (size of datatype)
a + i = i + a
a[i]= *(a+i) = *(i+a) = i[a]
2-D Array
int a [m] [n];
- ( a [i] [j] ) = *(a[i] + j ) = *(*(a+i) + j )
Examples
Leetcode 463 - island Parameter
#include <iostream>
#include <vector>
using namespace std;
int islandPerimeter(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
int p=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]==1){
if(i == 0 || grid[i-1][j] == 0) p++;
if(i == m-1 || grid[i+1][j] == 0) p++;
if(j == 0 || grid[i][j-1] == 0) p++;
if(j == n-1 || grid[i][j+1] == 0) p++;
}
}
}
return p;
}
int main(){
vector<vector<int>> grid = {{0,1,0,0},{1,1,1,0},{0,1,0,0}};
int p = islandPerimeter(grid);
cout << p;
return 0;
}
// this took very much time but i get it now (i was getting consufed on fundamentals)
// i was assuming that left if a[i-1][j] but it is top
// this was for all the directions
0
Subscribe to my newsletter
Read articles from Akash Jaiswal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
