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
/* | |
我的naive方法如下。大体上是正确的BFS,就是每一次找出没有入的节点,放到答案的vector里面去。 | |
如何找出没有入的节点呢?我用的是每一次都用一个新的数组prerequisites来看。每一次就把prerequisites中已经放入vector的course的约束删除。但这 | |
样每一回都要新建一个prerequisites vector. | |
为什么不直接用一个degree来标志入节点的个数呢? | |
*/ | |
class Solution { | |
public: |
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. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: |
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 { | |
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; } |
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 { | |
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){ |
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
/** | |
我的方法用了一个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) {} | |
* }; |
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 { | |
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]); | |
} | |
} |
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
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 |
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
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__ |
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
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 |
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
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 |