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 find_number(arr, k): | |
| arr_set = set(arr) # O(N) | |
| for ele in arr: # O(N) | |
| if (k-ele) in arr_set: | |
| return True | |
| return False | |
| print(find_number([1,2,3,4,10,123,12], 14)) # True 12 + 2 = 14 | |
| print(find_number([1,2,3,4,10,123], 1114)) # False |
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 Point: | |
| def __init__(self, x=0, y=0): | |
| self.x = x | |
| self.y = y | |
| def __repr__(self): | |
| return f"({self.x}, {self.y})" | |
| def closest_points(points, k, p): | |
| pts = {} |
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 TreeNode: | |
| def __init__(self, val=0, left=None, right=None): | |
| self.val = val | |
| self.left = left | |
| self.right = right | |
| def inorder(node, visited={}): | |
| if node is not None: | |
| s = "(" + inorder(node.left, visited) + str(node.val) + inorder(node.right, visited) + ")" |
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 sortColors(self, nums): | |
| s = 0 | |
| for i in range(0, len(nums)): | |
| if nums[i] != 0: | |
| s = i | |
| break | |
| for i in range(s, len(nums)): | |
| if nums[i] == 0 and i!=s: | |
| tmp = nums[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
| class Node: | |
| def __init__(self, val=0, left=None, right=None): | |
| self.val = val | |
| self.left = left | |
| self.right = right | |
| def inorder(node): | |
| if node: | |
| inorder(node.left) |
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 Node(object): | |
| def __init__(self, val, next=None): | |
| self.val = val | |
| self.next = next | |
| def __str__(self): | |
| if not self.next: | |
| return str(self.val) | |
| return str(self.val) + " " + str(self.next) | |
| class Solution(object): |
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
| # Definition for a binary tree node. | |
| # class TreeNode: | |
| # def __init__(self, val=0, left=None, right=None): | |
| # self.val = val | |
| # self.left = left | |
| # self.right = right | |
| class Solution: | |
| def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: | |
| if len(nums) ==1 : | |
| return TreeNode(nums[0]) |
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(object): | |
| def findDuplicates(self, nums): | |
| for i in range(0, len(nums)): | |
| tmp = nums[i] % len(nums) | |
| nums[tmp] = nums[tmp] + len(nums) | |
| res = [] | |
| for i in range(0, len(nums)): | |
| if nums[i] >= (2 * len(nums)): | |
| res.append(i) | |
| return res |
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 addDigits(n): | |
| if n > 10: | |
| return addDigits(addDigits(n//10) + n%10) | |
| else: | |
| return n | |
| print(addDigits(159)) | |
| # 1 + 5 + 9 = 15 | |
| # 1 + 5 = 6 | |
| # 6 |
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 checkPerfectNumber(self, num: int) -> bool: | |
| import math | |
| sum = 1 | |
| if num == 1: | |
| return False | |
| for i in range(2, int(math.sqrt(num)) + 1 ): | |
| if num % i == 0: | |
| sum = sum + i + num // i | |
| return sum == num |
NewerOlder