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
25 changes: 25 additions & 0 deletions mergesorted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# // Time Complexity : O(m+n)
# // Space Complexity : O(1)
# // Did this code successfully run on Leetcode : Yes

class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: None Do not return anything, modify nums1 in-place instead.
"""
p1=m-1
p2=n-1

for i in range(m+n-1,-1,-1):
if p2<0:
break
if p1>=0 and nums1[p1]>nums2[p2]:
nums1[i]=nums1[p1]
p1-=1
else:
nums1[i]=nums2[p2]
p2-=1
28 changes: 28 additions & 0 deletions removeduplicates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# // Time Complexity : O(n)
# // Space Complexity : O(1)
# // Did this code successfully run on Leetcode : Yes

class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
p1=0
p2=1
count=1
while (p2<len(nums)):
if nums[p2]==nums[p1]:
if count>=2:
p2+=1
continue
else:
count+=1
else:
count=1
p1+=1
nums[p1]=nums[p2]
p2+=1
return p1+1


29 changes: 29 additions & 0 deletions search2dmatrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# // Time Complexity : O(m+n)
# // Space Complexity : O(1)
# // Did this code successfully run on Leetcode : Yes

class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
m=len(matrix)
n=len(matrix[0])

diag = max(m,n)
row=0
col=n-1
while 0<=row<m and 0<=col<n:
if target<matrix[row][col]:
col-=1
elif target>matrix[row][col]:
row+=1
else:
return True

return False