我answer1的解決方案是O(n^2)的複雜度,要確認第二個輸入的陣列是第一個陣列的子集需要全部一個對一個的去比較, 全部比對完成沒有任何錯誤才會回傳true,那這樣一來所需最大次數就是兩個陣列元素數量的乘積, 舉例:兩個陣列都是長度為10那麼所需的計算次數最大就是10 * 10 = 10^2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>hello</title> | |
</head> | |
<body> | |
<div>hello world !</div> | |
<script src="https://unpkg.com/almond@0.3.3/almond.js"></script> | |
<script src="/main.js"></script> | |
<script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*問題出在作用域(scope),原先的程式碼中i變數的作用域是在整個createArrayOfFunctions()函式中, | |
陣列中裡每個函式裡面的i在return的時候都會等於y的值,這樣一來就形成了bug, | |
我的解決方案是利用let宣告迴圈的i變數或是將函式改用閉包的方式來將變數的作用域改成我們所想要的範圍內即可*/ | |
function createArrayOfFunctions(y) { //let的改法 | |
var arr = []; | |
for(let i = 0; i<y; i++) { | |
arr[i] = function(x) { return x + i; } | |
} | |
return arr; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function nextFibonacci(intArr){ | |
intArr.forEach(function(num) { | |
index = Math.round(Math.log(num/0.4472135955) / Math.log(1.618033988745)); | |
tmp = Math.round(0.4472135955*Math.pow(1.618033988745,index)); | |
if(num < tmp) | |
console.log(tmp); | |
else | |
console.log(Math.round(0.4472135955*Math.pow(1.618033988745,index+1))); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isSubset(a, b){ | |
for(let index in b){ | |
if(-1 == a.indexOf(b[index])) | |
return false; | |
} | |
return true; | |
} | |
a = ["A","B","C","D","E"]; | |
b = ["A","D","E"]; |