Sing's Log

Knowledge worth sharing


  • Home

  • Tags

  • Timeline

  • Portfolio

  • Service

  • Search

LeetCode - Average of Levels in Binary Tree

Posted on 2017-07-09 | Edited on 2024-07-16 |

一時興起參加了 LeetCode 的 Weekly Contest #40,用Javascript只解出了第一題計算二元樹每層平均值,思維是用兩個陣列,一個存每層的平均,一個存每層的總數量,最後相除得到每層的平均,實作如下:

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
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/

var averageOfLevels = function(root) {
var sumArr = [];
var countArr = [];
var sum = function(node, level) {
if (!node) return
if (!sumArr[level]) sumArr[level] = 0;
sumArr[level] += node.val;
if (!countArr[level]) countArr[level] = 0;
countArr[level]++;
sum(node.left, level + 1);
sum(node.right, level + 1);
}

sum(root, 0);
var meanArr = sumArr.map((sum, level) => sum / levelCountArr[level]);
return meanArr;
};

Git - Create empty branch

Posted on 2017-06-25 | Edited on 2024-07-16 |

Sometimes I want to create empty branch for gh-pages or server-packages instead of branch from master, I will excute following command:

1
2
3
4
5
git checkout --orphan empty.branch.name
git rm --cached -r .
echo "init empty branch" > README.md
git add README.md
git commit -m "init empty branch"

Stackoverflow reference

VSCode - Fix eslint autofix not work in .vue file

Posted on 2017-06-13 | Edited on 2024-07-16 |
  1. Install ESLint extension
  2. Open settings.json by pressing Ctrl,
  3. Add following setting

    1
    2
    3
    "eslint.validate": [
    { "language": "vue", "autoFix": true }
    ]
  4. (Optional) Add "eslint.autoFixOnSave": true for autofix on save.

  5. It should work now, enjoy :)

What is reverse proxy

Posted on 2017-06-06 | Edited on 2024-07-16 |

Proxy means on behalf of :

Proxy(Forward proxy) on behalf of user to reach websites
Reverse proxy on behalf of server to receive users request

Thanks to Dean Sim’s picture on this answer, it is the best to help understand reverse proxy :

More detail on stackoverflow answer

Set up Github Pages custom domain with HTTPS

Posted on 2017-06-06 | Edited on 2024-07-16 |

To set up custom domain with HTTPS on Github Pages, there are 6 steps :

  1. Buy domain
  2. Setup Github Pages CNAME
  3. Add domain to Cloudflare
  4. Setup Cloudflare A record
  5. Change DNS Nameservers
  6. Setup HTTPS in Cloudflare
    Read more »

Enable chrome source maps setting for debugging source script

Posted on 2017-04-15 | Edited on 2024-07-16 |

Open Developer tools (F12) => Settings (F1) => Enable JavaScript source maps

Read more »

HTML5 強大的新功能 data- 屬性

Posted on 2017-03-25 | Edited on 2024-07-16 |

常看到HTML裡有data-這個屬性, 其實這是HTML5才有的,透過讓每個元素有自己的dataset,讓本來需要用ajax獲得或是hide起來的資料,可直接使用data-把值藏好!

範例:

存人物的多項資訊到一個DOM元素中

1
2
3
4
5
<ul id="people">
<li id="person1" data-yearold="30" data-company="MK">Mark</li>
<li id="person2" data-yearold="26" data-company="AC">Andy</li>
<li id="person3" data-yearold="29" data-company="XU">Max</li>
</ul>
Read more »

Gmail sending error 5.5.1 Authentication Required.

Posted on 2017-03-24 | Edited on 2024-07-16 |

錯誤

程式自動寄送Email時發生錯誤:
SMTP 伺服器需要安全連接,或用戶端未經驗證。伺服器回應為: 5.5.1 Authentication Required.

Read more »

Python argparse - handle command line arguments

Posted on 2017-03-20 | Edited on 2024-07-16 |

Library argparse in python could help user handle arguments passed through command prompt, below I record the most important functions :

Read more »

Disable pylint warning

Posted on 2017-03-18 | Edited on 2024-07-16 |

裝了pylint後檔案到處都被標註有問題實在有點煩,於是找了disable部分lint的方式:

Read more »

1…6789
Sing Ming Chen

Sing Ming Chen

Sing's log, a developer's blog

90 posts
259 tags
GitHub E-Mail Linkedin Facebook
© 2024 Sing Ming Chen
Powered by Hexo v3.9.0
|
Theme — NexT.Gemini v6.3.0