/images/art2.png

Learn from leetCode

Some strategy learning from leetCode

C++ 2d array

Better use a vector(don’t need to consider allocator)

int row = 10;
int col = 10;
int init = 1;
vector<vector <int>> memos(row, vector<int>(col, init)) // init can ignore
int** a = new int*[rowCount];
for(int i = 0; i < rowCount; ++i)
    a[i] = new int[colCount];

delete ...

Traverse a tree

The tree structure:

Lambda in C++

Introduction of Lambda expression in C++ and how to use it.

What is Lambda in C++

Lambda expression is a new feature since c++11. It is used to create anonymous function object to simplify programming process.

How Lambda expressions are composed

[capture](parameters) mutable -> return type {statement}

// ...
#include <functional>
// ...
std:function<int(int, int)> add = [](int a, int b) -> int {return a+b;};

capture

Capture variables from context.

Crontab overview

What is crontab

Crontab is a toll to schedule commands. You can run your terminal command on a specific schedule like run a command every day or every weekends.

Basic command

crontab [-u user] {-l | -e | -r}

commandutility
-llist your cron job
-rremove your cron job
-eedit your cron job
-uspecific a user

How to schedule a command

Cron job looks like:

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                       7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * *  command_to_execute

# export date into a file every minutes
* * * * * date >> ~/Desktop/test.txt

# Mon to Fri, 2:00, 2:30 , 4:00, 4:30
*/30 2,4 * * 1-5 echo "hello world"
symbolmeaning
*any value
,value list separator
-range of value
/step values

crontab.guru

crontab.guru which is a website to check whether your schedule is correct.

DFS & BFS

DFS & BFS are two basic algorithms to traverse a graph(or a tree). DFS is Deep-first search and BFS is Breath-first search.

Basic idea

The idea behind two algorithms are identical but use different auxiliary data structure. DFS use stack and BFS use Queue.

First, Every node have a mark to identify is already be visited or not(it could be a list or an attribute in node).

Second, Push the start point into the auxiliary data structure and loop until structure is empty.

Miller Rabin Algorithm

What is millerRabin algorithm

The Miller–Rabin primality test or Rabin–Miller primality test is a primality test: an algorithm which determines whether a given number is prime, similar to the Fermat primality test and the Solovay–Strassen primality test. It was first discovered by Russian mathematician M. M. Artjuhov in 1967.[1] Gary L. Miller rediscovered it in 1976; Miller’s version of the test is deterministic, but its correctness relies on the unproven extended Riemann hypothesis.[2] Michael O. Rabin modified it to obtain an unconditional probabilistic algorithm in 1980.[3]

Makefile overview

What is makefile. How to write a makefile.

Ref: https://opensource.com/article/18/8/what-how-makefile

What is makefile

A makefile is a file containing a set of directives used by a make build automation tool to generate a target/goal.

You may have used make to compile a program from source code.

How to write makefile

To summarize, below is the syntax of a typical rule:

target: prerequisites
<TAB> recipe

As an example, a target might be a binary file that depends on prerequisites (source files). On the other hand, a prerequisite can also be a target that depends on other dependencies:

Safari Shortcut

Some keyboards shortcuts when using safari

Use arrow keys to go up and down.

Use Option+up/down or Shift+Space / Space to scroll quickly.

Use Command+up/down goto the top & button.

Open Pages in Tabs

Use Command+T to open a new Tab.

Use Shift+Command+Left/right arrow or Control+Tab / Control+Shift+Tab to move from tab to tab.

Use Command+W to close current tab.

Use Command+Z to reopen a closed tab.

Use Command+Option+W to close all tabs expect current tab.

My Zsh setting

Zsh & Bash

Since MacOs 10.15(Catalina). The default shell switch from bash to zsh. It is hard to say which one is better however Zsh has been used more widely than bash especially from Linux user.

Zsh

The Z shell (also known as zsh) is a Unix shell that is built on top of bash (the default shell for macOS (Before MacOs Catalina)) with additional features. It’s recommended to use zsh over bash. It’s also highly recommended to install a framework with zsh as it makes dealing with configuration, plugins and themes a lot nicer.

Erlang overview

Basic

Documentation: https://erlang.org/doc/search/

Functional language

Erlang is a functional language. Code need compile and running line by line.

Every line need finish by a .. like: A = 1..

Module

Every erlang file will consider as a module. You console will compile all module you want.

You have to add -module(<filename>). into first line. Module should be same with filename without suffix.

In erl console, run c(<filename>). to compile it. Run a function is like: <moduleName>: <functionName>(...<argument>)