加州提子面包


  • 首页

  • 分类

  • 关于

  • 归档

  • 标签

  • 搜索
加州提子面包

JavaScript算法试题系列-10

发表于 2017-06-15 | 更新于 2017-06-15 | 分类于 学习笔记 , JavaScript | | 共 137 字 | 阅读 次

问题描述:

1
2
3
4
5
// 数字判断是否为 2 的指数值isPowerOfTwo(4); // true
isPowerOfTwo(64); // true
isPowerOfTwo(1); // true
isPowerOfTwo(0); // false
isPowerOfTwo(-1); // false

解答:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 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 字 | 阅读 次

二分法搜索:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 二分搜索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 字 | 阅读 次

问题描述:

1
2
3
// 递归二进制转换通过某个递归函数将输入的数字转化为二进制字符串:decimalToBinary(3); // 11
decimalToBinary(8); // 1000
decimalToBinary(1000); // 1111101000

解答:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function 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 字 | 阅读 次

问题描述:

1
2
3
4
5
6
7
// 判断大括号是否闭合创建一个函数来判断给定的表达式中的大括号是否闭合:
var expression = "{{}}{}{}";
var expressionFalse = "{}{{}";
isBalanced(expression); // true
isBalanced(expressionFalse); // false
isBalanced(""); // true

解答:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function 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 字 | 阅读 次

题目要求:

1
2
3
4
5
6
7
// 栈与队列使用两个栈实现入队与出队var inputStack = []; // First stack
var outputStack = []; // Second stack
// For enqueue, just push the item into the first stack
function enqueue(stackInput, item) {
return stackInput.push(item);
}

解答:

1
2
3
4
5
6
7
8
9
10
11
12
13
function 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();
}
123…6
MiG

MiG

Learning | Coding | Reading | Running

30 日志
11 分类
20 标签
RSS
GitHub 微博 知乎
Creative Commons
Links
  • 更新日志
  • CNode
© 2016 - 2018 Created By MiG
由 Hexo 强力驱动
主题 - NexT.Mist