Created
January 29, 2022 16:21
-
-
Save nicksspirit/23640df2e4ebf5b64c7b31168c4c18f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let cmp = function(a, b){ | |
| if(a < b) return -1; | |
| if(a > b) return 1; | |
| return 0; | |
| }; | |
| function bsearch(arr, value, comparator) { | |
| let low = 0, | |
| high = arr.length - 1, | |
| mid; | |
| while (low <= high) { | |
| mid = Math.floor((low + high) / 2); | |
| let result = comparator(arr[mid], value); | |
| if (result > 0) high = mid - 1; | |
| else if (result < 0) low = mid + 1; | |
| else return mid; | |
| } | |
| return -1; | |
| } | |
| console.log(bsearch([1,2,3,4,5,6], 6, cmp)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let html = ` | |
| <div id="start" class="a"> | |
| <div class="aa"> | |
| <span class="aaa"> | |
| hello | |
| </span> | |
| <span class="aab"> | |
| </span> | |
| </div> | |
| <div class="ab"> | |
| <span class="aba"> | |
| </span> | |
| <span class="abb"> | |
| <span class="abb2"> | |
| <span class="abb3"> | |
| </span> | |
| </span> | |
| </span> | |
| </div> | |
| </div> | |
| ` | |
| function bfs(startNode, callback) { | |
| const historyQueue = [] | |
| const visited = [] | |
| historyQueue.push(startNode) | |
| while(historyQueue.length) { | |
| let currNode = historyQueue.shift() | |
| if (currNode == null) continue | |
| if (visited.includes(currNode)) continue | |
| visited.push(currNode) | |
| callback(currNode) | |
| for (let node of Array.from(currNode.children)) { | |
| historyQueue.push(node) | |
| } | |
| } | |
| } | |
| function dfs(startNode, callback) { | |
| const historyStack = [] | |
| const visited = [] | |
| historyStack.push(startNode) | |
| while(historyStack.length) { | |
| let currNode = historyStack.pop() | |
| if (currNode == null) continue | |
| if (visited.includes(currNode)) continue | |
| visited.push(currNode) | |
| callback(currNode) | |
| for (let node of Array.from(currNode.children)) { | |
| historyStack.push(node) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment