Skip to main content

Posts

Showing posts with the label array

javascrip Copy each element of the first array into the second array with second array unchanged

  You are given two arrays and an index. Copy each element of the first array into the second array, in order. Begin inserting elements at index  n  of the second array. Return the resulting array. The input arrays should remain the same after the function runs. 给你两个数组和一个索引。 按顺序将第一个数组的每个元素复制到第二个数组中。 开始在第二个数组的索引 n 处插入元素。 返回结果数组。 函数运行后,输入数组应保持不变。 这个题目的难点在于输入数组不能改变,所以需要创建第三个数组,把输入数组深度拷贝过去。 function   frankenSplice ( arr1 ,  arr2 ,  n ) {    var   newarr  = [];    for  ( let   j  =  0 ; j < arr2 . length ; j ++)   {      newarr . push ( arr2 [ j ]);   }    for  ( let   i = arr1 . length - 1 ; i >= 0 ; i --)   {      newarr . splice ( n , 0 , arr1 [ i ]);   }    console . log ( newarr );    return   newarr ; } frankenSplice ([ 1 ,  2 ,  3 ], [ 4 ,  5 ,  6 ],  1 );

Return Largest Numbers in Arrays

  Return an array consisting of the largest number from each provided sub-array. 题目要求:返回两层嵌套数组中,每个小数组中的最大值,组成一个数组,返回这个数组。 思路:程序中创建一个空数组,然后在两层for循环中,空数组中的index就是外层数组的index。 function   largestOfFour ( arr ) {       let   largestArr  = []; //将每个子数组中的最大值push进这个数组    for  ( let   i = 0 ; i < arr . length ; i ++) //数组中的数组   {      largestArr [ i ] = arr [ i ][ 0 ];//初始化为每个小数组第一个值      for ( let   j = 0 ; j < arr [ i ]. length ; j ++) //对每个小数组进行循环判断     {        if  ( largestArr [ i ]<= arr [ i ][ j ])       {          largestArr [ i ] =  arr [ i ][ j ];                }     }   }    return   largestArr ; } console . log ( largestOfFour ([[ 4 ,  5 ,  1 ,  3 ], [ 13 ,  27 ,  18 ,  26 ], [ 32 ,  35 ,  37 ,  39 ], [ 1000 ,  1001 ,  857 ,  1 ]])); largestOfFour ([[ 4 ,  5 ,  1 ,  3 ], [ 13 ,  27 ,  18 ,  26 ], [ 32 ,  35 ,  37 ,  39 ], [ 1000 ,  1001 ,  857 ,  1 ]]);

javascript Return an array consisting of the largest number

 Return an array consisting of the largest number .  题目: 返回一个数组,该数组由每个提供的子数组中的最大数组成。  比如: arraysInArray = [[ 4 ,  5 ,  1 ,  3 ], [ 13 ,  27 ,  18 ,  26 ], [ 32 ,  35 ,  37 ,  39 ], [ 1000 ,  1001 ,  857 ,  1 ]] 我的思路是: 外圈一个大的for循环对大数组中的小数组遍历,内部一个for循环对每个小数组的每个元素进行遍历,用一个变量存储最大的值,用另一个变量记录最大的值所在的小数组在大数组 中的index,最后返回这个arr[index],也就是一个小数组。 function   largestOfFour ( arr ) {    let   largestNum  =  0 ; //存储最大数    let   index  =  0 ; //存储最大数所在的array的序号    for  ( let   i = 0 ; i < arr . length ; i ++) //数组中的数组   {      for ( let   j = 0 ; j < arr [ i ]. length ; j ++) //对每个小数组进行循环判断     {        if  ( largestNum <= arr [ i ][ j ])       {          largestNum  =  arr [ i ][ j ];          index  =  i ; //每次更新index为最大值所在的小数组的index       }     }   }    return   arr [ index ]; } console . log ( largestOfFour ([[ 4 ,  5 ,  1 ,  3 ], [ 13 ,  27 ,  18 ,  26 ], [ 32 ,  35 ,  37 ,  39 ], [ 1000 ,  1001 ,  857 ,  1 ]])) largestOfFour ([[ 4 ,  5 ,  1 ,  3 ], [ 13 ,  27 ,  18 ,  26 ], [ 32 ,  35 , 

how to use Array.‎IndexOf(Array, Object) in C sharp

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ARRAYINDEX {     class Program     {         static void Main(string[] args)         {             double[] numbers = { 15.05, 58.3, 22, 95.55, 177.2, 46.65, 0.85 };             double maxNumber = numbers.Max();             int indexOfMaxNumber = Array.IndexOf(numbers, maxNumber);             Console.WriteLine("The index of max number in the array is {0}", indexOfMaxNumber);             Console.Read();         }     } }