【问题标题】:SortedSearch.countNumbers(new int[] { 1, 3, 5, 7 }, 4) should return 2 because there are two array elements less than 4SortedSearch.countNumbers(new int[] { 1, 3, 5, 7 }, 4) 应该返回 2,因为有两个数组元素小于 4
【发布时间】:2018-03-14 16:13:50
【问题描述】:

需要更高效的方式

结果如下:

  • 示例案例:正确答案

  • 各种小数组:正确答案

  • sortedArray 包含 lessThan 时的性能测试:时间 超出限制

  • sortedArray 不包含 lessThan 时的性能测试:超出时间限制

以下是代码:

import java.util.Arrays;

public class SortedSearch {
    public static int countNumbers(int[] sortedArray, int lessThan) {

        Arrays.sort(sortedArray);
        int count =0;

        for(int num :sortedArray){

            if(num < lessThan)
            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(SortedSearch.countNumbers(new int[] { 1, 3, 5, 7 }, 4));
    }
}

参考链接:Try here

【问题讨论】:

  • 查找“二分查找”
  • System.out.println(count) 放在你的 for 循环中。我怀疑你的循环没有正确实施。您将能够看到您的循环运行了多少次。
  • 为什么要对已排序的数组进行排序??

标签: java arrays sorting


【解决方案1】:

试试下面的代码

public static int countNumbers(int[] sortedArray, int lessThan) {
    int start = 0;
    int end = sortedArray.length - 1;
    int mid = 0;
    while (start <= end) {
        mid = (start + end) / 2;
        if (sortedArray[mid] < lessThan) {
            if (mid < sortedArray.length - 1 && sortedArray[mid + 1] < lessThan) { // check id next value is also valid
                start = mid + 1;
                continue;
            } else
                return mid + 1;
        }

        if (sortedArray[mid] >= lessThan) {
            end = mid - 1;
        } else {
            start = mid + 1;
        }
    }
    return 0;

}

【讨论】:

    【解决方案2】:

    不需要排序,只用foreach,比较有效的方法

    【讨论】:

    • 为每个添加但仍然相同的结果;检查上面的代码
    【解决方案3】:

    @shmosel 代码是正确的,但不需要 for 循环。 binarySearch 方法返回您正在查找的值的索引。因此,您只需返回 i。

    public static int countNumbers(int[] sortedArray, int lessThan) {
        int i = java.util.Arrays.binarySearch(sortedArray, lessThan);
        if (i < 0) 
            return -i - 1;
        else
            return i;
    }
    
    【解决方案4】:

    你可以使用Java内置的二分查找:

    public static int countNumbers(int[] sortedArray, int lessThan) {
        int i = java.util.Arrays.binarySearch(sortedArray, lessThan);
        if (i < 0) {
            return -i - 1;
        } else {
            // in case of duplicates, find first occurrence
            for (; i > 0 && sortedArray[i - 1] == lessThan; i--);
            return i;
        }
    }
    

    【讨论】:

      【解决方案5】:

      他们希望您使用Array.BinarySearch 方法获得 100%。

      using System;
      
      public class SortedSearch
          {
          public static int CountNumbers(int[] sortedArray, int lessThan)
          {
              int lengthOfArray = sortedArray.Length;
              if (lengthOfArray == 0) return 0;
      
              if (sortedArray[0] >= lessThan) return 0;
      
              if (sortedArray[lengthOfArray - 1] < lessThan) return lengthOfArray;
      
              int index = Array.BinarySearch(sortedArray, lessThan);
              if (index < 0)
                  return ~index;
      
              return index;
          }
      
          public static void Main(string[] args)
          {
              Console.WriteLine(SortedSearch.CountNumbers(new int[] { 1, 3, 5, 7 }, 4));
          }
      }
      

      【讨论】:

        【解决方案6】:
        public class SortedSearch {
            public static int countNumbers(int[] sortedArray, int lessThan) {
                   int end = sortedArray.length-1;
                int start = 0;
                int mid =0;
        
                while (start<=end){
                    mid = (start+end)/2;
                    if(sortedArray[mid] <lessThan){
                        if(mid<sortedArray.length-1 && sortedArray[mid+1]<lessThan){
                          start = mid+1;
                          continue;
                        }else{
                            return mid+1;
                        }
                    }else{
                        end = mid-1;
                    }
        
                }
        return 0;
            }
            
            
        

        【讨论】:

          猜你喜欢
          • 2011-05-20
          • 1970-01-01
          • 2022-10-08
          • 2016-02-13
          • 2021-05-30
          • 1970-01-01
          • 1970-01-01
          • 2013-02-26
          • 1970-01-01
          相关资源
          最近更新 更多