JavaScript算法试题系列-10 发表于 2017-06-15 | 更新于 2017-06-15 | 分类于 学习笔记 , JavaScript | | 共 137 字 | 阅读 次 问题描述: 12345// 数字判断是否为 2 的指数值isPowerOfTwo(4); // trueisPowerOfTwo(64); // trueisPowerOfTwo(1); // trueisPowerOfTwo(0); // falseisPowerOfTwo(-1); // false 解答: 123456789101112131415161718// For the non-zero case:function isPowerOfTwo(number) { // `&` uses the bitwise n. // In the case of number = 4; the expression would be identical to: // `return (4 & 3 === 0)` // In bitwise, 4 is 100, and 3 is 011. Using &, if two values at the same // spot is 1, then result is 1, else 0. In this case, it would return 000, // and thus, 4 satisfies are expression. // In turn, if the expression is `return (5 & 4 === 0)`, it would be false // since it returns 101 & 100 = 100 (NOT === 0) return number & (number - 1) === 0;}// For zero-case:function isPowerOfTwoZeroCase(number) { return (number !== 0) && ((number & (number - 1)) === 0);}
JavaScript算法试题系列-09 发表于 2017-06-15 | 更新于 2017-06-15 | 分类于 学习笔记 , JavaScript | | 共 56 字 | 阅读 次 二分法搜索: 1234567891011121314// 二分搜索function recursiveBinarySearch(array, value, leftPosition, rightPosition) { // Value DNE if (leftPosition > rightPosition) return -1; var middlePivot = Math.floor((leftPosition + rightPosition) / 2); if (array[middlePivot] === value) { return middlePivot; } else if (array[middlePivot] > value) { return recursiveBinarySearch(array, value, leftPosition, middlePivot - 1); } else { return recursiveBinarySearch(array, value, middlePivot + 1, rightPosition); }}
JavaScript算法试题系列-08 发表于 2017-06-15 | 更新于 2017-06-15 | 分类于 学习笔记 , JavaScript | | 共 100 字 | 阅读 次 问题描述: 123// 递归二进制转换通过某个递归函数将输入的数字转化为二进制字符串:decimalToBinary(3); // 11decimalToBinary(8); // 1000decimalToBinary(1000); // 1111101000 解答: 123456789101112131415function decimalToBinary(digit) { if(digit >= 1) { // If digit is not divisible by 2 then recursively return proceeding // binary of the digit minus 1, 1 is added for the leftover 1 digit if (digit % 2) { return decimalToBinary((digit - 1) / 2) + 1; } else { // Recursively return proceeding binary digits return decimalToBinary(digit / 2) + 0; } } else { // Exit condition return ''; }}
JavaScript算法试题系列-07 发表于 2017-06-15 | 更新于 2017-06-15 | 分类于 学习笔记 , JavaScript | | 共 123 字 | 阅读 次 问题描述: 1234567// 判断大括号是否闭合创建一个函数来判断给定的表达式中的大括号是否闭合:var expression = "{{}}{}{}";var expressionFalse = "{}{{}";isBalanced(expression); // trueisBalanced(expressionFalse); // falseisBalanced(""); // true 解答: 123456789101112131415161718192021222324function isBalanced(expression) { var checkString = expression; var stack = []; // If empty, parentheses are technically balanced if (checkString.length <= 0) return true; for (var i = 0; i < checkString.length; i++) { if(checkString[i] === '{') { stack.push(checkString[i]); } else if (checkString[i] === '}') { // Pop on an empty array is undefined if (stack.length > 0) { stack.pop(); } else { return false; } } } // If the array is not empty, it is not balanced if (stack.pop()) return false; return true;}
JavaScript算法试题系列-06 发表于 2017-06-15 | 更新于 2017-06-15 | 分类于 学习笔记 , JavaScript | | 共 111 字 | 阅读 次 题目要求: 1234567// 栈与队列使用两个栈实现入队与出队var inputStack = []; // First stackvar outputStack = []; // Second stack// For enqueue, just push the item into the first stackfunction enqueue(stackInput, item) { return stackInput.push(item);} 解答: 12345678910111213function dequeue(stackInput, stackOutput) { // Reverse the stack such that the first element of the output stack is the // last element of the input stack. After that, pop the top of the output to // get the first element that was ever pushed into the input stack if (stackOutput.length <= 0) { while(stackInput.length > 0) { var elementToOutput = stackInput.pop(); stackOutput.push(elementToOutput); } } return stackOutput.pop();}