functionPrint(pRoot) { var res = []; if (!pRoot) { return res; } var que = []; que.push(pRoot); var flag = false; while (que.length > 0) { var vec = []; var len = que.length; for (var i = 0; i < len; i++) { var tmp = que.shift(); //front vec.push(tmp.val); if (tmp.left) que.push(tmp.left); if (tmp.right) que.push(tmp.right); } if (flag) { vec.reverse(); } res.push(vec); flag = !flag; } return res; }
functionPrint(pRoot) { var res = []; if (!pRoot) { return res; } var que = []; que.push(pRoot); while (que.length > 0) { var vec = []; var len = que.length; for (var i = 0; i < len; i++) { var tmp = que.shift(); //front vec.push(tmp.val); if (tmp.left) que.push(tmp.left); if (tmp.right) que.push(tmp.right); } res.push(vec); } return res; }
functionmaxInWindows(num, size) { if (!num || num.length === 0) { returnnull; } var max = []; if (num.length >= size && size >= 1) { var index = []; for (var i = 0; i < size; ++i) { while (index.length > 0 && num[i] >= num[index[index.length - 1]]) index.pop(); index.push(i); } for (var i = size; i < num.length; ++i) { max.push(num[index[0]]); while (index.length > 0 && num[i] >= num[index[index.length - 1]]) index.pop(); if (index.length > 0 && index[0] <= i - size) index.shift(); index.push(i); } max.push(num[index[0]]); } return max; }
63. 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串”bcced”的路径,但是矩阵中不包含”abcb”路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
(function () { /* Todo: This Array contains the input lines in sequence. Each of the elements is like a line of input. */ var test_lines = ['5', '1 2 3 3 5', '3', '1 2 1', '2 4 5', '3 5 3']; // Do not change the name of this array if you don't like bugs. /**************************************************************** Todo: Add your code here including the callback function of event 'rl.on()' and global variables except the statements and definitions of 'readline' and 'rl'. *****************************************************************/ /* global variables here */ var lines = 1; /* callback function of 'rl.on()' */ functionmain(line) { // Do not change the name of this function if you don't like bugs. var str = line.trim(); console.log('lines', lines++, ':', str); } /**************** End of Your Code *****************/ (function () { var test_len = test_lines.length; var test_it = gen(test_lines); var test_val = test_it.next(); while (test_len) { main(test_val.value); test_val = test_it.next(); test_len--; } function* gen(test_lines) { var len = test_lines.length; var i = 0; while (len) { yield test_lines[i]; i++; } } }()) }());