2021年1月25日 星期一

A good material for C

 http://shihyu.github.io/books/index.html

 https://medium.com/swlh/my-preparation-journey-for-google-interviews-f41e2dc3cdf9


2019年12月22日 星期日

[LeetCode] 50. Pow(x, n)

我覺得這題應該算Easy,

如果說歸在Medium的原因,

應該是看你能不能一次把Edge Case考慮到位.


題目很單純,

給定x, 輸出x的n次方

大致上需要考慮的Edge Case:

n = 0的情況

x = 1 or x = -1

跟平方的結果overflow的情況.

2019年12月20日 星期五

sqrt(x)的實作方法

A tour of Go裡面學到的

給定一個值x, 找出z, 使得z平方最接近x
(找出x的平方根)

裡面提到可以運用Newton's Method

z -= (z * z - x) / (2 * z)

z從1開始猜,
跑個十次, 每次的值會越來越接近x

double sqrt(int x) {
    double z = 1;
    for (int i = 0; i < 10; ++i) {
        z -= (z * z - x) / (2 * z)
    }
    return z;
}

2019年12月19日 星期四

關於shared library

Compile Time:
編譯時系統預設會從/lib, /usr/lib尋找so檔.

但如果你的so檔不在預設目錄裡面,
就必須指定, ex:
g++ -L /usr/local/lib -I /usr/local/include test.cc -lfoo -o test

-l: 程式會用到libfoo.so這個library
-L: 從/usr/local/lib位置尋找library
-I:  從/usr/local/include位置尋找header

Runtime:
執行時找不到library的情況

1. LD_LIBRARY_PATH
設定後會從這個目錄找library, ex:
LD_LIBRARY_PATH=/usr/local/lib test

指定目錄後執行test

2. 添加系統預設搜尋目錄
在/etc/ld.so.conf裡面添加目錄
然後執行sudo ldconfig更新目錄cache

接著再執行test

3. 編譯時加入rpath選項
rpath可以指定執行時搜尋目錄
g++ -L /usr/local/lib -I /usr/local/include -Wl,-rpath=/usr/local/lib test.cc -lfoo -o test

2019年12月16日 星期一

在Yocto啟動ICECC Distributed Compiler

安裝icecc
$ apt install icecc
會裝icecc跟icecc-scheduler

在區網中找一台啟動icecc-scheduler
其他啟動iceccd

在local.conf裡面加入
# Support icecc
ICECC_PARALLEL_MAKE = "-j 24"
INHERIT += "icecc"

就ok了

2019年11月22日 星期五

[LeetCode] 77. Combinations

題目:
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

Example:
Input: n = 4, k = 2
Output:
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]
思路:
用遞迴的方式, 從後面排回來
12
13
   -> 23
14
   -> 24
   -> 34

n = 4, k = 3
123
124
   -> 134
           -> 234

https://gist.github.com/richardneko/6870d498e3916733728917b0d9c8ba7f






[LeetCode] 46. Permutations

題目:
Given a collection of distinct integers, return all possible permutations.

Example:
Input: [1,2,3]
Output:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]
思路:
從1開始排
1

接著2可以插在1的前面或後面, 兩種排列方式
12
21

最後3可以分別插入12和21前中後的位置
12
312
132
123

21
321
231
213

https://gist.github.com/richardneko/8ec0f82967460a6c2a4bdc0740269164

僅為自己刷完題後的紀錄, 不一定是最佳解...