# [Java][LeetCode][BinarySearch] Sqrt(X) #69

x=n*n，n是x的平方根，所以n=x/n
product=mid*mid，mid是x的平方根，所以mid=product/mid
。x=mid*mid的時候，表示mid是x的平方根，直接return mid。
。x/mid>mid的時候，表示x>mid*mid，我要將mid*mid往上提升接近x，所以要將left移到mid，又因為知道mid*mid<x，可以肯定target會在mid的右側，所以將left移到mid+1。
。x/mid<mid的時候，表示x<mid*mid，我要將mid*mid往下修正接近x，所以要將right移到mid，又因為知道mid*mid>x，可以肯定target會在mid的左側，所以將right移到mid。

--

--

# [Binary] Logic concept

Making a note about binary logic.
[AND &]
Both elements are 1 get 1.
0&0=0
0&1=0
1&0=0
1&1=1

[OR |]
One element is 1 get 1.
0&0=0
0&1=1
1&0=1
1&1=1

[XOR ^]
Two different elements get 1, and the same element gets 0.
0&0=0
0&1=1
1&0=1
1&1=0

[NOT !]
change 1 to 0, 0 to 1.
!0=1
!1=0

--

--

# [Java][LeetCode][Hash] Group Anagrams

This problem request to classify by a string of the same letter. I split the string into a character array and sorted it. Restructure the sorted char array to a string and it will be a key to the map. Added original string into the value of the map…

--

--

# [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…

--

--

# [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 count the number. The next step is traverse elements of the guess string. If the character is appeared in map, I will subtract the count of character and I will count B number at the same time. Until the count is 0, then remove the character in the map.

--

--

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

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

Asteroids collision conditions are:
1. They are in different directions.
2. Asteroids collide in order in the sequence.
3. The small one will explode. The same size will explode with each other.
4. back one collides front one, if the front is explode, the back one will collide continuously. Until all ahead asteroids explode.
5. When the front one is negative, and the back one is positive, they won’t collide.
with the mention above, I got those conditions:
current<top of the stack: current will collide. The current asteroids will not be kept.
current = top: current and top will collide. the top one will be removed from the stack.
current>top: collide continuously.

--

--

# [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 1 and calculate the level. The worst case is [[0,0,0],[0,0,0],[0,0,1]].
level by level to search is qualify BFS.

--

--

# [Java][LeetCode][Tree][BFS][DFS]

DFS:

BFS:

Implement with DFS and BFS, the time complexity both are O(N).
DFS:
Create a global variable to return the sum when the tree is traversal done. The number of each level is the number of the last level multiple 10 and adds the current node value. The number needs to be delivered to the next level. Until, all nodes were traversed, quite the while loop.
BFS:
Create two Queue, one is to keep the node, and one is to keep the number of the last level calculated. While this side of the tree is traversed, put the number into a list, I will calculate the sum when the two sides of the tree are finished.

--

--