Skip to content

Instantly share code, notes, and snippets.

View lyleaf's full-sized avatar
💭
I may be slow to respond.

Liu Yiling lyleaf

💭
I may be slow to respond.
  • Google
  • Australia
View GitHub Profile
@lyleaf
lyleaf / 210. Course Schedule II.cpp
Last active August 19, 2016 05:23
Topological Sort Using BFS/DFS
/*
我的naive方法如下。大体上是正确的BFS,就是每一次找出没有入的节点,放到答案的vector里面去。
如何找出没有入的节点呢?我用的是每一次都用一个新的数组prerequisites来看。每一次就把prerequisites中已经放入vector的course的约束删除。但这
样每一回都要新建一个prerequisites vector.
为什么不直接用一个degree来标志入节点的个数呢?
*/
class Solution {
public:
@lyleaf
lyleaf / 23. Merge k Sorted Lists.cpp
Last active August 22, 2016 08:01
把k个已经sort了的linked list合并成一个linked list. key words: priority_queue
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
@lyleaf
lyleaf / 2. Add Two Numbers.cpp
Last active August 23, 2016 07:47
两个整数分别用两个Linked List 表示 ,把它们加起来
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int before = 0;
ListNode* res = new ListNode(0);
ListNode* pre = res;
while (l1 != NULL || l2 != NULL){
int current = 0;
if (!l2) { current = l1->val; l1 = l1->next; }
else if (!l1) { current = l2->val; l2 = l2->next; }
@lyleaf
lyleaf / 16. 3Sum Closest.cpp
Created August 23, 2016 11:52
一个vector里3个数加在一起离target最近的值。Two Pointers
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int res = target + 1000;
sort(nums.begin(),nums.end());
for (int i=0;i<nums.size()-2;i++){
int target2 = target - nums[i];
int j=i+1;
int k=nums.size()-1;
while (j<k){
/**
我的方法用了一个queue,再用一个NULL指针来分辨是不是一层的。
看了答案以后发现optimized的解法是,不需要用queue,因为这个next的link已经可以serve as queue.
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
@lyleaf
lyleaf / 26. Remove Duplicates from Sorted Array.cpp
Last active August 24, 2016 18:20
sorted array去重只需要O(n) time, O(1) space
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.empty()) return 0;
int cur = 0;
for (int i=1;i<nums.size();i++){
if (nums[i] != nums[cur]){
swap(nums[i],nums[++cur]);
}
}
@lyleaf
lyleaf / loop.js
Created September 30, 2019 03:49
Loop an array asynchronously with map
exports.getTranslations = functions.https.onRequest(async (req, res) => {
const english_words = req.body.english_words || [];
console.log(english_words);
const collectionRef = admin.firestore().collection("translations");
const createResponse = (res) => {
var data = {
english_word: (res === undefined) ? '' : res.english_word ,
translation: (res === undefined) ? '' : res.translation ,
transliteration: (res === undefined) ? '' : res.transliteration,
sound_link: (res === undefined) ? '' : res.sound_link
@lyleaf
lyleaf / shape2geojson.py
Last active October 2, 2019 06:14
Create geojson from shape file
import shapefile
# read the shapefile
reader = shapefile.Reader("hello.shp")
fields = reader.fields[1:]
field_names = [field[0] for field in fields]
buffer = []
for sr in reader.shapeRecords():
atr = dict(zip(field_names, sr.record))
geom = sr.shape.__geo_interface__
@lyleaf
lyleaf / geojson2bigquery.py
Created October 2, 2019 06:10
Convert geojson to get schema to upload to bigquery
import json
with open('HGURiau_EoF2019ATR2014Bappenas2011.json', 'r') as ifp:
with open('to_load.json', 'w') as ofp:
features = json.load(ifp)['features']
print(features)
# new-line-separated JSON
schema = None
for obj in features:
props = obj['properties'] # a dictionary
props['geometry'] = json.dumps(obj['geometry']) # make the geometry a string
@lyleaf
lyleaf / pyaudio_recorder.py
Created November 10, 2019 07:14
pyaudio_recorder.py
import pyaudio
import wave
sample_format = pyaudio.paFloat32 # 16 bits per sample
channels = 1
fs = 44100 # Record at 44100 samples per second
seconds = 10
filename = "output.wav"
chunk = 2048#fs*5 # Record in chunks of 1024 samples