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
/** | |
* 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
/* | |
我的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
/*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; |
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: | |
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; |
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(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 |
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(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 |
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 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): |
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
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. |
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
--求一列中第二大的数据 | |
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); |