Skip to content
Open

Array-2 #1842

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
14 changes: 14 additions & 0 deletions Find all numbers disappeared in Array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
n=len(nums)
result=[]
for i in range(len(nums)):
if nums[abs(nums[i])-1]>0:
nums[abs(nums[i])-1]=nums[abs(nums[i])-1]*-1

for i in range(len(nums)):
if nums[i]>0:
result.append(i+1)
else:
nums[i]=nums[i]*-1
return result
39 changes: 39 additions & 0 deletions Game of Life.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m=len(board)
n=len(board[0])

#1-0 ->2
#0-1-> 3
for i in range(m):
for j in range(n):
countn=self.neighbourCount(board, i , j)
if board[i][j]==1:
if countn<2 or countn>3:
board[i][j]=2

else:
if countn==3:
board[i][j]=3
for i in range(m):
for j in range(n):
if board[i][j]==2:
board[i][j]=0
elif board[i][j]==3:
board[i][j]=1
def neighbourCount( self,board: List[List[int]], r: int, c:int)->int:
count=0
m=len(board)
n=len(board[0])

dirr=[[-1,-1],[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1],[0,-1]]
for dir in dirr:
nr=r+dir[0]
nc=c+dir[1]

if nr>=0 and nc>=0 and nr<m and nc<n and (board[nr][nc]==1 or board[nr][nc]==2):
count=count+1
return count