JustinLiu

@JustinLiu

歡迎透過我的email聯絡我 : )

Joined on Aug 16, 2020

  • 題目 https://leetcode.com/problems/maximum-subarray/description/ 思路 這個解法應該是我想得到的解中最快的解了 就是去iterate每一個element後去記錄當前的最高sum跟連續sum 接著隨時去更新maxSum 最後去return class Solution { public:
     Like  Bookmark
  • 題目 https://leetcode.com/problems/reverse-bits/description/ 思路 這個解法滿簡單的,因為我們要保證他是32bit的unsigned int,所以我們要做滿32次。 每次都是自己 $*2+n的最後一個bit$ class Solution { public: uint32_t reverseBits(uint32_t n) {
     Like  Bookmark
  • 題目 https://leetcode.com/problems/number-of-1-bits/description/?envType=daily-question&envId=2023-12-12 去計算一個數字裡面有多少個1。 思路 原版: 先去判斷第0個bit是否為1,如果是的話cnt就++,我們可以透過n&1的回傳來得到結果 class Solution { public:
     Like  Bookmark
  • 題目 https://leetcode.com/problems/single-number/description/ 思路 這個解法就是使用xor來做,奇數個1輸出為1。 意思就是當遇到兩個一樣的數字,就會消掉變成0。 class Solution { public: int singleNumber(vector<int>& nums) {
     Like  Bookmark
  • 題目 https://leetcode.com/problems/reverse-linked-list/ 思路 這個就是透過while,走一圈,結束後return. /** * Definition for singly-linked list. * struct ListNode { * int val;
     Like  Bookmark
  • 題目 https://leetcode.com/problems/validate-binary-search-tree/description/ 思路 參考資料 這個解法主要是透過inorderTraversal的性質來解題一個BST再經過inorderTraversal後,會是monotonic increasing的 所以先把它存起後再去做比較~ class Solution {
     Like  Bookmark
  • Solved on 2024/02/21 Solved 2nd on 2025/04/27 題目 https://leetcode.com/problems/binary-tree-right-side-view/description/ 思路 使用DFS,只記錄最右邊的node (WA) class Solution {
     Like  Bookmark
  • 題目 https://leetcode.com/problems/binary-tree-level-order-traversal/description/ 思路 這個解法其實就是用簡單的BFS來完成level order traversal. 在每次pop之前都先去計算目前的queue有多少個element 如此一來我們就可以知道在這次新增了多少個element /** * Definition for a binary tree node.
     Like  Bookmark
  • 題目 https://leetcode.com/problems/subtree-of-another-tree/description/ 思路 這個解法主要是把問題拆為兩個步驟:第一個步驟是先去traverse這顆tree,直到他們的root是相同的 第二個步驟是當他們的頭相同後,我們再去比較他們的子樹是否相同 如果不相同的話,我們就去判斷root的子樹有沒有可能會成功這樣~ /** * Definition for a binary tree node.
     Like  Bookmark
  • 2024/02/26 Daily Challenge 題目 https://leetcode.com/problems/same-tree/description/ 思路 這個問題其實就是判斷兩棵樹長的相不相同,可以透過比較現在這個node&&left&&right來做決定。 class Solution { public:
     Like  Bookmark
  • 2024/02/27 Daily Challenge 題目 https://leetcode.com/problems/diameter-of-binary-tree/description/ 思路 首先我覺得這題應該要是medium == 一開始看題目的testcase會認為說,只要找到左邊的最深度,跟右邊的最深度再加起來就好。 但是實際submit的時候會遇到一個很討厭的測資:image
     Like  Bookmark
  • 2023/01/21 Daily Challenge 2025/04/05 Second solved by Neetcode150 題目 https://leetcode.com/problems/house-robber/description/?envType=daily-question&envId=2024-01-23 思路 這個解法.... class Solution {
     Like  Bookmark
  • Second solved on 2025/04/05 題目 https://leetcode.com/problems/min-cost-climbing-stairs/description/ 思路 這個爬梯子的題目其實就是一個很基本的DP題目,只要用一個dp array來記錄這個step最少需要多少cost就好。 現在這個狀態只有兩種可能,因為一次可以跨一步或是兩步。所以要看一步前+一步前的cost 或是兩步前+兩步前的cost
     Like  Bookmark
  • Blind 169 Challenge Solved on 2024/02/13 Second solved on Neetcode150 on 2025/02/26 題目 https://leetcode.com/problems/rotting-oranges/description/ 思路 先說這個解法看似正確但答案會是錯的 首先看到這個題目就應該要想到要使用BFS來做擴散,因為他會是levelOrder的去計算level 所以要先造訪grid上的每一個(x,y)來去計算Rotten orange的位置,在這裡我使用pair來儲存x,y,就不需要另外設計資料結構。
     Like  Bookmark
  • Blind 169 Challenge solved on 2024/02/13 聽說這題是某年群暉intern出現過的白板題,一次過超開心>< Neetcode150 Challenge solved on 2025/02/19 (Solve second times) 題目 https://leetcode.com/problems/number-of-islands/description/ 思路
     Like  Bookmark
  • First solved on 2023/12/16 Second solved on 2025/01/04 題目 https://leetcode.com/problems/invert-binary-tree/description/ 思路 這個題目用簡單的DFS搭配交換左子樹跟右子樹即可。 class Solution {
     Like  Bookmark
  • First Solved on 2023/12/29 second solved on 2025/01/04 題目 https://leetcode.com/problems/symmetric-tree/ 思路 這個解法其實不太算是我想的,但我覺得這個寫法很讚 最主要就是在dfs裡面去判斷兩個node是否相同,如果相同的話就繼續往下比....跟一般的DFS差在他用兩個ptr去指
     Like  Bookmark
  • 題目 https://leetcode.com/problems/evaluate-reverse-polish-notation/description/ 思路 這個題目是資料結構常常會出現的題目,通常會在教stack的時候一起出 簡單來說就是把operator放在後面,希望你活用stack特性去解決 我的做法就是如果今天是數字,我就把它放到stack裡面 如果不是的話我就把stack裡面的數字拿出來運算 class Solution {
     Like 1 Bookmark
  • 題目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 思路 這個題目就是典型使用dp的題目,我利用dp table來記錄現在最便宜的買點。 class Solution { public: int maxProfit(vector<int>& prices) { vector<int> dp(prices.size());
     Like  Bookmark
  • Solved on 2024/06/27 Daily Challenge 題目 https://leetcode.com/problems/find-center-of-star-graph/description/?envType=daily-question&envId=2024-06-27 這題目的目的是要你找到哪一個node有連到其他所有的node 思路 這個解法就是用一個hashMap去紀錄哪一個node有連到所有的node.
     Like  Bookmark