牛客网:faremax https://www.nowcoder.com/discuss/49349
12.给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
function Power(base, exponent){
return Math.pow(base, exponent);
}
13.输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
function reOrderArray(array){
var result = [];
var even = [];
array.forEach(function(item){
if((item & 1) === 1){
result.push(item);
} else {
even.push(item);
}
});
return result.concat(even);
}
14.输入一个链表,输出该链表中倒数第k个结点。
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function FindKthToTail(head, k){
if(!head || k <= 0){
return null;
}
var i = head, j = head;
while(--k){
j = j.next;
if(!j){
return null;
}
}
while(j.next){
i = i.next;
j = j.next;
}
j = null;
return i;
}
15.输入一个链表,反转链表后,输出链表的所有元素。
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function ReverseList(pHead){
var newHead, temp;
if(!pHead){
return null;
}
if(pHead.next === null){
return pHead;
} else {
newHead = ReverseList(pHead.next);
}
temp = pHead.next;
temp.next = pHead;
pHead.next = null;
temp = null;
return newHead;
}
16.输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function Merge(pHead1, pHead2){
if(!pHead1){
return pHead2 ? pHead2 : null
} else if(!pHead2){
return pHead1;
}
// debugger;
var curr1 = pHead1;
var curr2 = pHead2;
var result = new ListNode(-1);
var curr = result;
while(curr1 && curr2){
if(curr1.val < curr2.val){
curr.next = curr1;
curr1 = curr1.next;
} else{
curr.next = curr2;
curr2 = curr2.next;
}
curr = curr.next;
}
if(curr1){
curr.next = curr1;
}
if(curr2){
curr.next = curr2;
}
//防止内存泄露
curr = result.next;
result.next = null;
result = curr;
curr = curr1 = curr2 = null;
return result;
}
17.输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
function HasSubtree(pRoot1, pRoot2){
if(pRoot1 == null || pRoot2 == null){
return false;
}
if(isSubTree(pRoot1, pRoot2)){
return true;
} else{
return HasSubtree(pRoot1.left, pRoot2) || HasSubtree(pRoot1.right, pRoot2);
}
function isSubTree(pRoot1, pRoot2){
if(pRoot2 == null) return true;
if(pRoot1 == null) return false;
if(pRoot1.val === pRoot2.val){
return isSubTree(pRoot1.left, pRoot2.left) && isSubTree(pRoot1.right, pRoot2.right);
} else {
return false;
}
}
}
18.操作给定的二叉树,将其变换为源二叉树的镜像。
function Mirror(root){
if(!root){
return null;
}
var temp = root.left;
root.left = root.right;
root.right = temp;
if(root.left){
Mirror(root.left);
}
if(root.right){
Mirror(root.right);
}
}
19.输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
function printMatrix(matrix){
if(!matrix || !matrix.length) return null;
var result = [];
var rows = matrix.length, cols = matrix[0].length;
var len = rows * cols;
var i = 0, j = 0;
var circle = 0;
while(1){
while(j < cols - circle){
result.push(matrix[i][j]);
j++;
}
if(result.length === len) break;
j--, i++;
while(i < rows - circle){
result.push(matrix[i][j])
i++;
}
if(result.length === len) break;
i--, j--;
while(j >= circle){
result.push(matrix[i][j]);
j--;
}
if(result.length === len) break;
j++, i--;
circle++;
while(i >= circle){
result.push(matrix[i][j])
i--;
}
if(result.length === len) break;
j++, i++;
}
return result;
}
20.定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
var stack = [];
function push(node){
stack.push(node);
}
function pop(){
return stack.pop();
}
function top(){
return stack[stack.length - 1];
}
function min(){
return Math.min.apply(null, stack);
}
21.输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
function IsPopOrder(pushV, popV){
if(!pushV.length || !popV.length){
return false;
}
var temp = [];
var popIndex = 0;
var len = pushV.length;
for(var i = 0; i < len; i++){
temp.push(pushV[i]);
while(temp.length && temp[temp.length - 1] === popV[popIndex]){
temp.pop();
popIndex++;
}
}
return temp.length === 0;
}
22.从上往下打印出二叉树的每个节点,同层节点从左至右打印。
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function PrintFromTopToBottom(root){
if (!root) {
return [];
}
var queue = [];
queue.push(root);
var result = [];
while (queue.length) {
var temp = queue.shift();
result.push(temp.val);
if (temp.left) {
queue.push(temp.left);
}
if (temp.right) {
queue.push(temp.right);
}
}
return result;
}