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 / 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 / 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 / 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 / 96. Unique Binary Search Trees.cpp
Last active August 19, 2016 13:25
二叉搜索树的个数和枚举(DP)(vector copy)
/*My naive solution...only beats 0.6% of people...
因为..因为有好多numTrees(n)都重复算了...
所以把这个改成DP的话应该就可以加快不少时间
*/
class Solution {
public:
int numTrees(int n) {
int r = 0;
if (n==0) return 0;
@lyleaf
lyleaf / 94. Binary Tree Inorder Traversal.cpp
Last active August 18, 2016 14:18
二叉树的遍历
class Solution {
public:
void inorderTraversalHelper(TreeNode* root, vector<int>& x) {
if (!root) return;
if (root->left) inorderTraversalHelper(root->left,x);
x.push_back(root->val);
if (root->right) inorderTraversalHelper(root->right,x);
}
vector<int> inorderTraversal(TreeNode* root){
vector<int> x;
@lyleaf
lyleaf / 289. Game of Life.py
Created December 23, 2015 15:05
289. Game of Life
class Solution(object):
def gameOfLife(self, board):
"""
:type board: List[List[int]]
:rtype: void Do not return anything, modify board in-place instead.
"""
def Nnearby(i,j):
n = 0
if j >= 1 and board[i][j-1]==1: n+=1
if j+1 < len(board[0]) and board[i][j+1]==1: n+=1
@lyleaf
lyleaf / 289. Game of Life.py
Created December 23, 2015 15:05
289. Game of Life
class Solution(object):
def gameOfLife(self, board):
"""
:type board: List[List[int]]
:rtype: void Do not return anything, modify board in-place instead.
"""
def Nnearby(i,j):
n = 0
if j >= 1 and board[i][j-1]==1: n+=1
if j+1 < len(board[0]) and board[i][j+1]==1: n+=1
@lyleaf
lyleaf / 226. Invert Binary Tree.py
Created December 23, 2015 10:25
Python Algorithms
#
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def invertTree(self, root):
@lyleaf
lyleaf / 299 Bulls and Cows
Last active November 23, 2015 13:18
299 Bulls and Cows
Comment
https://leetcode.com/problems/bulls-and-cows/
First my solution is to loop it for once to get the dict of the secret. Then loop it for a second time.
Then I see that it can be done in one loop.
--求一列中第二大的数据
select max(PersonId) from Address where PersonID not in (select MAX(PersonId) FROM Address)
--Use < is faster than not in
SELECT max(Salary)
FROM Employee
WHERE Salary <
(SELECT max(Salary)
FROM Employee);