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;
Edward Hsu changed 5 days agoView mode 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;
Edward Hsu changed 4 months agoView mode 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:
Edward Hsu changed a year agoView mode 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
Edward Hsu changed a year agoView mode 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;
Edward Hsu changed a year agoView mode 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) {}
Edward Hsu changed a year agoView mode 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
Edward Hsu changed a year agoView mode 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 {
Edward Hsu changed a year agoView mode 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) {}
Edward Hsu changed a year agoView mode 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)
Edward Hsu changed a year agoView mode 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;
Edward Hsu changed a year agoView mode 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;
}
Edward Hsu changed a year agoView mode 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;
Edward Hsu changed a year agoView mode 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) {}
Edward Hsu changed a year agoView mode 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) {
Edward Hsu changed a year agoView mode Like Bookmark