This file contains 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 sum_of_squares(self, n): | |
result = 0 | |
while n > 0: | |
digit = n % 10 | |
n = n // 10 | |
result += digit ** 2 | |
return result | |
def is_happy(self, n): |
This file contains 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 ListNode(object): | |
def __init__(self, x): | |
self.val = x | |
self.next = None | |
class Solution(object): | |
def has_cycle(self, head): | |
""" | |
:type head: ListNode |
This file contains 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(object): | |
def __init__(self, val=0, left=None, right=None): | |
self.val = val | |
self.left = left | |
self.right = right | |
class Solution(object): | |
def is_same_tree(self, p, q): | |
""" |
This file contains 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 climb_stairs(self, n): | |
""" | |
:type n: int | |
:rtype: int | |
""" | |
if n == 1: | |
return 1 | |
results = [0] * n |
This file contains 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 plus_one(self, digits): | |
""" | |
:type digits: List[int] | |
:rtype: List[int] | |
""" | |
carry = 0 | |
index = 0 | |
added = False | |
digits = digits[::-1] |
This file contains 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 max_subarray(self, nums): | |
""" | |
:type nums: List[int] | |
:rtype: int | |
""" | |
max_sum = nums[0] | |
curr_sum = 0 | |
for num in nums: |
This file contains 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 top_k_frequent(self, nums, k): | |
""" | |
:type nums: List[int] | |
:type k: int | |
:rtype: List[int] | |
""" | |
num_count = {} | |
list_of_counts = [[] for i in range(len(nums) + 1)] | |
results = [] |
This file contains 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 longest_consecutive(self, nums): | |
""" | |
:type nums: List[int] | |
:rtype: int | |
""" | |
longest_sequence = 0 | |
set_of_nums = set(nums) | |
for num in nums: |
This file contains 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 search(self, nums, target): | |
""" | |
:type nums: List[int] | |
:type target: int | |
:rtype: int | |
""" | |
left_pointer = 0 | |
right_pointer = len(nums) - 1 |
This file contains 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(object): | |
def __init__(self, val=0, left=None, right=None): | |
self.val = val | |
self.left = left | |
self.right = right | |
class Solution(object): | |
def invert_tree(self, root): | |
""" |
NewerOlder