- 博客(27)
- 收藏
- 关注
转载 Set Matrix Zeros
class Solution { public: void setZeroes(vector>& matrix) { int rowSize = matrix.size(); int colSize = matrix[0].size(); int col = 1; for (int i = 0; i
2017-12-27 10:17:43
453
转载 RAII 的妙用
RAII 就是为了防止 e.g 1) malloc 忘了 delete 2) open 忘了 close 3) acquire lock忘了 release good link https://www.tomdalling.com/blog/software-design/resource-acquisition-is-initialisation-raii-explained/ http
2017-10-23 10:22:10
400
原创 Sort algorithm
Merge Sort / Insertion Sort/ bubble sort/ selection sort #include using namespace std; int CUTOFF = 7; int arrAux[7]; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp; *yp = tem
2017-10-17 10:32:41
351
原创 XOR usage
1. single number https://leetcode.com/problems/single-number/description/ 2. swap value
2017-10-03 12:42:27
290
原创 stack usage
1. Valid Parentheses class Solution { public: bool isValid(string s) { stack st; for (char c : s) { switch (c) { case '(':
2017-10-03 11:26:08
905
原创 Range based for loop
Range-based for loop is better for container . Strings, arrays, and all STL containers can be iterated over with the new range-based for loop already https://www.cprogramming.com/c++11/c++11-ran
2017-10-03 11:07:47
432
原创 Max subarray
Good reference http://www.geeksforgeeks.org/largest-sum-contiguous-subarray/
2017-10-03 10:25:44
415
原创 stack-structured algorithm and queue-structured algorithm
https://www.ics.uci.edu/~eppstein/161/960215.html http://www.egr.unlv.edu/~larmore/Courses/CSC477/bfsDfs.pdf
2017-10-02 11:35:56
297
原创 Merge two sorted linked list
method 1 dummy node /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { pub
2017-10-02 11:33:49
337
原创 2 sum
method 2. O(n2)的方法 class Solution { public: vector twoSum(vector& nums, int target) { vector result; for (int i = 0; i < nums.size(); ++i) { for (int j = i+1;
2017-10-02 08:17:28
283
原创 Invert Binary (recursive and iterative)
两种做饭 递归相对好做 1. recursive class Solution { public: void swap(TreeNode** left, TreeNode** right) { TreeNode* temp = (*left); *left = *right; *right = temp; } Tr
2016-05-16 08:55:01
471
原创 Tree Traverse
Pre-Order tree Traverse. 1.中左右 两种方法 1. Recurisveclass Solution { public: vector result; vector preorderTraversal(TreeNode* root) { if (root == nullptr) return result;
2016-05-16 08:18:27
436
原创 remove duplicate from sort array 2
这个貌似复杂了点 丑陋的代码 class Solution { public: int removeDuplicates(vector& nums) { if (nums.size() < 3) { return nums.size(); } int c = 0; int i = 1
2016-04-18 06:09:36
292
原创 Remove the duplicates from sorted array
其实这个题目有一个关键就是sorted array 如果不是sorted的话 可以用array index bit的类似方法解决这个问题 Remove duplicates from sorted array 1 1. Do not allocate extra space for another array, you must do this in place with constant
2016-04-18 04:42:21
295
原创 Reverse Bit
这题很简单但是老是没过 因为 运算符优先级搞错 要有括号 class Solution { public: uint32_t reverseBits(uint32_t n) { if (n == 0) return 0; uint32_t bit; uint32_t result = 0; uin
2016-03-16 12:04:20
415
原创 Reorder Linked List
方法1 一次AC了 class Solution { public: void reorderList(ListNode* head) { if (head == NULL || head->next == NULL || head->next->next == NULL) { return; } L
2016-03-14 03:30:50
363
原创 Palindrome 系列
1. Palindrome linked list 这个解法很不错呀 利用了reverse linked list的方法 + 两个指针 class Solution { public: ListNode* reverse(ListNode* node) { ListNode* prev = NULL; while (node != NULL)
2016-03-13 05:31:07
388
原创 Remove Linked List Elements
dummy node的妙用 1.切记 用了dummy node以后 要head 从dummy开始,把他们连接起来 ListNode* dummy = new ListNode(0); dummy->next = head; head = dummy;2. 用temp然后delete temp 完成删除 class Solution { public: ListNode* remov
2016-03-09 13:30:17
322
原创 Odd even Linked list
第一次出现了两次错误 class Solution { public: ListNode* oddEvenList(ListNode* head) { bool flag = false; if (head == NULL || head->next == NULL) { return head; }
2016-03-09 13:23:50
338
转载 Aggregate POD and initialize struct to 0
Aggregate class and POD http://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special/4178176#4178176 An aggregate is an array or a class (clause 9) with no
2016-03-02 12:01:00
504
原创 power of n
Power of 2 return (n&(n-1)) ==0; n & (n-1)==0 is executed as n & ((n-1)==0) See https://en.wikipedia.org/wiki/OperatorsinCandC%2B%2B class Solution { public: bool isPowerOfTwo(int n) {
2016-02-27 11:10:46
496
原创 Reverse Integer
又是一个简单的但是都没一次通过的题目, 第一遍刷题就这么差么 自己丑陋的代码 so ugly class Solution { public: int reverse(int x) { if ((x >= 2147483647) || (x <= -2147483648) || x == 0) { return 0;
2016-02-25 12:53:21
405
原创 Remove duplicates from sorted listed 2
这次又没有能一次通过....so sad..... solution 1: test case 最基本的[1,2,3,4,5,6] 没有能通过 然后想到了加一个flag class Solution { public: ListNode* deleteDuplicates(ListNode* head) { if (head == NULL || head-
2016-02-03 12:26:06
277
原创 Remove duplicate in sorted linked list 1
Remove duplicate in linked list 1 Note 1. when you want access n->next you need to make sure that n is not NULL, but when you want to access n->next->val, you need to make sure that n->next is not
2016-02-01 10:34:32
397
原创 Linked List Cycle
This question is just something like if you know the trick then you can do it, otherwise just no way to solve it. 两个指针 Note: 1. The faster one move 2 steps and slower one move one step each time 2
2016-02-01 09:34:59
301
原创 Reverse Linked List 2
1.dummy Node 的使用 感觉不是简单的就是 new一个dummy node 然后 dummy->next = head; 就好了, 这样是虚的 最关键的是 dummy->next = head; 之后 head 要再从dummy 开始 head = dummy; 这样的dummy才是有效的 并且连续的 2. reverse linked list 2 其实和1 很像 核心部
2016-01-31 12:45:49
285
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅