Skip to content

Instantly share code, notes, and snippets.

View chocoluffy's full-sized avatar
🎯
Focusing

Luffy Yu chocoluffy

🎯
Focusing
View GitHub Profile
@chocoluffy
chocoluffy / note.md
Created August 12, 2018 09:53
[【string】frequenet api] #string

stringstream ss; while(getline(ss, symbol, ',')) {...}; // get every part split by seperator. stoi(char); // into int.

@chocoluffy
chocoluffy / note.md
Last active August 25, 2018 15:21
[【c++】 `&` before class function name] #cpp
@chocoluffy
chocoluffy / note.md
Last active August 12, 2018 09:44
[【stack】stack.push() or stack.emplace()] #cpp

push: 直接将元素append进数据结构。只允许一个argument。对于stack<pair<int, char>>的情况,应该在argument处使用constructor: stack.push(std::make_pair(3, 'a')),以保证只有一个argument。 emplace: 将元素放进数据结构,同时call constructor,允许多个argument,相当于简化了上述必须在argument处所做的处理: stack.emplace(3, 'a') 即可。


https://stackoverflow.com/questions/4303513/push-back-vs-emplace-back

In addition to what visitor said :

The function void emplace_back(Type&& _Val) provided by MSCV10 is non conforming and redundant, because as you noted it is strictly equivalent to push_back(Type&& _Val).

@chocoluffy
chocoluffy / solution.cpp
Last active August 12, 2018 09:44
[【stack】 using array and linked list] #data_structure #stack
// https://www.geeksforgeeks.org/stack-data-structure-introduction-program/
// usually implemented with array or linked list implementation.
// Or direcly using STL library: `#include <stack>`.
// following is an example of re-writing stack class, with extra function `max()`, returns the current max element of the stack.
#include <stack>
template <typename T>
class Stack {
@chocoluffy
chocoluffy / solution.cpp
Last active August 12, 2018 02:51
[Palindrome linked List & Zip a linked list] #linked_list #tricks
// Then sugguests that using only O(1) space complexity, meaning only `swap` is recommended!
// 很多关于linked list的技巧基本都和two pointer同时iterate有关。
// 1. using two pointer technique(slower and fast pointer) to find the middle point of the list.
// Q: check if a linked list is palindrome: two pointer iterate elements one by one and check if equal.
// Q: zip a linked list: interleaving the first and second halves of the linked list.
@chocoluffy
chocoluffy / solution.cpp
Last active August 12, 2018 09:44
[【linked list】] #data_structure
struct Node{
int data;
Node* next;
}
@chocoluffy
chocoluffy / solution.cpp
Last active August 11, 2018 13:11
[Q7.9 reverse a singly linked list] #linked_list
node* reverse(node* L) {
node* prev = NULL, curr = L;
node* tmp = NULL;
while(curr) {
tmp = curr.next;
curr.next = prev;
prev = curr;
curr = tmp;
}
return prev;
@chocoluffy
chocoluffy / solution.cpp
Last active August 11, 2018 13:11
[Q7.6 Even-Odd linked list merge] linked list two pointer iteration #linked_list
// Q: given a list <l0, l1, l2, ...>, modify it into <l0, l2, l4,..., l1, l3...> a even-odd merged list.
// 多指针同时遍历的技巧。prev的使用。以及corner case针对even\odd的讨论。一个技巧是,根据已有的变量是否会seg fault来判断需要新增的case,最常见的遗漏就是对是否为null的讨论(针对linked list是否为空等等)。
node* even_odd_merge(node* L) {
node* prev_even = NULL;
node* even = L, odd = L.next;
node* odd_start = L.next;
while(even && odd) {
prev_even = even;
even.next = odd.next;
even = even.next;