如果存在,如何在javascript中查找元素的索引? [英] How to find index of element in javascript if it present?

查看:81
本文介绍了如果存在,如何在javascript中查找元素的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否请您告诉我如何找到索引。我有问题

Could you please tell me how to find the index. I have a problem

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

Example 1:

Input: [1,3,5,6], 5
Output: 2
Example 2:

Input: [1,3,5,6], 2
Output: 1
Example 3:

Input: [1,3,5,6], 7
Output: 4

我尝试过这样

var searchInsert = function(nums, target) {
          if (target > nums[nums.length - 1]) return nums.length;
    if (target < nums[0]) return 0;
    let start = 0,
        end = nums.length - 1,
        mid = Math.floor((start + end) / 2);
    while (start < end) {
        if(nums[mid] == target) return  mid
        if(nums[mid] > target) end = mid -1;
        if(nums[mid] < target) start = mid +1;
        mid = Math.floor((start + end) / 2);
    }
    return nums.length %2 == 0 ? mid +1 : mid
};

我的测试用例失败

Input
[1,3]
2
Output
2
Expected
1

我的测试用例仅在找不到元素时失败,我想在完美索引中插入

my test case only fail when an element is not found I want to insert in perfect index

更新的答案

var searchInsert = function (nums, target) {
    if (target > nums[nums.length - 1]) return nums.length;
    if (target === nums[nums.length - 1]) return nums.length-1;
    if (target < nums[0]) return 0;

    if (target ===nums[0]) return 0;
    let start = 0,
        end = nums.length - 1,
        mid = Math.floor((start + end) / 2);
    while (start <= end) {
        if(nums[mid] == target) return  mid
        if(nums[mid] > target) end = mid -1;
        if(nums[mid] < target) start = mid +1;
        mid = Math.floor((start + end) / 2);
    }
    return  nums[mid] > target ? mid : mid + 1;
};

感谢代码在所有测试用例中都能正常工作

Thanks for the help this code works perfectly all test case

推荐答案

问题与您的最后一条语句有关

Problem is with your last statement

return nums.length %2 == 0 ? mid +1 : mid

因此,在最后一种情况下,您的 mid = 1 ,因为数组的长度为2,所以您要添加 mid + 1

So in the last case your mid = 1 as since the length of array is two you're adding mid + 1

一个方法是在 start === end

const searchInsert = function(nums, target) {
  if (target > nums[nums.length - 1]) return nums.length;
  if (target <= nums[0] || nums.length === 0) return 0;
  let start = 0,
    end = nums.length - 1,
    mid = Math.floor((start + end) / 2);
  while (start < end) {
    if (nums[mid] == target) return mid
    if (nums[mid] > target) end = mid - 1;
    if (nums[mid] < target) start = mid + 1;
    if(start >= end){
      return nums[mid] > target ? mid  : mid + 1 
    }
    mid = Math.floor((start + end) / 2);
  }
};

console.log(searchInsert( [1,3,5,6], 5)) // 2
console.log(searchInsert([1,3,5,6], 2)) // 1
console.log(searchInsert([1,3,5,6], 7)) // 4
console.log(searchInsert([1,3], 2)) // 1
console.log(searchInsert([1,2,3,5], 4)) // 3
console.log(searchInsert([1,2,3,4,5,7,9,10,15], 8)) //6
console.log(searchInsert([0, 1, 3, 4], 2)) // 2
console.log(searchInsert([1],1)) // 0
console.log(searchInsert([0],1)) // 1
console.log(searchInsert([],1)) // 0

这篇关于如果存在,如何在javascript中查找元素的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆