Skip to content

Instantly share code, notes, and snippets.

class Node:
def __init__(self, key=-1, val=-1, prev=None, next=None):
self.key = key
self.val = val
self.prev = prev
self.next = next
class LRUCache:
def __init__(self, capacity: int):
@vancexu
vancexu / leetcode34.py
Created October 8, 2020 06:53
Leetcode 34. Find First and Last Position of Element in Sorted Array
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
if n == 0:
return [-1, -1]
lo = 0
hi = n - 1
while lo < hi:
mid = lo + (hi - lo) // 2
@vancexu
vancexu / leetcode74.py
Created October 8, 2020 06:49
leetcode 74. Search a 2D Matrix
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
m = len(matrix)
if m == 0:
return False
n = len(matrix[0])
if n == 0:
return False
# find last num in first column that <= target
@vancexu
vancexu / thriftEncoding.go
Created June 19, 2018 19:05
thriftEncoding
// thriftEncoding encapsulates thrift serializer/de-serializer.
type thriftEncoding struct{}
func toStructPtr(obj interface{}) interface{} {
val := reflect.ValueOf(obj)
vp := reflect.New(val.Type())
vp.Elem().Set(val)
return vp.Interface()
}
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;