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 maxDistance(self, arrays): | |
""" | |
:type arrays: List[List[int]] | |
:rtype: int | |
""" | |
# min and max store the min and max value in previous arrays | |
_min = arrays[0][0] | |
_max = arrays[0][-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
class Solution(object): | |
def hIndex(self, citations): | |
""" | |
:type citations: List[int] | |
:rtype: int | |
""" | |
# sorting the citations in ascending order | |
citations = sorted(citations) | |
# finding h-index by linear search |
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 merge(self, nums1, m, nums2, n): | |
""" | |
:type nums1: List[int] | |
:type m: int | |
:type nums2: List[int] | |
:type n: int | |
:rtype: void Do not return anything, modify nums1 in-place instead. | |
""" | |
while m > 0 and n > 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 findKthLargest(self, nums, k): | |
""" | |
:type nums: List[int] | |
:type k: int | |
:rtype: int | |
""" | |
# O(n) time, quick selection | |
# convert the kth largest to smallest | |
return self.findKthSmallest(nums, len(nums)+1-k) |
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 reverseWords(self, s): | |
""" | |
:type str: List[str] | |
:rtype: void Do not return anything, modify str in-place instead. | |
""" | |
s.reverse() | |
index = 0 | |
for i in range(len(s)): |
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 reverseWords(self, s): | |
""" | |
:type s: str | |
:rtype: str | |
""" | |
return " ".join(s.split()[::-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
class Solution(object): | |
def missingNumber(self, nums): | |
""" | |
:type nums: List[int] | |
:rtype: int | |
""" | |
n = len(nums) | |
return n * (n+1) / 2 - sum(nums) |
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 findDuplicate(self, nums): | |
""" | |
:type nums: List[int] | |
:rtype: int | |
""" | |
if len(nums) > 1: | |
slow = nums[0] | |
fast = nums[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
# Definition for singly-linked list. | |
# class ListNode(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.next = None | |
class Solution(object): | |
def getIntersectionNode(self, headA, headB): | |
""" | |
:type head1, head1: ListNode |
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 reverseBetween(self, head, m, n): | |
""" | |
:type head: ListNode | |
:type m: int | |
:type n: int | |
:rtype: ListNode | |
""" | |
# corner case | |
if m == n: |
NewerOlder