Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions FindAllNumbersDisappearedInAnArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Time Complexity :O(n)
// Space Complexity :O(n)
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this :no

class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> answer = new ArrayList<>();
HashSet<Integer> set = new HashSet<>();
for(int i=0;i<nums.length;i++){
set.add(nums[i]);
}
for(int i=1;i<=nums.length;i++){
if(!set.contains(i)) {
answer.add(i);
}
}
return answer;
}
}
47 changes: 47 additions & 0 deletions GameOfLife.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Time Complexity :O(m*n)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this :no

class Solution {
int[][] dirs;
int m,n;

public void gameOfLife(int[][] board) {

dirs = new int[][]{{-1,-1}, {-1,0}, {-1, 1}, {0, -1}, {0, 1}, {1,-1}, {1,0}, {1,1}};
m = board.length;
n = board[0].length;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
int count = count(board, i, j);
if(board[i][j] == 0 && count == 3){
board[i][j] = 3;
}else if(board[i][j] == 1 && (count < 2 || count >3)){
board[i][j] = 2;
}
}
}
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(board[i][j] == 2){
board[i][j] = 0;
}else if(board[i][j] == 3){
board[i][j] = 1;
}
}
}
}
private int count(int[][] board, int i, int j){
int count = 0;
for(int[] dir: dirs){
int r = i + dir[0];
int c = j + dir[1];
if(r>=0 && c>=0 && r<m && c<n){
if(board[r][c] == 1 || board[r][c] == 2) count++;
}
}
return count;
}
}