# [Java][LeetCode][Hash] 380. Insert Delete GetRandom O(1)

insert: 加到Map與List中
remove: 從Map與List中移除該數值。移除前，要先將List中最後一個值放到將被移除的數值的位置，並且更新在Map的Key與Value。
getRandom: 以List的size隨機產生一個數，將此數輸出，這樣就可以達到the same probability

This problem requests to implement insert, remove, and get the random element. I am creating a Map to record value and index. Creating a List to record the value that was inserted.
insert: add value to map and list.
remove: remove value from map and list. Before removing the value, I need to get the last element of the list and replace the value that will be removed. Then remove the last element in the list and the Key of value in the Map.

--

--

# [Java][LeetCode][HashMap] Bulls and Cows

Traverse element of secret and guess at the same time. The count will be pulsed one when the guess character is the same as the secret character at the same index. If the secret and guess character are different, the character will be put in map and…

--

--

# [Java][LeetCode][Stack]Asteroid Collision #735

1. 兩個行星要不同的方向
2. 行星是依序碰撞的，不會跳著碰撞
3. 小的行星會被撞毀，一樣大小的時候會兩個都撞毀
4. 後者往前看要撞擊的行星，如果被撞掉了，就再繼續往前比較
5. 當前者為負的，後者為正的，不會被撞掉

--

--

# [Java][LeetCode][BFS][DFS][Graph] Number Of Islands #200

DFS用遞迴的方式，BFS用Queue的方式。

1. 上下左右的位置必須在有matrix圍內:

x≥0 && x<grid.length && y≥0 && y<grid[0].length

2. 上下左右的值為1

grid[m][n]==’1'

DFS is with recursive, and BFS is with Queue.
Traversal all position of ‘1’. Dependent on the position, change the value of neighbors to ‘0’. Keeping in mind:
1. Positions of neighbors are inside the edge of the…

--

--

# [Java][LeetCode][Graph][BFS]

Time complexity is O(m*n)

1. 找每個1，然後去找最近的0，特殊案例就是大多都是1，只有一個0
2. 找每個0，然後去找0附近的1，特殊案例就是大多都是0，只有一個1

“return the distance of the nearest 0 for each cell”
consider 1. find each 1, then find the nearest 0 and count the distance. The worst case is [[1,1,1],[1,1,1],[1,0,1]].
consider 2. find each 0, depend on the coordinate of 0 to search…

--

--

DFS:

BFS:

--

--

# [Java][LeetCode][Bitwise] Binary Number with Alternating Bits #693

The wrong approach is to do modulo and keep modulo in the list until n=0. Pair compare each other. Even I can get the binary array of n and through the pair compare to check n is sparse or not. But when n is a larger number( n=60000)…

--

--

# [Java][LeetCode][BFS] Binary Tree Right Side View #199

Given the `root` of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]

imagine yourself standing on the right side of it

The first time, I did not take care that sentence:

imagine yourself standing on the right side of it

I just only saw right side. I output all right side elements. Absolutely, It is failed… I return to reading this problem again and saw the sentence. My approach is: to keep the last value at every layer, I don’t matter other values at this layer.

The code includes a test suit.

--

--

# [Java][LeetCode][BFS] Binary Tree Zigzag Level Order Traversa #103

Given the `root` of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between).
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[20,9],[15,7]]

output is dependent on Layer, so this problem is…

--

--

# [Android][Firebase] Firebase connect with Android Studio

Add Firebase using the Firebase Console

1. Create a Firebase Project
2. Register an App with Firebase