FLAMEc隨手記

一陣風飄過~~~

0%

是leetcode的題目,題目如下(直接複製的):

You are given two arrays rowSum and colSum of non-negative integers where rowSum[i] is the sum of the elements in the ith row and colSum[j] is the sum of the elements of the jth column of a 2D matrix. In other words, you do not know the elements of the matrix, but you do know the sums of each row and column.

Find any matrix of non-negative integers of size rowSum.length x colSum.length that satisfies the rowSum and colSum requirements.

Return a 2D array representing any matrix that fulfills the requirements. It’s guaranteed that at least one matrix that fulfills the requirements exists.

Example 1:

Input: rowSum = [3,8], colSum = [4,7]
Output: [[3,0],
[1,7]]
Explanation:
0th row: 3 + 0 = 3 == rowSum[0]
1st row: 1 + 7 = 8 == rowSum[1]
0th column: 3 + 1 = 4 == colSum[0]
1st column: 0 + 7 = 7 == colSum[1]
The row and column sums match, and all matrix elements are non-negative.
Another possible matrix is: [[1,2],
[3,5]]
Example 2:

Input: rowSum = [5,7,10], colSum = [8,6,8]
Output: [[0,5,0],
[6,1,0],
[2,0,8]]

心得:

看來看去這一題主要是要知道這個問題的解法,就像魔術方塊有公式解
這個問題應該也有類似的公式解,應該不是考推導而是考公式解如何轉化成Code。

一開始我是沒碰過這個問題的,所以卡了一下,要推導出公式還是有點難度的QQ
所以看了一下Hint,Hint中提到大概做法,可能是公式之一吧,其實也是有點懷疑真的嗎XD?

Hint 1
Find the smallest rowSum or colSum, and let it be x. Place that number in the grid, and subtract x from rowSum and colSum. Continue until all the sums are satisfied.

一步一步測試後確認可行。就像魔術方塊公式解也有多種,應該也是有更簡化的方法的,這邊就不繼續研究了。

最後如下;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public:
vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
vector<vector<int>> ans(rowSum.size(), vector<int>(colSum.size(),0));


int minRor = 0;
int minCol = 0;

while(true)
{
minRor = 0;
minCol = 0;

//找最小值
for(int i = 1 ; i < rowSum.size(); i++)
{
if((rowSum[i] > 0 && rowSum[i] < rowSum[minRor]) || rowSum[minRor] == 0)
{
minRor = i;
}
}

//找最小值
for(int i = 1 ; i < colSum.size(); i++)
{
if((colSum[i] > 0 && colSum[i] < colSum[minCol]) || colSum[minCol] == 0)
{
minCol = i;
}
}

//都是0結束
if(rowSum[minRor] == 0 && colSum[minCol] == 0)
break;

//塞結果和更新值
if(rowSum[minRor] > colSum[minCol])
{
ans[minRor][minCol] = colSum[minCol];
rowSum[minRor] -= colSum[minCol]; //這邊就是一開始我最懷疑地方,直接改不會沒算出來嗎XD?結果沒事!!
colSum[minCol] = 0;
}
else
{
ans[minRor][minCol] = rowSum[minRor];
colSum[minCol] -= rowSum[minRor]; //這邊就是一開始我最懷疑地方,直接改不會沒算出來嗎XD?結果沒事!!
rowSum[minRor] = 0;
}
}


return ans;
}
};

最近正在研究android新跨平台開發套件 Flutter use android studio

先不說能跨多少平台,在建專案的時候就有兩個選擇!!

1.Jave
2.kotlin

一個新的名稱!! 所以特別去google查了一下狀況~~~如下!!

Kotlin 是針對Java 優化的一種語言(雖然語法稍有不同?)

Kotlin 最大的特色就是 100% 和 JAVA 配合,使用者可以不用在Kotlin與Java 之間猶豫二選一,需要的時候再使用Kotlin 即可。

順便提一下語言差別~~~

靜態語言

需要在宣告型別時定義變數的型別

String hello = ‘hello’;

動態語言

不用事先宣告型別的語言

var a = 0;

直譯式語言

程式碼不用編譯成執行檔直接用直譯器執行

編譯式語言

程式碼會先編譯成下兩種之一
1.執行檔 => 直接執行
2.中間檔 => 平台環境執行器轉譯執行

今天研究好久Vuejs…

目前發現Javascript的import和export很重要吧!!!

因為可以管理好資料!!

Vue部分:

使用方法大概兩種:

  1. 使用Cdn上面的js檔案
  2. 完整安裝整個開發環境(npm create vue@latest)

因為這邊使用了hexo就沒再裝環境了,所以使用1.

目標想要做SliderImage之類,剛好發現一個Vue外掛吧!!

Swiper: 可以很方便做一些Slider功能

使用方法大概兩種:

  1. 使用Cdn
  2. 完整安裝(npm install swiper),但是估計要裝在Vue環境下…

目前只弄了一個Demo:

FLAMEc Demo

進程 Process

就是程式執行後的狀態,在工作管理員看到的狀態~

線程 Thread

一個Process至少會有一個Main Thread,實際執行程式碼的也是Main Thread.

多線程 MultiThread

就是一個Process除了有Main Thread以外,還會多開其他Thread去做任務.

下面都是C++11新功能或調整~

這邊左右值重點XDDD

左值為運算式結束後還會持續存在的非暫時物件

右值則為運算式結束後就不再存在之暫時物件

用在一些傳參數時避免做出call by value動作,導致複製出新物件

右值參照 T &&
用於增加對暫存物件使用
主動宣告成右ref或傳入參數右ref,也不會被判定成右ref,需使用std::move轉換

下面使用重載測試呼叫結果~~~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool checkrref(int&& t) { return true; }
bool checkrref(const int& t) { return false; }

int getRint() {
int a = 5;
return a;
}

void test(int&& t)
{
std::cout << std::boolalpha << checkrref(t) << "\n";
std::cout << std::boolalpha << checkrref(std::move<int&>(t)) << "\n";
std::cout << std::boolalpha << checkrref(getRint()) << "\n";
}

int main()
{
test(5);
}

false
true
true

  1. const 進化版 constexpr

比如常數陣列宣告,可用constexpr變成能呼叫函示或物件建構式

1
2
3
constexpr int FunSize() {return 10;}

int test[FunSize() + 5]; //15
  1. 初始化串列構造式
    std::initializer_list
    可以使建構參數或函式參數變成可透過語法{}靜態的建構,建構後固定不能改,begin傳回指標
    ex int v[]{1,2,3,4,5}; 編譯器會把{…}變成initializer_list丟給陣列XDD

  2. 初始化語法擴展,更方便吧

    1
    2
    3
    4
    5
    6
    struct ex
    {
    int a,b;
    }

    ex t{0,1}
  3. auto 和 decltype
    auto 用法類似 var, ref型態要用auto&,指標不用
    decltype(T) 能在編譯時期算出型態

  4. 範圍for

    1
    2
    3
    4
    5
    int ar[] = {...}
    for(int &n : ar)
    {
    ...
    }
  5. lambda 匿名函式 , 就是C#的 () => {} , 但是C++要用外部引用需要設定
    外部引用 -> return型態 { 函式內容 }
    [](int param) -> int { return param+1; }

外部引用設定
[] // 未定義不能用
[x, &y] // x傳值,y傳ref
[&] // 全部傳ref
[=] // 全部傳值
[&, x] // x傳值,其他全部傳ref
[=, &z] // z傳ref,其他全部傳值

this指標用傳值

mutable,可以使lambda能修改傳值參數,且值能保留,因為式傳值依然不影響外部值

1
2
3
4
5
int mt = 5;
auto f = [mt]() mutable {return mt++; };

f(); //return 5
f(); //return 6
  1. 返回型別後置的函式宣告,如下int,可解決decltype計算一些template的回傳型態

    1
    auto funcenddef() -> int {}
    1
    2
    3
    4
    5
    template <typename Container, typename Index>
    auto DoSomething(Container& c, Index i) -> decltype(c[i])
    {
    return c[i];
    }
  2. 物件建構可以呼叫自己的其他建構

    1
    2
    3
    4
    5
    class A
    {
    A():A(0) { }
    A(int i) {}
    }
  3. override檢查
    virtual void virtual_func(float) override; 如果沒有繼承就會出錯

  4. 新nullptr定義指標空值,可以避免NULL或0有時候多載造成的錯誤

  5. 強型別列舉

    1
    2
    3
    4
    enum class Name : int
    {
    ...
    }

    int是預設值,可不指定,也可換成其他型態如uint

  6. using 很像typedef,差別是能給模板別名
    using B = A
    typedef A B

  7. template可以指定默認預設值
    typename T = A

  8. char16_t , char32_t 對應UTF16和UTF32,原本的char對應UTF8

  9. thread_local 不管宣告在哪都會變成每個thread獨立的static物件
    值會保留且獨立!!

  10. default設定預設建構式
    delete禁止產生設定函式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    struct A
    {
    A() = default; //預設建構式
    A& operator=(const A&) = delete; 禁止產生複製函式

    void b(int i); //輸入int
    void b(float f) = delete;//禁止輸入float
    template<class T> void b(T) = delete; 禁止輸入int以外資料
    }
  11. static_assert 編譯時期測試assertion

  12. C++標準thread,尚未測試,以前都用Win32的thread…
    std::thread
    detach , 執行後不再可控制thread,只能等main thread結束後自行銷毀
    joint , 等待thread結束

量級越大消耗越大吧!
std::mutex 1量級的類別,本身就能鎖,但是可能會忘記解鎖XDDD
std::lock_guard 2量級的類別,只能等結束自毀解鎖
std::unique_lock 3量級的類別,可以靈活unlock再lock,自毀也會解鎖

  1. 多元組型別,就是多個不同型態的東西已特定順序排列的結構
    std::tuple;

  2. 雜湊表(Hash table) , 查詢速度極快!!

    1
    2
    std::unordered_map<T1,T2>
    std::unordered_set<T>

    等等…

  3. 正規表示式 std::regex , 對字串做檢查的

  4. std::shared_ptr是一種ref計數的指針,沒有人在ref時才會銷毀記憶體,可assign給別人,會增加ref count
    std::unique_ptr是自己生命周期到的時候會自動銷毀記憶體,不能assign給別人

  5. 新亂數產生器
    linear_congruential
    subtract_with_carry
    mersenne_twister

  6. std::ref,包裝參照,用在模板需要傳照的時候使用

  7. std::result_of,計算回傳型別統一方法,用在模板return不一樣狀況

  8. std::iota,能對std::array做連續整數填充

  9. std::function 就是 C#System.Action

    1
    2
    3
    4
    5
    int add(int a, int b) {
    return a + b;
    }

    std::function<int(int, int)> func = add;

參考文件:
wiki c++11