【问题标题】:Search in sorted array X for first index i such that X[i] >= a在排序数组 X 中搜索第一个索引 i 使得 X[i] >= a
【发布时间】:2012-07-05 15:11:36
【问题描述】:

我正在努力调整 codaddict proposed here 找到的代码来解决 这个问题的简单变体。有人有想法吗?

【问题讨论】:

  • 你在纠结什么?
  • 我们应该只发布代码还是?
  • 我不是计算机科学家,但你如何修改 codaddict 的代码(可能是标准步骤)。我的意思是,我不能将 if(X[mid] - mid == 0) 更改为 if(X[mid] - mid > 0) 否则算法总是返回 mid :(
  • 您希望使用哪种语言的解决方案?
  • c++(或最终是 JAVA\R\matlab 之一)。语言无所谓,我特别想了解逻辑(如果这是一个令人尴尬的简单问题,再次抱歉)

标签: java c++ algorithm language-agnostic


【解决方案1】:

您采用他的算法,并返回 high 而不是 -1。如果X[high] 小于您的目标,请取而代之的是下一项。如果high 等于您的数组大小,则没有这样的索引。

Michael Anderson 对您所指的算法的编写方式是正确的,但它很容易适应。

int find_hi (const std::vector<int> &X, int t) {
   int low  = 0;
   int high = X.size() - 1;
   while(low <= high) {
       int mid = (low + high) / 2;
       if(X[mid] == t) return mid;
       else if(X[mid] < t)
           low = mid + 1;
       else
           high = mid - 1;
   }
   if (high < sz && X[high] < t) ++high;
   return high;
}

但是,这等价于 C++ 中的以下内容:

int find_hi (const std::vector<int> &X, int t) {
    return std::lower_bound(X.begin(), X.end(), t) - X.begin();
}

【讨论】:

    【解决方案2】:

    这是对binary search的采用。

    #include <list>
    #include <iostream>
    
    int arr[9]={0,1,2,3,4,5,7,7,8};
    
    /*int * array - where to search
      int numberOfElements - the number of elements in array
      int targetValue - the value that we are looking for 
    */
    
    int find( int * array, int numberOfElements, int targetValue)
    {
        //we start with searching in the whole array (this is our "scope").
        int left = 0;
        int right = numberOfElements;
    
        while(left < right)
        {
            //We take the middle item of our "scope" and compare it with the target value.
            int mid = (left + right) / 2;
            if( array[ mid ] >= targetValue)//If we did hit
            {
                //we check is it the first item to match our criteria.              
                if(array[ mid - 1 ] < targetValue)
                {
                    //If it is, we return the index.
                    return mid;
                }
                else
                { 
                    //Else, we continue searching to the left. 
                    right = mid;
                }
            }
            //If we didnt hit from the first guess, we continue with our "scope"
            //being either the left of the right half of the previous "scope".
            else if( array[mid] > targetValue )
                right = mid;
            else
                left = mid;
         }
    }
    
    int main()
    {
        std::cout << find(arr, 9, 7);
    }
    

    output: 3

    【讨论】:

      【解决方案3】:

      要找到第一个索引i where X[i] &gt;= a 你需要像这样修改结束条件来检查它是否真的是第一个满足条件的索引:

      public int findIndex(int[] array, int target) {
          int left = 0;
          int right = array.length;
          while (left < right) {
              int mid = (left + right) / 2;
              if (array[mid] >= target && (mid == 0 || array[mid - 1] < target)) {
                  return mid;
              } else if (array[mid] > target) {
                  right = mid;
              } else
                  left = mid;
          }
          return -1;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-05-09
        • 2011-07-24
        • 1970-01-01
        • 1970-01-01
        • 2020-05-27
        • 1970-01-01
        • 2020-03-20
        • 1970-01-01
        • 2013-11-26
        相关资源
        最近更新 更多