ARTS08
Algorithm
401. 二進位制手錶
二進位制手錶頂部有 4 個 LED 代表小時(0-11),底部的 6 個 LED 代表分鐘(0-59)。 每個 LED 代表一個 0 或 1,最低位在右側。
例如,上面的二進位制手錶讀取 “3:25”。 給定一個非負整數 n 代表當前 LED 亮著的數量,返回所有可能的時間。
案例:
輸入: n = 1 返回: [“1:00”, “2:00”, “4:00”, “8:00”, “0:01”, “0:02”, “0:04”, “0:08”, “0:16”, “0:32”]
注意事項:
輸出的順序沒有要求。 小時不會以零開頭,比如 “01:00” 是不允許的,應為 “1:00”。 分鐘必須由兩位陣列成,可能會以零開頭,比如 “10:2” 是無效的,應為 “10:02”。
解決方案:
我的解法:
class Solution { public List<String> readBinaryWatch(int num) { int[] hours = {8, 4, 2, 1}; int[] minutes = {32, 16, 8, 4, 2, 1}; List<String> result = new ArrayList<>(); for (int i = 0; i <= num; i++) { List<Integer> listHours = generateDigit(hours, i); List<Integer> listMinutes = generateDigit(minutes, num - i); for ( int hour : listHours) { if (hour >= 12) continue; for (int minute: listMinutes) { if (minute >= 60) continue; result.add(hour +":" + (minute < 10 ? "0" + minute : minute)); } } } return result; } private List<Integer> generateDigit(int[] nums, int count) { List<Integer> result = new ArrayList<>(); generateDigitHelper(nums, count, 0, 0, result); return result; } private void generateDigitHelper(int[] nums, int count, int pos, int sum, List<Integer> result) { if(count == 0) { result.add(sum); return; } for(int i = pos; i < nums.length; i++){ generateDigitHelper(nums, count - 1, i + 1, sum + nums[i], result); } } }
更好的解法:
class Solution { int[] weight = {8, 4, 2, 1, 32, 16, 8, 4, 2, 1}; public List<String> readBinaryWatch(int num) { List<String> res = new ArrayList<>(); helper(0, 0, 0, num, res); return res; } private void helper(int hour, int min, int index, int num, List<String> res) { if (hour > 12 || min >= 60 || hour == 12 && min == 0) { return; } if (num == 0) { String strMin = String.valueOf(min); if (strMin.length() == 1) { strMin = "0" + strMin; } res.add(String.valueOf(hour) + ":" + strMin); } else if (index < 10 && num <= 10 - index && num > 0) { helper(hour, min, index + 1, num, res); if (index < 4) { helper(hour + weight[index], min, index + 1, num - 1, res); } else { helper(hour, min + weight[index], index + 1, num - 1, res); } } } }
python解法:
class Solution: def readBinaryWatch(self, num): """ :type num: int :rtype: List[str] """ return ['%d:%02d' % (h, m) for h in range(12) for m in range(60) if (bin(h) + bin(m)).count('1') == num]
Review
ofollow,noindex" target="_blank">對待程式碼評審的心態
本文認為在程式碼評審這項實踐中心態是最為重要的。
消極心態示例
作者:我寫了很多編碼,我為此感到自豪。 我真的想合併,但我需要請某人檢查我的工作。
評論者:我忙於自己的程式碼,但現在我還有另一個任務要做。
作者:這可能需要很長時間……
評論者:有人在動我的程式碼,我最好確保他們不會搞砸。
作者:嗯……我認為評論者甚至不理解我正在做的事情。
積極心態示例
作者:我寫了一些很棒的程式碼,但我知道它並不完美,我想改進,所以我想得到我信任的人的反饋。 他們很忙,所以我應該讓評論儘可能簡單。
評論者:有人很信任我,讓我看看她花了幾個小時的工作。 我應該盡我所能儘快給予建設性的反饋。
作者:評論者可能很困惑。 我需要讓我的程式碼更清晰。
評論者:某些部分修改得很棒,特別是考慮到她剛加入團隊。 我應該讓她知道。
作者:我感謝他們的努力和時間。 反饋很有幫助。
作者
當作者以積極的方式思考時,他們自然會:
- 盡職盡責,盡力編寫好的程式碼;
- 小心、仔細地檢查他們的工作;
- 寫清晰的提交訊息來說明變更;
- 將大修改成小段提交,使評審者的工作變得更輕鬆;
- 當評審者指出錯誤或改進時,不會感到難受;
- 從反饋中學習而不是保護自己。
##評論者 當評論者以積極的方式思考時,他們自然會:
- 及時回覆評審請求;
- 盡力全面地審查程式碼;
- 積極提供建設性的反饋,並指出潛在的錯誤和改進;
- 要善良,耐心,並使用大量的☺;
- 同理作者的情況 - 也許他們是新人,他們還不熟悉程式碼庫; 也許他們功能釋出的壓力; 或者現有程式碼可能是令人困惑的;
- 從變化中學習,而不是批評它們。
Build the culture first, the rest will follow, as always.
Tip
hub是一個針對github操作的命令列包裝庫。
macOS: 安裝方式
$ brew install hub
別名
$ alias git=hub
$ git version git version 2.17.0 hub version 2.3.0 # ← it works!
克隆專案
$ git clone dotfiles → git clone git://github.com/YOUR_USER/dotfiles.git
克隆其他專案
$ git clone github/hub → git clone git://github.com/github/hub.git
開啟當前專案issues頁面
$ git browse – issues → open https://github.com/github/hub/issues
開啟其他專案wiki
$ git browse mojombo/jekyll wiki → open https://github.com/mojombo/jekyll/wiki
建立主題分支
$ git checkout -b feature ( making changes … ) $ git commit -m “done with feature”
\fork repo
$ git fork → (forking repo on GitHub/">GitHub…) → git remote add YOUR_USER git://github.com/YOUR_USER/hub.git
push變更到遠端
$ git push YOUR_USER feature
開啟一個主題分支的pull request
$ git pull-request → (opens a text editor for your pull request message)