成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

JavaScript 面試中常見算法問題詳解

開發 開發工具 算法
所謂提升,顧名思義即是 JavaScript 會將所有的聲明提升到當前作用域的頂部。這也就意味著我們可以在某個變量聲明前就使用該變量,不過雖然 JavaScript 會將聲明提升到頂部,但是并不會執行真的初始化過程。

[[185725]]

闡述下 JavaScript 中的變量提升

所謂提升,顧名思義即是 JavaScript 會將所有的聲明提升到當前作用域的頂部。這也就意味著我們可以在某個變量聲明前就使用該變量,不過雖然 JavaScript 會將聲明提升到頂部,但是并不會執行真的初始化過程。

闡述下 use strict; 的作用

use strict; 顧名思義也就是 JavaScript 會在所謂嚴格模式下執行,其一個主要的優勢在于能夠強制開發者避免使用未聲明的變量。對于老版本的瀏覽器或者執行引擎則會自動忽略該指令。

  1. // Example of strict mode 
  2. "use strict"
  3.  
  4. catchThemAll(); 
  5. function catchThemAll() { 
  6.   x = 3.14; // Error will be thrown 
  7.   return x * x; 

解釋下什么是 Event Bubbling 以及如何避免

Event Bubbling 即指某個事件不僅會觸發當前元素,還會以嵌套順序傳遞到父元素中。直觀而言就是對于某個子元素的點擊事件同樣會被父元素的點擊事件處理器捕獲。避免 Event Bubbling 的方式可以使用event.stopPropagation() 或者 IE 9 以下使用event.cancelBubble。

== 與 === 的區別是什么

=== 也就是所謂的嚴格比較,關鍵的區別在于=== 會同時比較類型與值,而不是僅比較值。

  1. // Example of comparators 
  2. 0 == false; // true 
  3. 0 === false; // false 
  4.  
  5. 2 == '2'; // true 
  6. 2 === '2'; // false 

解釋下 null 與 undefined 的區別

JavaScript 中,null 是一個可以被分配的值,設置為 null 的變量意味著其無值。而 undefined 則代表著某個變量雖然聲明了但是尚未進行過任何賦值。

解釋下 Prototypal Inheritance 與 Classical Inheritance 的區別

在類繼承中,類是不可變的,不同的語言中對于多繼承的支持也不一樣,有些語言中還支持接口、final、abstract 的概念。而原型繼承則更為靈活,原型本身是可以可變的,并且對象可能繼承自多個原型。

數組

找出整型數組中乘積***的三個數

給定一個包含整數的無序數組,要求找出乘積***的三個數。

  1. var unsorted_array = [-10, 7, 29, 30, 5, -10, -70]; 
  2.  
  3. computeProduct(unsorted_array); // 21000 
  4.  
  5. function sortIntegers(a, b) { 
  6.   return a - b; 
  7.  
  8. // greatest product is either (min1 * min2 * max1 || max1 * max2 * max3) 
  9. function computeProduct(unsorted) { 
  10.   var sorted_array = unsorted.sort(sortIntegers), 
  11.     product1 = 1, 
  12.     product2 = 1, 
  13.     array_n_element = sorted_array.length - 1; 
  14.  
  15.   // Get the product of three largest integers in sorted array 
  16.   for (var x = array_n_element; x > array_n_element - 3; x--) { 
  17.       product1 = product1 * sorted_array[x]; 
  18.   } 
  19.   product2 = sorted_array[0] * sorted_array[1] * sorted_array[array_n_element]; 
  20.  
  21.   if (product1 > product2) return product1; 
  22.  
  23.   return product2 
  24. }; 

尋找連續數組中的缺失數

給定某無序數組,其包含了 n 個連續數字中的 n - 1 個,已知上下邊界,要求以O(n)的復雜度找出缺失的數字。

  1. // The output of the function should be 8 
  2. var array_of_integers = [2, 5, 1, 4, 9, 6, 3, 7]; 
  3. var upper_bound = 9; 
  4. var lower_bound = 1; 
  5.  
  6. findMissingNumber(array_of_integers, upper_bound, lower_bound); //8 
  7.  
  8. function findMissingNumber(array_of_integers, upper_bound, lower_bound) { 
  9.  
  10.   // Iterate through array to find the sum of the numbers 
  11.   var sum_of_integers = 0; 
  12.   for (var i = 0; i < array_of_integers.length; i++) { 
  13.     sum_of_integers += array_of_integers[i]; 
  14.   } 
  15.  
  16.   // 以高斯求和公式計算理論上的數組和 
  17.   // Formula: [(N * (N + 1)) / 2] - [(M * (M - 1)) / 2]; 
  18.   // N is the upper bound and M is the lower bound 
  19.  
  20.   upper_limit_sum = (upper_bound * (upper_bound + 1)) / 2; 
  21.   lower_limit_sum = (lower_bound * (lower_bound - 1)) / 2; 
  22.  
  23.   theoretical_sum = upper_limit_sum - lower_limit_sum; 
  24.  
  25.   // 
  26.   return (theoretical_sum - sum_of_integers) 

數組去重

給定某無序數組,要求去除數組中的重復數字并且返回新的無重復數組。

  1. // ES6 Implementation 
  2. var array = [1, 2, 3, 5, 1, 5, 9, 1, 2, 8]; 
  3.  
  4. Array.from(new Set(array)); // [1, 2, 3, 5, 9, 8] 
  5.  
  6.  
  7. // ES5 Implementation 
  8. var array = [1, 2, 3, 5, 1, 5, 9, 1, 2, 8]; 
  9.  
  10. uniqueArray(array); // [1, 2, 3, 5, 9, 8] 
  11.  
  12. function uniqueArray(array) { 
  13.   var hashmap = {}; 
  14.   var unique = []; 
  15.   for(var i = 0; i < array.length; i++) { 
  16.     // If key returns null (unique), it is evaluated as false
  17.     if(!hashmap.hasOwnProperty([array[i]])) { 
  18.       hashmap[array[i]] = 1; 
  19.       unique.push(array[i]); 
  20.     } 
  21.   } 
  22.   return unique

數組中元素***差值計算

給定某無序數組,求取任意兩個元素之間的***差值,注意,這里要求差值計算中較小的元素下標必須小于較大元素的下標。譬如[7, 8, 4, 9, 9, 15, 3, 1, 10]這個數組的計算值是 11( 15 - 4 ) 而不是 14(15 - 1),因為 15 的下標小于 1。

  1. var array = [7, 8, 4, 9, 9, 15, 3, 1, 10]; 
  2. // [7, 8, 4, 9, 9, 15, 3, 1, 10] would return `11` based on the difference between `4` and `15` 
  3. // Notice: It is not `14` from the difference between `15` and `1` because 15 comes before 1. 
  4.  
  5. findLargestDifference(array); 
  6.  
  7. function findLargestDifference(array) { 
  8.  
  9.   // 如果數組僅有一個元素,則直接返回 -1 
  10.  
  11.   if (array.length <= 1) return -1; 
  12.  
  13.   // current_min 指向當前的最小值 
  14.  
  15.   var current_min = array[0]; 
  16.   var current_max_difference = 0; 
  17.    
  18.   // 遍歷整個數組以求取當前***差值,如果發現某個***差值,則將新的值覆蓋 current_max_difference 
  19.   // 同時也會追蹤當前數組中的最小值,從而保證 `largest value in future` - `smallest value before it` 
  20.  
  21.   for (var i = 1; i < array.length; i++) { 
  22.     if (array[i] > current_min && (array[i] - current_min > current_max_difference)) { 
  23.       current_max_difference = array[i] - current_min; 
  24.     } else if (array[i] <= current_min) { 
  25.       current_min = array[i]; 
  26.     } 
  27.   } 
  28.  
  29.   // If negative or 0, there is no largest difference 
  30.   if (current_max_difference <= 0) return -1; 
  31.  
  32.   return current_max_difference; 

數組中元素乘積

給定某無序數組,要求返回新數組 output ,其中 output[i] 為原數組中除了下標為 i 的元素之外的元素乘積,要求以 O(n) 復雜度實現:

  1. var firstArray = [2, 2, 4, 1]; 
  2. var secondArray = [0, 0, 0, 2]; 
  3. var thirdArray = [-2, -2, -3, 2]; 
  4.  
  5. productExceptSelf(firstArray); // [8, 8, 4, 16] 
  6. productExceptSelf(secondArray); // [0, 0, 0, 0] 
  7. productExceptSelf(thirdArray); // [12, 12, 8, -12] 
  8.  
  9. function productExceptSelf(numArray) { 
  10.   var product = 1; 
  11.   var size = numArray.length; 
  12.   var output = []; 
  13.  
  14.   // From first array: [1, 2, 4, 16] 
  15.   // The last number in this case is already in the right spot (allows for us) 
  16.   // to just multiply by 1 in the next step. 
  17.   // This step essentially gets the product to the left of the index at index + 1 
  18.   for (var x = 0; x < size; x++) { 
  19.       output.push(product); 
  20.       product = product * numArray[x]; 
  21.   } 
  22.  
  23.   // From the back, we multiply the current output element (which represents the product 
  24.   // on the left of the indexand multiplies it by the product on the right of the element) 
  25.   var product = 1; 
  26.   for (var i = size - 1; i > -1; i--) { 
  27.       output[i] = output[i] * product; 
  28.       product = product * numArray[i]; 
  29.   } 
  30.  
  31.   return output

數組交集

給定兩個數組,要求求出兩個數組的交集,注意,交集中的元素應該是唯一的。

  1. var firstArray = [2, 2, 4, 1]; 
  2. var secondArray = [1, 2, 0, 2]; 
  3.  
  4. intersection(firstArray, secondArray); // [2, 1] 
  5.  
  6. function intersection(firstArray, secondArray) { 
  7.   // The logic here is to create a hashmap with the elements of the firstArray as the keys. 
  8.   // After that, you can use the hashmap's O(1) look up time to check if the element exists in the hash 
  9.   // If it does exist, add that element to the new array. 
  10.  
  11.   var hashmap = {}; 
  12.   var intersectionArray = []; 
  13.  
  14.   firstArray.forEach(function(element) { 
  15.     hashmap[element] = 1; 
  16.   }); 
  17.  
  18.   // Since we only want to push unique elements in our case... we can implement a counter to keep track of what we already added 
  19.   secondArray.forEach(function(element) { 
  20.     if (hashmap[element] === 1) { 
  21.       intersectionArray.push(element); 
  22.       hashmap[element]++; 
  23.     } 
  24.   }); 
  25.  
  26.   return intersectionArray; 
  27.  
  28.   // Time complexity O(n), Space complexity O(n) 

字符串

顛倒字符串

給定某個字符串,要求將其中單詞倒轉之后然后輸出,譬如"Welcome to this Javascript Guide!" 應該輸出為 "emocleW ot siht tpircsavaJ !ediuG"。

  1. var string = "Welcome to this Javascript Guide!"
  2.  
  3. // Output becomes !ediuG tpircsavaJ siht ot emocleW 
  4. var reverseEntireSentence = reverseBySeparator(string, ""); 
  5.  
  6. // Output becomes emocleW ot siht tpircsavaJ !ediuG 
  7. var reverseEachWord = reverseBySeparator(reverseEntireSentence, " "); 
  8.  
  9. function reverseBySeparator(string, separator) { 
  10.   return string.split(separator).reverse().join(separator); 

亂序同字母字符串

給定兩個字符串,判斷是否顛倒字母而成的字符串,譬如Mary與Army就是同字母而順序顛倒:

  1. var firstWord = "Mary"
  2. var secondWord = "Army"
  3.  
  4. isAnagram(firstWord, secondWord); // true 
  5.  
  6. function isAnagram(firstsecond) { 
  7.   // For case insensitivity, change both words to lowercase. 
  8.   var a = first.toLowerCase(); 
  9.   var b = second.toLowerCase(); 
  10.  
  11.   // Sort the strings, and join the resulting array to a string. Compare the results 
  12.   a = a.split("").sort().join(""); 
  13.   b = b.split("").sort().join(""); 
  14.  
  15.   return a === b; 

會問字符串

判斷某個字符串是否為回文字符串,譬如racecar與race car都是回文字符串:

  1. isPalindrome("racecar"); // true 
  2. isPalindrome("race Car"); // true 
  3.  
  4. function isPalindrome(word) { 
  5.   // Replace all non-letter chars with "" and change to lowercase 
  6.   var lettersOnly = word.toLowerCase().replace(/\s/g, ""); 
  7.  
  8.   // Compare the string with the reversed version of the string 
  9.   return lettersOnly === lettersOnly.split("").reverse().join(""); 

棧與隊列

使用兩個棧實現入隊與出隊

  1. var inputStack = []; // First stack 
  2. var outputStack = []; // Second stack 
  3.  
  4. // For enqueue, just push the item into the first stack 
  5. function enqueue(stackInput, item) { 
  6.   return stackInput.push(item); 
  7.  
  8. function dequeue(stackInput, stackOutput) { 
  9.   // Reverse the stack such that the first element of the output stack is the 
  10.   // last element of the input stack. After that, pop the top of the output to 
  11.   // get the first element that was ever pushed into the input stack 
  12.   if (stackOutput.length <= 0) { 
  13.     while(stackInput.length > 0) { 
  14.       var elementToOutput = stackInput.pop(); 
  15.       stackOutput.push(elementToOutput); 
  16.     } 
  17.   } 
  18.  
  19.   return stackOutput.pop(); 

判斷大括號是否閉合

創建一個函數來判斷給定的表達式中的大括號是否閉合:

  1. var expression = "{{}}{}{}" 
  2. var expressionFalse = "{}{{}"
  3.  
  4. isBalanced(expression); // true 
  5. isBalanced(expressionFalse); // false 
  6. isBalanced(""); // true 
  7.  
  8. function isBalanced(expression) { 
  9.   var checkString = expression; 
  10.   var stack = []; 
  11.  
  12.   // If empty, parentheses are technically balanced 
  13.   if (checkString.length <= 0) return true
  14.  
  15.   for (var i = 0; i < checkString.length; i++) { 
  16.     if(checkString[i] === '{') { 
  17.       stack.push(checkString[i]); 
  18.     } else if (checkString[i] === '}') { 
  19.       // Pop on an empty array is undefined 
  20.       if (stack.length > 0) { 
  21.         stack.pop(); 
  22.       } else { 
  23.         return false
  24.       } 
  25.     } 
  26.   } 
  27.  
  28.   // If the array is not empty, it is not balanced 
  29.   if (stack.pop()) return false
  30.   return true

遞歸

二進制轉換

通過某個遞歸函數將輸入的數字轉化為二進制字符串:

  1. decimalToBinary(3); // 11 
  2. decimalToBinary(8); // 1000 
  3. decimalToBinary(1000); // 1111101000 
  4.  
  5. function decimalToBinary(digit) { 
  6.   if(digit >= 1) { 
  7.     // If digit is not divisible by 2 then recursively return proceeding 
  8.     // binary of the digit minus 1, 1 is added for the leftover 1 digit 
  9.     if (digit % 2) { 
  10.       return decimalToBinary((digit - 1) / 2) + 1; 
  11.     } else { 
  12.       // Recursively return proceeding binary digits 
  13.       return decimalToBinary(digit / 2) + 0; 
  14.     } 
  15.   } else { 
  16.     // Exit condition 
  17.     return ''
  18.   } 

二分搜索

  1. function recursiveBinarySearch(array, value, leftPosition, rightPosition) { 
  2.   // Value DNE 
  3.   if (leftPosition > rightPosition) return -1; 
  4.  
  5.   var middlePivot = Math.floor((leftPosition + rightPosition) / 2); 
  6.   if (array[middlePivot] === value) { 
  7.     return middlePivot; 
  8.   } else if (array[middlePivot] > value) { 
  9.     return recursiveBinarySearch(array, value, leftPosition, middlePivot - 1); 
  10.   } else { 
  11.     return recursiveBinarySearch(array, value, middlePivot + 1, rightPosition); 
  12.   } 

數字

判斷是否為 2 的指數值

  1. isPowerOfTwo(4); // true 
  2. isPowerOfTwo(64); // true 
  3. isPowerOfTwo(1); // true 
  4. isPowerOfTwo(0); // false 
  5. isPowerOfTwo(-1); // false 
  6.  
  7. // For the non-zero case
  8. function isPowerOfTwo(number) { 
  9.   // `&` uses the bitwise n. 
  10.   // In the case of number = 4; the expression would be identical to
  11.   // `return (4 & 3 === 0)` 
  12.   // In bitwise, 4 is 100, and 3 is 011. Using &, if two values at the same 
  13.   // spot is 1, then result is 1, else 0. In this case, it would return 000, 
  14.   // and thus, 4 satisfies are expression. 
  15.   // In turn, if the expression is `return (5 & 4 === 0)`, it would be false 
  16.   // since it returns 101 & 100 = 100 (NOT === 0) 
  17.  
  18.   return number & (number - 1) === 0; 
  19.  
  20. // For zero-case
  21. function isPowerOfTwoZeroCase(number) { 
  22.   return (number !== 0) && ((number & (number - 1)) === 0); 

 【本文是51CTO專欄作者“張梓雄 ”的原創文章,如需轉載請通過51CTO與作者聯系】

戳這里,看該作者更多好文

責任編輯:武曉燕 來源: 51CTO專欄
相關推薦

2017-11-22 14:20:07

前端JavaScript排序算法

2023-12-04 07:49:06

選擇排序排序算法

2019-06-21 10:13:26

JavaScript錯誤開發

2017-08-16 10:03:57

前端面試題算法

2009-06-30 16:03:00

異常Java

2018-02-06 22:18:47

Java虛擬機面試

2011-04-21 15:04:30

C#

2011-01-21 14:13:10

2020-11-05 18:53:15

JavaScript開發前端

2020-05-29 09:36:59

越權訪問漏洞Web安全

2010-05-12 17:04:20

BlackBerry開

2011-04-08 13:58:52

JavaJSP

2022-08-03 14:52:26

數據治理商業價值貨幣

2019-09-18 09:56:41

MySQLSQL函數

2022-02-04 21:56:59

回溯算法面試

2009-12-31 09:58:51

Ubuntu常見問題

2011-10-11 09:50:44

PhoneGap常見問題

2010-08-31 13:49:12

CSS

2022-03-11 10:01:47

開發跨域技術

2009-11-02 17:25:04

ADSL常見問題
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美精品在线看 | 亚洲国产免费 | 欧美国产激情二区三区 | 国产美女视频黄a视频免费 国产精品福利视频 | 久久综合九色综合欧美狠狠 | 先锋资源网站 | 福利视频网站 | 日本黄色一级片视频 | 99久久99久久精品国产片果冰 | 91视频在线看| 精品亚洲视频在线 | 国产婷婷在线视频 | 国产偷录叫床高潮录音 | 亚洲精品在线看 | 国产精品久久久久久久久久久久久 | 婷婷色在线播放 | 精品国产黄色片 | 久热中文字幕 | 超碰免费在线 | 一区二区三区av | 天天干狠狠 | 精品欧美一区二区三区久久久小说 | 久久99精品视频 | 中文字幕视频在线观看 | 亚洲不卡在线观看 | 日韩在线免费视频 | 一区二区免费视频 | 综合久久综合久久 | 91精品国产综合久久福利软件 | 欧美精品久久久久久久久老牛影院 | 欧美久久不卡 | 国产精品久久99 | 国产精品毛片无码 | 国产一区二区三区四区在线观看 | 久久亚洲一区二区 | 免费观看av网站 | 青青草在线视频免费观看 | 亚洲一区 | 日日日日日日bbbbb视频 | 久久精品国产一区二区电影 | www.中文字幕av |