问:能优化下么? 答:可以,上面循环中我对divisor每次增加1,其实 n > 3 的时候divisor每次递增2就可以,因为一个数如果可以被一个偶数整除,那么它就可以被2整除。因此我可以减少一些循环次数。
step-1: Any number will not be divisible by a number bigger than half of it. for example, 13 will never be divisible by 7, 8, 9 .. it could be as big as half of it for even number. for example, 16 will be divisible by 8 but will never be by 9, 10, 11, 12…
Decision: a number will never be divisible by a number bigger than half of its values. So, we dont have to loop 50%
step-2: Now, if a number is not divisible by 3. (if it is divisible by 3, then it wouldn’t be a prime number). then it would be divisible any number bigger than the 1/3 of its value. for example, 35 is not divisible by 3. hence it will be never divisible by any number bigger than 35/3 will never be divisible by 12, 13, 14 … if you take an even number like 36 it will never be divisible by 13, 14, 15
Decision: a number could be divisible by numbers 1/3 of its value.
step-3: For example u have the number 127. 127 is not divisible by 2 hence you should check upto 63.5. Secondly, 127 is not divisible by 3. So, you will check up to 127/3 approximately 42. It is not divisible by 5, divisor should be less than 127/5 approximately 25 not by 7. So, where should we stop?
Decision: divisor would be less than Math.sqrt (n)
functiontopSum(arr) { var biggest = arr[0], second = arr[1], i = 2, len = arr.length; if (biggest < second) { biggest = arr[1]; second = arr[0]; } for (; i < len; i++) { if (biggest < arr[i]) { second = biggest; biggest = arr[i]; } elseif (second < arr[i]) { second = arr[i]; } } return biggest + second; } > topSum([1,8,9,12,2,3,4]) = 21
10. 给定两个无序数组,找出是否能有两个元素想加等于指定的一个数,返回这样的两个数的下标?
思路:最先想到的是双重循环
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
functionsumFinder(arr, num) { var len = arr.length; var res = []; for (var i = 0; i < len - 1; i++) { for (var j = i + 1; j < len; j++) { if (arr[i] + arr[j] === num) { res.push({ index1: i, index2: j }); } } } returnJSON.stringify(res); }
问:有没有更好的方法呢? 答:
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
functionsumFinder(arr, num) { var differ = {}, substract, i = 0; len = arr.length; for (; i < len; i++) { substract = num - arr[i]; if (!differ[substract]) { differ[arr[i]] = true; } else { returntrue } } returnfalse; }
functionsumFinder(arr, num) { arr = arr.sort(function (a, b) { return a - b; }); console.log(arr); var iLeft = 0, iRight = arr.length - 1; var res = []; while (iLeft < iRight) { console.log(1); if (arr[iLeft] + arr[iRight] > num) { iRight--; } elseif (arr[iLeft] + arr[iRight] < num) { iLeft++; } else { res.push({ index1: iLeft, index2: iRight }); iLeft++; iRight--; }
} returnJSON.stringify(res); }
11. 水仙花数?
问:给你个数,判断它是不是水仙花数? 答:水仙花就是每一位的n次幂的和等于其自身的数
代码如下:
1 2 3 4 5 6 7 8 9 10 11
functionnarcissistic(value) { var val = String(value).split(''), len = val.length; var result = val.map(function (v, i) { v = parseInt(v, 10); returnMath.pow(v, len); }).reduce(function (prev, curr, i, arr) { return prev + curr; }); return result === value; }
functiongenerateNarcissistic(n, m) { var result = []; var i = n, val = [], tmp = 0; while (i <= m) { val = String(i).split(''); var len = val.length; tmp = val.map(function (v, i) { v = parseInt(v, 10); returnMath.pow(v, len); }).reduce(function (prev, curr) { return prev + curr; }); if (i === tmp) { result.push(i); } i++; } return result; }
12. 数组乱序?
问:如何将一个排序好的数组乱序? 答:可以运用著名的 Fisher–Yates 算法
具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
functionrandomarizeArray(arr) { var curIndex = 0, randomIndex, tmpValue, len = arr.length;
functionshuffle(array) { var counter = array.length, temp, index;
// While there are elements in the array while (counter > 1) { // Pick a random index index = Math.floor(Math.random() * (--counter)); console.log(index); // Decrease counter by 1
// And swap the last element with it temp = array[counter]; array[counter] = array[index]; array[index] = temp; }
return array; }
13. 数组扁平化?
问:给定一个随机数组,数组可能包含数组,将其扁平化并去掉其中的重复元素。 答:采取递归调用的方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
functionflatArray(arr) { var result = []; process(arr); functionprocess(arr) { var i = 0, len = arr.length, item; for (; i < len; i++) { item = arr[i]; if (typeof item === 'number' && result.indexOf(item) == -1) { result.push(item); } elseif (Object.prototype.toString.call(item) === '[object Array]') { process(item); } } } return result; }
functionrepeater(str) { for (var i = 0; i < str.length; i++) { var char = str.charAt(i); if (str.indexOf(char) === i && str.indexOf(char, i + 1) === -1) { return char; } } returnnull; }
此外还可以通过正则表达式匹配的方式 具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
functionrepeater(str) { var result = false;
while (str) { var len = str.length; var char = string[0]; var reg = newRegExp(char, "gi"); str = str.replace(reg, ""); if (str.length == len - 1) { result = char; break; } } return result; }
还有一种解决方案:
1 2 3 4 5 6 7 8 9 10 11 12
functionrepeater(str) { var strArr = str.split(''); for (var i = 0; i < str.length; i++) { var char = str.charAt(i); if (strArr.filter(function (j) { return j === char; }).length === 1) { return char; } } returnnull; }
15. 找出一个数组的最大和的子数组
问:给你一个数组,如何找出这个数组的最大和子数组 答:
具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
functionchildArray(arr) { var currSum = arr[0], maxSum = arr[0], iL = 0, iR, begain, end; for (var i = 1; i < arr.length; i++) { if (currSum > 0) { currSum += arr[i]; iR = i; } else { currSum = arr[i]; iL = i; iR = i; } if (currSum > maxSum) { maxSum = currSum; begain = iL; end = iR; } } return maxSum+""+begain+""+end; }