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 / moveZeroes
Last active September 29, 2015 20:37
Move-Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
@lyleaf
lyleaf / 3-solve.py
Created November 4, 2015 22:49
Algorithm-Homework3-MinimumCut
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 4 22:04:24 2015
@author: yiling
This is done by change the graph g. There must be other ways to better the script
"""
import random
--求一列中第二大的数据
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);
@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.
@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 / 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 / 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 / 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;