Maison > Questions et réponses > le corps du texte
第一次提问,如有不当之处,还望海涵。感谢大家。
题目要求:
给定一个数组,要求找到一个元素(的索引),这个元素左边的元素之和 === 右边的元素之和。
You are going to be given an array of integers. Your job is to take that array and find an index N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N. If there is no index that would make this happen, return -1.
For example:
Let's say you are given the array {1,2,3,4,3,2,1}:
Your function will return the index 3, because at the 3rd position of the array, the sum of left side of the index ({1,2,3}) and the sum of the right side of the index ({3,2,1}) both equal 6.Let's look at another one.
You are given the array {1,100,50,-51,1,1}:
Your function will return the index 1, because at the 1st position of the array, the sum of left side of the index ({1}) and the sum of the right side of the index ({50,-51,1,1}) both equal 1.
我提交的代码如下:
function findEvenIndex(arr)
{
function eleadd(x,y){
return x+y;
}
var i;
for (i=1;i<arr.length-1;i++){
if (arr.slice(0,i).reduce(eleadd) === arr.slice(i+1).reduce(eleadd)) {
return i;
}else{
i++;
};
}
return -1;
}
而网站上的测试结果出乎我的意料,有12个failed,因为每个测试参数都非常长,只列出其中一个吧,我在文本编辑器里都找不到它expected的数字:
Your answer failed on: 9785,-6045,-3072,-9878,-6490,-9241,-69,-9603,7650,-2092,-2506,1761,4179,1049,-1382,-2033,-1957,6328,4710,2721,-1410,-1092,-7121,984,8688,-8251,4947,-3032,-4517,3821,-4196,8126,-6526,5440,-6828,3039,2719,-649,-1335,6921,-6592,-1703,-607,3862,-9703,805,7546,-2468,-2109,1944,4366,7731,-3422,3208,-5232,5103,-1594,-832,-8830,-5805,9179,46,1602,9627,-2157,-1734,-8218,5651,5672,6527,1237,-3689,-5910,1834,-8228,1212,-1308,-9463,7463,-1881,-6732,4979,6700,4069,-7867,-8717,166,-6501,4513,-907,-1542,-2800,1701,838,-6168,-419,1456,3065,-622,-2140,9896,-7519,3157,8667,3830,-4675,-4711,-8017,2242,-2254,-9649,-5471,-5531,3914,1877,5006,9058,9680,4467,8217,-7206,5393,6841,-7719,357,9166,2992,-2944,1777,3623,8731,-4,2278,2709,2530,-9983,-1833,9980,1762,8869,1453,-7103,6130,-7269,7853,-4898,-9450,6379,6468,-7278,6849,9582,1093,-7511,-122,-3535,7763,-4411,5385,1134,3042,9549,8032,8884,2115,-9529,-4615,3459,7882,2041,7301,3372,4640,7252,2206,-2085,-7421,9018,1641,1751,-1054,3877,-3715,-9314,4168,-5745,-6919,-1849,8820,5675,-7428,3777,-3660,7204,-3049,153,7720,4110,-389,-92,7463,-6184,-8292,-5917,-2002,-3035,-1591,3126,6433,-64,-4601,505,1992,-7103,8123,-2545,-10000,-6250,6096,5612,-7497,1419,-1625,7249,9574,-1326,-359,2027,-492,-7775,8669,9451,-8643,-9925,5619,-3956,-5580,2892,9910,3694,1782,1751,4869,-7866,9181,-4929,872,3620,9956,1842,4807,-123,1490,796,4474,52,4828,-2534,2365,9253,6767,9876,7873,-8366,9033,8883,6440,8992,8680,5601,3306,3821,5518,-622,-4023,-3878,-4756,-9528,6525,3192,7431,4816,-4481,8960,-4639,6502,3704,6838,-3611,9598,5984,1856,-9285,6590,7424,-8365,-3674,1196,6040,-1623,1329,6464,2189,-9356,2907,6030,6875,2571,-9228,6478,-1813,-3547,-2474,1998,-9972,-8276,-724,-776,-1503,6425,-4289,-7205,9605,-6089,1320,-2216,-6930,2937,-7202,-6526,944,-380,-2937,8877,4809,3652,5761,9647,-8265,-1561,-7902,2219,-5479,9248,7220,8612,6423,3554,671,3309,1549,5260,8433,-9992,1659,3111,4819,7814,8768,-2008,7386,-8665,-4112,1952,-7201,-2996,8623,-1864,-7250,4409,6078,-5748,5250,7987,3236,-5425,771,4733,-2786,-8407,-3384,9719,7949,135924
Expected: 328, instead got: -1
怪我咯2017-04-11 11:35:02
首先你得答案有一个问题,多了一个i++
function findEvenIndex(arr)
{
function eleadd(x,y){
return x+y;
}
var i;
for (i=1;i<arr.length-1;i++){
if (arr.slice(0,i).reduce(eleadd) === arr.slice(i+1).reduce(eleadd)) {
return i;
}
}
return -1;
}
其次,你的代码是O(n^2)的复杂度,太麻烦了,肯定有更好的方法