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
""" | |
Add copy to clipboard from IPython! | |
To install, just copy it to your profile/startup directory, typically: | |
~/.ipython/profile_default/startup/ | |
Example usage: | |
%clip hello world | |
# will store "hello world" |
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
# logging_example.py | |
import logging | |
# Create a custom logger | |
logger = logging.getLogger(__name__) | |
# Create handlers | |
c_handler = logging.StreamHandler() | |
f_handler = logging.FileHandler('file.log') |
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
# copyed from http://code.activestate.com/recipes/522995-priority-dict-a-priority-queue-with-updatable-prio/ | |
from heapq import heapify, heappush, heappop | |
class priority_dict(dict): | |
"""Dictionary that can be used as a priority queue. | |
Keys of the dictionary are items to be put into the queue, and values | |
are their respective priorities. All dictionary methods work as expected. | |
The advantage over a standard heapq-based priority queue is | |
that priorities of items can be efficiently updated (amortized O(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
def reversePairs_using_merge_sort(nums): | |
n = len(nums) | |
buffer = [None] * n | |
# 最本质的方法其实应该想到使用merge_sort,因为天然的思考过程可以考虑到分为两个部分 | |
# reversePairs的个数等于左边reverse的和右边reverse的,然后加上合并过程中reverse的 | |
# 这和merge_sort的过程不谋而和。实现的过程中,控制左边变量i的移动,每次找到一个i | |
# 不停的移动p指针找到符合要求节点的上限,使用j指针找到比i节点小的节点上限,合并和 | |
# 计算reverse pair过程重叠,非常nice的solution | |
def merge_sort_impl(l, r): |
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: | |
def __init__(self, x): | |
self.val = x | |
self.left = None | |
self.right = None | |
def front_travel(root): | |
if not root: return | |
s = [root] | |
while s: |
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
def qsort(nums): | |
qsort_impl(nums, 0, len(nums)-1) | |
return nums | |
def qsort_impl(nums, start, end): | |
if start >= end: return | |
pivot = partition(nums, start, end) | |
qsort_impl(nums, start, pivot-1) | |
qsort_impl(nums, pivot+1, end) |
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
def next_permutation(nums): | |
""" | |
:type nums: List[int] | |
:rtype: void Do not return anything, modify nums in-place instead. | |
""" | |
if not nums: return nums | |
l = len(nums) | |
i, j = l - 2, l - 1 | |
while i >= 0 and nums[i] >= nums[i + 1]: | |
i -= 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
def bisect_left(data, v): | |
left, right = 0, len(data) | |
while left < right: | |
mid = (left+right)//2 | |
if v <= data[mid]: | |
right = mid | |
else: | |
left = mid+1 | |
return right |
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 LinkedNode: | |
def __init__(self, value, next): | |
self.value = value | |
self.next = next | |
class LRUCache: | |
def __init__(self, capacity): | |
self.head = LinkedNode(None, None) | |
self.tail = None |
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
def reversePairs_using_bst_tree(nums): | |
class Node: | |
def __init__(self, v, left=None, right=None): | |
self.val = v | |
# cnt初始值为1,因为节点本身记录cnt,创建的时候就包含了一次 | |
# cnt记录大于等于v的节点个数 | |
self.cnt = 1 | |
self.left = left | |
self.right = right |