diff --git a/Problem 1- Leetcode80.py b/Problem 1- Leetcode80.py new file mode 100644 index 00000000..f5266a9b --- /dev/null +++ b/Problem 1- Leetcode80.py @@ -0,0 +1,18 @@ +#Time Complexity: O(n) +#Space Complexity: O(1) + +class Solution(object): + def removeDuplicates(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + slow, fast = 2, 2 + while fast < len(nums): + if nums[fast] != nums[slow - 2]: + nums[slow] = nums[fast] + slow += 1 + fast += 1 + return slow + + \ No newline at end of file diff --git a/Problem 2- Leetcode88.py b/Problem 2- Leetcode88.py new file mode 100644 index 00000000..1cd936db --- /dev/null +++ b/Problem 2- Leetcode88.py @@ -0,0 +1,28 @@ +#Time Complexity: O(n) +#Space Complexity: O(1) + +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, p2, idx = m - 1, n - 1, m + n - 1 + while p1 >= 0 and p2 >= 0: + if nums1[p1] > nums2[p2]: + nums1[idx] = nums1[p1] + p1 -= 1 + idx -= 1 + else: + nums1[idx] = nums2[p2] + p2 -= 1 + idx -= 1 + + while p2 >= 0: + nums1[idx] = nums2[p2] + p2 -= 1 + idx -= 1 + \ No newline at end of file diff --git a/Problem 3- Leetcode240.py b/Problem 3- Leetcode240.py new file mode 100644 index 00000000..3e5fc9f4 --- /dev/null +++ b/Problem 3- Leetcode240.py @@ -0,0 +1,20 @@ +#Time Complexity: O(m+n) +#Space Complexity: O(1) + +class Solution(object): + def searchMatrix(self, matrix, target): + """ + :type matrix: List[List[int]] + :type target: int + :rtype: bool + """ + m, n = len(matrix), len(matrix[0]) + r, c = 0, n - 1 + + while r < m and c >= 0: + if matrix[r][c] == target: return True + elif matrix[r][c] > target: c -= 1 + else: r += 1 + + return False + \ No newline at end of file