Edward Hsu

@hsuedw

Joined on Feb 18, 2022

  • My Solution Solution 1: DFS (recursion) The Key Idea for Solving This Coding Question C++ Code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right;
     Like  Bookmark
  • My Solution The Key Idea for Solving This Coding Question 用一個陣列來實作環狀佇列 (circular queue) 。不過,==為了區分佇列為滿與佇列為空這兩種狀態,必須犧牲陣列中的一個元素不能存放資料==。所以我們的陣列的長度必須是 k + 1 。 我們必須用兩個索引 (index) 來操作這個陣列。這兩個索引分別命名為 front 與 rear 。 ==front 指向佇列的第一個元素==, ==rear 指向佇列最後一個元素的下一個元素==。所以, ==rear 所指向的那個元素永遠沒有資料==。這就是為什麼我們的陣列長度必須是 k + 1 的原因。 另外,實作 Rear() 時,對陣列的操作必須注意。當 rear 指向索引值 0 時,佇列尾端的元素位於陣列的索引值 k 的位置。不能只是單純的用 rear - 1 去操作陣列。否則,會得到一個負的索引值去操作陣列,這樣是會有 runtime error 出現的。 C++ Code class MyCircularQueue { public: MyCircularQueue(int k) {
     Like  Bookmark
  • My Solution The Key Idea for Solving This Coding Question C++ Code /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {}
     Like  Bookmark
  • My Solution The Key Idea for Solving This Coding Question 輸入陣列 nums 的長度為 n 。且 nums 內的元素為 [0, n] 間的整數。所以有 n+1 個整數要放入 n 個位子中,一定會有一個整數無法放入 nums 中。題目要我們找出 [0, n] 中,沒有被放入 nums 的那個整數。 我們可以先用數學公式 ${(1 + n) \cdot n \over 2}$ 求得 [0, n] 間所有整數的和 answer。然後,遍歷 nums ,逐一將 nums 中的整數自 answer 中減去。只要我們將 nums 中的所有元素都走過一遍, answer 中最後的值就是沒有被放入 nums 中的整數。 C++ Code class Solution { public: int missingNumber(vector<int> &nums) { int n = nums.size();
     Like  Bookmark
  • My Solution Solution 1: DFS (recursion) The Key Idea for Solving This Coding Question C++ Code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right;
     Like  Bookmark
  • My Solution Solution 1: DFS The Key Idea for Solving This Coding Question C++ Code 1 #define WHITE (1) // not visited #define GRAY (2) // visiting the node and its descendants #define BLACK (3) // have visited this node and all its descendants. class Solution { public:
     Like  Bookmark
  • My Solution The Key Idea for Solving This Coding Question Example: $w = [1, 3]$ => $data = [1, 4]$ Therefore, $x$ can be $0$, $1$, $2$ or $3$. We map $x = 0$ to $data[0]$ which is $1$. $x = 1, 2, 3$ to $data[1]$ which is $4$. Therefore, we should find a value in $data$ that is greater than $x$. C++ Code
     Like  Bookmark
  • My Solution The Key Idea for Solving This Coding Question C++ Code class Solution { public: int mySqrt(int x) { unsigned long left = 0, right = x; while (left < right) { unsigned long middle = left + (right - left + 1) / 2;
     Like  Bookmark
  • My Solution The Key Idea for Solving This Coding Question C++ Code 1 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {}
     Like  Bookmark
  • Solution 1 The Key Idea for Solving This Coding Question If a number n is a power of two, the follow statements hold. $n == 2^{x}$ is true and $x$ is a non-negtive integer. There is exactly one 1-bit in the binary representation of n. The key idea is to understand that for any number n, doing a bit-wise AND of n and n−1 flips the least-significant 1-bit of n to 0. n = 01101000
     Like  Bookmark
  • Solution 1 The Key Idea for Solving This Coding Question Satisfy following three conditions num is postive integer num is power of 2. This power of 2 is even power. C++ Code class Solution {
     Like  Bookmark
  • My Solution Solution 1: Top-down Merge Sort The Key Idea for Solving This Coding Question C++ Code 1 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {}
     Like  Bookmark
  • My Solution Solution 1: DFS The Key Idea for Solving This Coding Question Count the number of nodes in the largest component. C++ Code #define WHITE (1) #define GRAY (2) #define BLACK (3)
     Like  Bookmark
  • My Solution Solution 1: DFS and unordered map The Key Idea for Solving This Coding Question C++ Code /* // Definition for Employee. class Employee { public: int id; int importance;
     Like  Bookmark
  • My Solution The Key Idea for Solving This Coding Question C++ Code class Solution { public: int openLock(vector<string> &deadends, string target) { unordered_set<string> deadendSet(deadends.begin(), deadends.end()); if (deadendSet.find("0000") != deadendSet.end()) { return -1; }
     Like  Bookmark
  • My Solution The Key Idea for Solving This Coding Question 在運算的過程中所產生的值有可能超出 int 所能表示的範圍,所以 st 得用 long 或更大的 built-in type 來容納運算的過程中所產生的值。 C++ Code class Solution { public: int evalRPN(vector<string>& tokens) { stack<int> st; int op1, op2;
     Like  Bookmark
  • My Solution The Key Idea for Solving This Coding Question C++ Code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {}
     Like  Bookmark
  • My Solution The Key Idea for Solving This Coding Question C++ Code class Solution { public: bool canTransform(string start, string end) { string startNoX, endNoX; removeX(start, startNoX); removeX(end, endNoX); if (startNoX != endNoX) {
     Like  Bookmark
  • My Solution The Key Idea for Solving This Coding Question C++ Code /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {}
     Like  Bookmark
  • My Solution The Key Idea for Solving This Coding Question C++ Code /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {}
     Like  Bookmark