javascript 面试中常见算法问题详解-亚博电竞手机版
阐述下 javascript 中的变量提升
所谓提升,顾名思义即是 javascript 会将所有的声明提升到当前作用域的顶部。这也就意味着我们可以在某个变量声明前就使用该变量,不过虽然 javascript 会将声明提升到顶部,但是并不会执行真的初始化过程。
阐述下 use strict;
的作用
use strict;
顾名思义也就是 javascript 会在所谓严格模式下执行,其一个主要的优势在于能够强制开发者避免使用未声明的变量。对于老版本的浏览器或者执行引擎则会自动忽略该指令。
// example of strict mode "use strict"; catchthemall(); function catchthemall() { x = 3.14; // error will be thrown return x * x; }
解释下什么是 event bubbling 以及如何避免
event bubbling 即指某个事件不仅会触发当前元素,还会以嵌套顺序传递到父元素中。直观而言就是对于某个子元素的点击事件同样会被父元素的点击事件处理器捕获。避免 event bubbling 的方式可以使用event.stoppropagation()
或者 ie 9 以下使用event.cancelbubble
。
== 与 === 的区别是什么
===
也就是所谓的严格比较,关键的区别在于===
会同时比较类型与值,而不是仅比较值。
// example of comparators 0 == false; // true 0 === false; // false 2 == '2'; // true 2 === '2'; // false
解释下 null 与 undefined 的区别
javascript 中,null 是一个可以被分配的值,设置为 null 的变量意味着其无值。而 undefined 则代表着某个变量虽然声明了但是尚未进行过任何赋值。
解释下 prototypal inheritance 与 classical inheritance 的区别
在类继承中,类是不可变的,不同的语言中对于多继承的支持也不一样,有些语言中还支持接口、final、abstract 的概念。而原型继承则更为灵活,原型本身是可以可变的,并且对象可能继承自多个原型。
找出整型数组中乘积最大的三个数
给定一个包含整数的无序数组,要求找出乘积最大的三个数。
var unsorted_array = [-10, 7, 29, 30, 5, -10, -70]; computeproduct(unsorted_array); // 21000 function sortintegers(a, b) { return a - b; } // greatest product is either (min1 * min2 * max1 || max1 * max2 * max3) function computeproduct(unsorted) { var sorted_array = unsorted.sort(sortintegers), product1 = 1, product2 = 1, array_n_element = sorted_array.length - 1; // get the product of three largest integers in sorted array for (var x = array_n_element; x > array_n_element - 3; x--) { product1 = product1 * sorted_array[x]; } product2 = sorted_array[0] * sorted_array[1] * sorted_array[array_n_element]; if (product1 > product2) return product1; return product2 };
寻找连续数组中的缺失数
给定某无序数组,其包含了 n 个连续数字中的 n – 1 个,已知上下边界,要求以o(n)
的复杂度找出缺失的数字。
// the output of the function should be 8 var array_of_integers = [2, 5, 1, 4, 9, 6, 3, 7]; var upper_bound = 9; var lower_bound = 1; findmissingnumber(array_of_integers, upper_bound, lower_bound); //8 function findmissingnumber(array_of_integers, upper_bound, lower_bound) { // iterate through array to find the sum of the numbers var sum_of_integers = 0; for (var i = 0; i < array_of_integers.length; i ) { sum_of_integers = array_of_integers[i]; } // 以高斯求和公式计算理论上的数组和 // formula: [(n * (n 1)) / 2] - [(m * (m - 1)) / 2]; // n is the upper bound and m is the lower bound upper_limit_sum = (upper_bound * (upper_bound 1)) / 2; lower_limit_sum = (lower_bound * (lower_bound - 1)) / 2; theoretical_sum = upper_limit_sum - lower_limit_sum; // return (theoretical_sum - sum_of_integers) }
数组去重
给定某无序数组,要求去除数组中的重复数字并且返回新的无重复数组。
// es6 implementation var array = [1, 2, 3, 5, 1, 5, 9, 1, 2, 8]; array.from(new set(array)); // [1, 2, 3, 5, 9, 8] // es5 implementation var array = [1, 2, 3, 5, 1, 5, 9, 1, 2, 8]; uniquearray(array); // [1, 2, 3, 5, 9, 8] function uniquearray(array) { var hashmap = {}; var unique = []; for(var i = 0; i < array.length; i ) { // if key returns null (unique), it is evaluated as false. if(!hashmap.hasownproperty([array[i]])) { hashmap[array[i]] = 1; unique.push(array[i]); } } return unique; }
数组中元素最大差值计算
给定某无序数组,求取任意两个元素之间的最大差值,注意,这里要求差值计算中较小的元素下标必须小于较大元素的下标。譬如[7, 8, 4, 9, 9, 15, 3, 1, 10]
这个数组的计算值是 11( 15 – 4 ) 而不是 14(15 – 1),因为 15 的下标小于 1。
var array = [7, 8, 4, 9, 9, 15, 3, 1, 10]; // [7, 8, 4, 9, 9, 15, 3, 1, 10] would return `11` based on the difference between `4` and `15` // notice: it is not `14` from the difference between `15` and `1` because 15 comes before 1. findlargestdifference(array); function findlargestdifference(array) { // 如果数组仅有一个元素,则直接返回 -1 if (array.length <= 1) return -1; // current_min 指向当前的最小值 var current_min = array[0]; var current_max_difference = 0; // 遍历整个数组以求取当前最大差值,如果发现某个最大差值,则将新的值覆盖 current_max_difference // 同时也会追踪当前数组中的最小值,从而保证 `largest value in future` - `smallest value before it` for (var i = 1; i < array.length; i ) { if (array[i] > current_min && (array[i] - current_min > current_max_difference)) { current_max_difference = array[i] - current_min; } else if (array[i] <= current_min) { current_min = array[i]; } } // if negative or 0, there is no largest difference if (current_max_difference <= 0) return -1; return current_max_difference; }
数组中元素乘积
给定某无序数组,要求返回新数组 output ,其中 output[i] 为原数组中除了下标为 i 的元素之外的元素乘积,要求以 o(n) 复杂度实现:
var firstarray = [2, 2, 4, 1]; var secondarray = [0, 0, 0, 2]; var thirdarray = [-2, -2, -3, 2]; productexceptself(firstarray); // [8, 8, 4, 16] productexceptself(secondarray); // [0, 0, 0, 0] productexceptself(thirdarray); // [12, 12, 8, -12] function productexceptself(numarray) { var product = 1; var size = numarray.length; var output = []; // from first array: [1, 2, 4, 16] // the last number in this case is already in the right spot (allows for us) // to just multiply by 1 in the next step. // this step essentially gets the product to the left of the index at index 1 for (var x = 0; x < size; x ) { output.push(product); product = product * numarray[x]; } // from the back, we multiply the current output element (which represents the product // on the left of the index, and multiplies it by the product on the right of the element) var product = 1; for (var i = size - 1; i > -1; i--) { output[i] = output[i] * product; product = product * numarray[i]; } return output; }
数组交集
给定两个数组,要求求出两个数组的交集,注意,交集中的元素应该是唯一的。
var firstarray = [2, 2, 4, 1]; var secondarray = [1, 2, 0, 2]; intersection(firstarray, secondarray); // [2, 1] function intersection(firstarray, secondarray) { // the logic here is to create a hashmap with the elements of the firstarray as the keys. // after that, you can use the hashmap's o(1) look up time to check if the element exists in the hash // if it does exist, add that element to the new array. var hashmap = {}; var intersectionarray = []; firstarray.foreach(function(element) { hashmap[element] = 1; }); // since we only want to push unique elements in our case... we can implement a counter to keep track of what we already added secondarray.foreach(function(element) { if (hashmap[element] === 1) { intersectionarray.push(element); hashmap[element] ; } }); return intersectionarray; // time complexity o(n), space complexity o(n) }
颠倒字符串
给定某个字符串,要求将其中单词倒转之后然后输出,譬如”welcome to this javascript guide!” 应该输出为 “emoclew ot siht tpircsavaj !ediug”。
var string = "welcome to this javascript guide!"; // output becomes !ediug tpircsavaj siht ot emoclew var reverseentiresentence = reversebyseparator(string, ""); // output becomes emoclew ot siht tpircsavaj !ediug var reverseeachword = reversebyseparator(reverseentiresentence, " "); function reversebyseparator(string, separator) { return string.split(separator).reverse().join(separator); }
乱序同字母字符串
给定两个字符串,判断是否颠倒字母而成的字符串,譬如mary
与army
就是同字母而顺序颠倒:
var firstword = "mary"; var secondword = "army"; isanagram(firstword, secondword); // true function isanagram(first, second) { // for case insensitivity, change both words to lowercase. var a = first.tolowercase(); var b = second.tolowercase(); // sort the strings, and join the resulting array to a string. compare the results a = a.split("").sort().join(""); b = b.split("").sort().join(""); return a === b; }
会问字符串
判断某个字符串是否为回文字符串,譬如racecar
与race car
都是回文字符串:
ispalindrome("racecar"); // true ispalindrome("race car"); // true function ispalindrome(word) { // replace all non-letter chars with "" and change to lowercase var lettersonly = word.tolowercase().replace(/\s/g, ""); // compare the string with the reversed version of the string return lettersonly === lettersonly.split("").reverse().join(""); }
使用两个栈实现入队与出队
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); } 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(); }
判断大括号是否闭合
创建一个函数来判断给定的表达式中的大括号是否闭合:
var expression = "{{}}{}{}" var expressionfalse = "{}{{}"; isbalanced(expression); // true isbalanced(expressionfalse); // false isbalanced(""); // true 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; }
二进制转换
通过某个递归函数将输入的数字转化为二进制字符串:
decimaltobinary(3); // 11 decimaltobinary(8); // 1000 decimaltobinary(1000); // 1111101000 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 ''; } }
二分搜索
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); } }
判断是否为 2 的指数值
ispoweroftwo(4); // true ispoweroftwo(64); // true ispoweroftwo(1); // true ispoweroftwo(0); // false ispoweroftwo(-1); // false // 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); }