Skip to content

Instantly share code, notes, and snippets.

@cixuuz
cixuuz / 624_1004.py
Created November 4, 2017 21:05
[624. Maximum Distance in Arrays] #leetcode
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]
@cixuuz
cixuuz / 274_1023.py
Created October 23, 2017 19:45
[274. H-Index] #leetcode
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
@cixuuz
cixuuz / 88_1023.py
Created October 23, 2017 18:34
[88. Merge Sorted Array] #leetcode
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:
@cixuuz
cixuuz / 215_1023.py
Created October 23, 2017 18:29
[215. Kth Largest Element in an Array] #leetcode
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)
@cixuuz
cixuuz / 186_1023.py
Created October 23, 2017 18:14
[186. Reverse Words in a String II] #leetcode
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)):
@cixuuz
cixuuz / 151_1023.py
Created October 23, 2017 18:09
[151. Reverse Words in a String] #leetcode
class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
return " ".join(s.split()[::-1])
@cixuuz
cixuuz / 268_1023.py
Created October 23, 2017 18:01
[268. Missing Number] #leetcode
class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
return n * (n+1) / 2 - sum(nums)
@cixuuz
cixuuz / 287_1023.py
Last active October 23, 2017 17:51
[287. Find the Duplicate Number] #leetcode
class Solution(object):
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) > 1:
slow = nums[0]
fast = nums[nums[0]]
@cixuuz
cixuuz / 160_1023.py
Created October 23, 2017 15:47
[160. Intersection of Two Linked Lists] #leetcode
# 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
@cixuuz
cixuuz / 92_1023.py
Created October 23, 2017 14:42
[92. Reverse Linked List II] #leetcode
class Solution:
def reverseBetween(self, head, m, n):
"""
:type head: ListNode
:type m: int
:type n: int
:rtype: ListNode
"""
# corner case
if m == n: