This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def merge(self, intervals: List[Interval]) -> List[Interval]: | |
| intervals.sort(key=lambda x: x.start) | |
| result = [] | |
| current_interval_index = 0 | |
| intervals_len = len(intervals) | |
| while current_interval_index < intervals_len: | |
| new_interval = intervals[current_interval_index] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def maximalSquare(matrix): | |
| if matrix is None or len(matrix)==0: | |
| return 0 | |
| max_sq = 0 | |
| height, width = len(matrix), len(matrix[0]) | |
| dp = [[0 for i in range(width+1)] for j in range(height+1)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import deque | |
| def rangeSumBST(root, L, R): | |
| s = 0 | |
| queue = deque([root]) | |
| while queue: | |
| c = queue.popleft() | |
| if c: | |
| if R<c.val: | |
| queue.append(c.left) | |
| elif c.val<L: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def rangeSumBST(root, L, R): | |
| def rangeSumBST_helper(node, L, R): | |
| if not node: | |
| return 0 | |
| if R<node.val: | |
| return rangeSumBST_helper(node.left, L, R) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def backspaceCompare( S, T): | |
| def normalize(s): | |
| stack = [] | |
| for c in s): | |
| if c!='#': | |
| stack.append(c) | |
| elif stack: | |
| stack.pop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def findPeakElement(nums): | |
| left , right = 0, len(nums)-1 | |
| while(left<right): | |
| middle = (left+right)//2 | |
| if nums[middle]<nums[middle+1]: | |
| left = middle + 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def non_conti(nums): | |
| if nums == []: | |
| return 0 | |
| if len(nums)==1: | |
| return nums[0] | |
| dp = [0] * (len(nums)+1) | |
| dp[1], dp[2] = nums[0], nums[1] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def compareVersion(version1, version2): | |
| p1 = map(int, version1.split(".")) | |
| p2 = map(int, version2.split(".")) | |
| d = len(p1)-len(p2) | |
| p1 += [0] * (-d) | |
| p2 += [0] * (d) | |
| for i in range(len(p1)): | |
| if p1[i] > p2[i]: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def singleNumber(nums): | |
| v = 0 | |
| for i in nums: | |
| v ^= i | |
| return v |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def singleNumber(nums): | |
| nums.sort() | |
| i = 0 | |
| n = len(nums) | |
| while i < n: | |
| if i == n-1 or nums[i]!=nums[i+1]: | |
| return nums[i] | |
| i += 2 |
NewerOlder