1.找出整型数组中乘积最大的三个数
思路:1.先进行排序
2.考虑出现最大值的时候出现的情况 两负一正,三正
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
};
2.寻找连续数组中的缺失数
1.通过相减去找一个数字
var array_of_integers = [2, 5, 1, 4, 9, 6, 3, 7];
findMissingNumber(array_of_integers); //8
function findMissingNumber(array_of_integers, upper_bound, lower_bound) {
var sum_of_integers = 0;
let arr=array_of_integers.concat().sort();
for (var i = 0; i < array_of_integers.length; i++) {
sum_of_integers += array_of_integers[i];
}
theoretical_sum = (arr[0]+arr[arr.length-1])*(arr[arr.length-1]-arr[0]+1)/2;
return (theoretical_sum - sum_of_integers)
}
3.数组去重
// 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]
var Arr=[1,2,3,2,4,4,1];
var arr = [Arr[0]]; //创建一个临时数组,并将要去重数组的第一项存入临时数组
for(var i = 1; i < Arr.length; i++) { //从要去重数组第二项开始遍历
if (arr.indexOf(this[i]) == i){ //判断临时数组中是否存有当前项,没有则执行
arr.push(this[i]); //将当前项push到临时数组中
}
}
4.数组中元素最大差值计算
给定某无序数组,求取任意两个元素之间的最大差值,注意,这里要求差值计算中较小的元素下标必须小于较大元素的下标。譬如[7, 8, 4, 9, 9, 15, 3, 1, 10]这个数组的计算值是 11( 15 - 4 ) 而不是 14(15 - 1),因为 15 的下标小于 1。
1.先定义第一个为最小值,与最大相差值为1
2.循环获取后面比最小值小的,赋值给最小值
3.循环中获取比当前最小值大的,并且比当前值减去最小值大于相差值的值赋给相差值
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;
}
5.数组中元素乘积
给定某无序数组,要求返回新数组 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 = [];
numArray.forEach(val=>{
product*=val;
})
for (var i = size - 1; i > -1; i--) {
output[i] = product/numArray[i] ;
}
return output;
}
6.数组交集
给定两个数组,要求求出两个数组的交集,注意,交集中的元素应该是唯一的。
var firstArray = [2, 2, 4, 1];
var secondArray = [1, 2, 0, 2];
function checksameValues(firstArray,secondArray){
let arr=[];
firstArray.forEach((val)=>{
if(secondArray.indexOf(val)!==-1&&arr.indexOf(val)===-1){
arr.push(val);
}
})
return arr;
}
checksameValues(firstArray,secondArray)
7.字符串颠倒字符串
给定某个字符串,要求将其中单词倒转之后然后输出,譬如"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);
}
7.二进制转换
通过某个递归函数将输入的数字转化为二进制字符串:
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 '';
}
}
8.二分搜索
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);
}
}