- 博客(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 334
转载 RAII 的妙用
RAII 就是为了防止 e.g 1) malloc 忘了 delete 2) open 忘了 close 3) acquire lock忘了 releasegood link https://www.tomdalling.com/blog/software-design/resource-acquisition-is-initialisation-raii-explained/http
2017-10-23 10:22:10 285
原创 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 266
原创 XOR usage
1. single numberhttps://leetcode.com/problems/single-number/description/2. swap value
2017-10-03 12:42:27 223
原创 stack usage
1. Valid Parenthesesclass Solution {public: bool isValid(string s) { stack st; for (char c : s) { switch (c) { case '(':
2017-10-03 11:26:08 760
原创 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 alreadyhttps://www.cprogramming.com/c++11/c++11-ran
2017-10-03 11:07:47 337
原创 Max subarray
Good reference http://www.geeksforgeeks.org/largest-sum-contiguous-subarray/
2017-10-03 10:25:44 309
原创 stack-structured algorithm and queue-structured algorithm
https://www.ics.uci.edu/~eppstein/161/960215.htmlhttp://www.egr.unlv.edu/~larmore/Courses/CSC477/bfsDfs.pdf
2017-10-02 11:35:56 225
原创 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 252
原创 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 210
原创 Invert Binary (recursive and iterative)
两种做饭 递归相对好做1. recursiveclass Solution {public: void swap(TreeNode** left, TreeNode** right) { TreeNode* temp = (*left); *left = *right; *right = temp; } Tr
2016-05-16 08:55:01 364
原创 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 356
原创 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 222
原创 Remove the duplicates from sorted array
其实这个题目有一个关键就是sorted array 如果不是sorted的话 可以用array index bit的类似方法解决这个问题Remove duplicates from sorted array 11. Do not allocate extra space for another array, you must do this in place with constant
2016-04-18 04:42:21 228
原创 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 329
原创 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 272
原创 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 306
原创 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 257
原创 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 265
转载 Aggregate POD and initialize struct to 0
Aggregate class and PODhttp://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special/4178176#4178176An aggregate is an array or a class (clause 9) with no
2016-03-02 12:01:00 423
原创 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%2Bclass Solution {public: bool isPowerOfTwo(int n) {
2016-02-27 11:10:46 399
原创 Reverse Integer
又是一个简单的但是都没一次通过的题目, 第一遍刷题就这么差么自己丑陋的代码 so uglyclass Solution {public: int reverse(int x) { if ((x >= 2147483647) || (x <= -2147483648) || x == 0) { return 0;
2016-02-25 12:53:21 321
原创 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 221
原创 Remove duplicate in sorted linked list 1
Remove duplicate in linked list 1Note1. when you want access n->next you need to make sure thatn is not NULL, but when you want to access n->next->val, you need to make sure thatn->next is not
2016-02-01 10:34:32 313
原创 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 time2
2016-02-01 09:34:59 243
原创 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 230
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人