/** * @param {string[][]}message * @return {string} */ functiondecode(message) { if (message.length === 0 || message[0].length === 0) { return''; } let res = ''; let i = 0, j = 0; let top = true; // 表示目前正在向 i 增大方向行进,对应图中的向下行进
while (j < message[0].length) { if (top) { res += message[i++][j++]; } else { res += message[i--][j++]; } if (i === message.length - 1) { top = false; } if (i === 0) { top = true; } }
/** * Definition for isBadVersion() * * @param {integer}version number * @return {boolean}whether the version is bad * isBadVersion = function(version) { * ... * }; */
/** * @param {function}isBadVersion() * @return {function} */ var solution = function(isBadVersion) { /** * @param {integer}n Total versions * @return {integer}The first bad version */ returnfunction(n) { let l = 1, r = n; let ans = 1; while (l <= r) { const mid = l + ((r - l) >> 1); if (isBadVersion(mid)) { // 满足条件,寻找最左满足,因此收缩右边界 r = mid - 1; ans = mid; } else { l = mid + 1; } }
/* you can use this Class which is bundled together with your code class Stack { push(element) { // add element to stack } peek() { // get the top element } pop() { // remove the top element} size() { // count of element } } */
classQueue{ constructor() { this.stack1 = []; this.stack2 = []; } enqueue(element) { // add new element to the rare this.stack1.push(element); } peek() { // get the head element if (this.stack2.length) { returnthis.stack2[this.stack2.length - 1]; } while (this.stack1.length) { this.stack2.push(this.stack1.pop()); } returnthis.stack2[this.stack2.length - 1]; } size() { // return count of element returnthis.stack1.length + this.stack2.length; } dequeue() { // remove the head element if (this.stack2.length) { returnthis.stack2.pop(); } while (this.stack1.length) { this.stack2.push(this.stack1.pop()); } returnthis.stack2.pop(); } }
/** * @param {HTMLElement}rootA * @param {HTMLElement}rootB - rootA and rootB are clone of each other * @param {HTMLElement}nodeA */ const findCorrespondingNode = (rootA, rootB, target) => { if (rootA === target) { return rootB; }
// children 是一个 DOM 的 api,表示所有的子节点 for (let i = 0; i < rootA.children.length; i++) { const res = findCorrespondingNode(rootA.children[i], rootB.children[i], target); // 注意这里必须要判断 res 是否存在 if (res) { return res; } } }