Javascript 如何与数组中的其余元素进行比较
我正在研究一个leetcode问题,我想不出一种方法来比较数组中的其他元素。我计算出了最大和最小的数字,但与其他数字相比,我遇到了麻烦。下面您将找到问题以及我的工作: 有多少个数字比当前数字小 给定数组nums,对于每个nums[i]找出数组中有多少个数字小于它。也就是说,对于每个nums[i],您必须计算有效j的数量,以便j!=i和nums[j]Javascript 如何与数组中的其余元素进行比较,javascript,arrays,Javascript,Arrays,我正在研究一个leetcode问题,我想不出一种方法来比较数组中的其他元素。我计算出了最大和最小的数字,但与其他数字相比,我遇到了麻烦。下面您将找到问题以及我的工作: 有多少个数字比当前数字小 给定数组nums,对于每个nums[i]找出数组中有多少个数字小于它。也就是说,对于每个nums[i],您必须计算有效j的数量,以便j!=i和nums[j]{ 常量输出=[] const max=nums.reduce(函数(a,b){ 返回Math.max(a,b); }); const min=num
Input: nums = [8,1,2,2,3]
Output: [4,0,1,1,3]
Explanation:
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
For nums[1]=1 does not exist any smaller number than it.
For nums[2]=2 there exist one smaller number than it (1).
For nums[3]=2 there exist one smaller number than it (1).
For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
var smallerNumbersThanCurrent = (nums) => {
const output = []
const max = nums.reduce(function(a, b) {
return Math.max(a, b);
});
const min = nums.reduce(function(a, b) {
return Math.min(a, b);
});
for(let i = 0; i < nums.length; i++){
if(nums[i] === max){
output.push(nums.length - 1)
} else if (nums[i] === min){
output.push(0)
}
else if (nums[i] < max && nums[i] > min){
//how do i compare with rest of the elements in the array?
}
}
}
我的工作:
Input: nums = [8,1,2,2,3]
Output: [4,0,1,1,3]
Explanation:
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
For nums[1]=1 does not exist any smaller number than it.
For nums[2]=2 there exist one smaller number than it (1).
For nums[3]=2 there exist one smaller number than it (1).
For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
var smallerNumbersThanCurrent = (nums) => {
const output = []
const max = nums.reduce(function(a, b) {
return Math.max(a, b);
});
const min = nums.reduce(function(a, b) {
return Math.min(a, b);
});
for(let i = 0; i < nums.length; i++){
if(nums[i] === max){
output.push(nums.length - 1)
} else if (nums[i] === min){
output.push(0)
}
else if (nums[i] < max && nums[i] > min){
//how do i compare with rest of the elements in the array?
}
}
}
var smallerNumbersThanCurrent=(nums)=>{
常量输出=[]
const max=nums.reduce(函数(a,b){
返回Math.max(a,b);
});
const min=nums.reduce(函数(a,b){
返回Math.min(a,b);
});
for(设i=0;imin){
//如何与数组中的其余元素进行比较?
}
}
}
一种方法是在值小于当前值的情况下过滤数组,然后计算过滤数组中的值数:
constnums=[8,1,2,2,3];
常量smallerNums=nums.map(v=>nums.filter(n=>n console.log(smallerNums);//[4,0,1,1,3]
一种方法是在值小于当前值的条件下过滤数组,然后计算过滤数组中的值数:
constnums=[8,1,2,2,3];
常量smallerNums=nums.map(v=>nums.filter(n=>n console.log(smallerNums);//[4,0,1,1,3]
一种简单得多的方法是对数组进行排序,然后元素的索引将告诉您有多少个元素比它小:
const nums=[8,1,2,2,3]
常量排序=[…nums].sort();
常量结果=nums.map((i)=>{
返回排序后的.findIndex(s=>s==i);
});
控制台日志(结果)代码>一种更简单的方法是简单地对数组排序,然后元素的索引将告诉您有多少比它小:
const nums=[8,1,2,2,3]
常量排序=[…nums].sort();
常量结果=nums.map((i)=>{
返回排序后的.findIndex(s=>s==i);
});
控制台日志(结果)代码>使用嵌套循环
nums=[8,1,2,2,3];
答案=[];
for(设i=0;i使用嵌套循环
nums=[8,1,2,2,3];
答案=[];
for(设i=0;i我想:
函数rankZero(数组){
常量s=[…数组],r=[];
s、 排序((a,b)=>{
返回a-b;
});
for(数组的设n){
r、 推(s.indexOf(n));
}
返回r;
}
log(rankZero([8,1,2,2,3])代码>我想:
函数rankZero(数组){
常量s=[…数组],r=[];
s、 排序((a,b)=>{
返回a-b;
});
for(数组的设n){
r、 推(s.indexOf(n));
}
返回r;
}
log(rankZero([8,1,2,2,3])代码>受@tao的启发,我对每个解决方案进行了性能测试。在我的计算机上(一台带有64GB RAM的Intel Core I9-9900)@StackSlave的解决方案始终是最快的,其次是其他排序解决方案、reduce解决方案、基本迭代和过滤器。您可以自己运行以下测试:
const datalength=1000;
常数迭代=100;
const getRandom=(min,max)=>Math.random()*(max-min)+min;
const data=Array.from({
长度:数据长度
},()=>getRandom(1100));
常量映射器=arr=>arr.map(i=>arr.filter(n=>n{
常量排序=[…nums].sort();
常量结果=nums.map((i)=>{
返回排序后的.findIndex(s=>s==i);
});
};
常量迭代器=arr=>{
常数回答=[];
for(设i=0;i{
常量s=[…数组],
r=[];
s、 排序((a,b)=>{
返回a-b;
});
for(数组的设n){
r、 推(s.indexOf(n));
}
返回r;
}
const reducer=arr=>arr.map(v=>arr.reduce((c,n)=>c+=(n }
受@tao的启发,我对每个解决方案进行了性能测试。在我的计算机上(一台带有64GB RAM的Intel Core I9-9900)@StackSlave的解决方案始终是最快的,其次是其他排序解决方案、reduce解决方案、基本迭代和过滤器。您可以自己运行以下测试:
const datalength=1000;
常数迭代=100;
const getRandom=(min,max)=>Math.random()*(max-min)+min;
const data=Array.from({
长度:数据长度
},()=>getRandom(1100));
常量映射器=arr=>arr.map(i=>arr.map)。