无法在 LeetCode 质数挑战中找到我的程序的修复程序

Cant find the fix for my program at LeetCode Challenge of PrimeNumbers

我已经尝试了几种方法来更改我在 LeetCode 中的代码,但我找不到解决方法,挑战是下一个:

<<统计小于非负数n的素数个数。

示例:

输入:10 输出:4 解释:有4个小于10的素数,分别是2,3,5,7>>

我的提案代码是下一个:

 import java.util.Scanner;
    class Solution {
        public int countPrimes(int n) {
        Scanner sc = new Scanner(System.in);
        int sum = 0;
        int cont = 0;
        int prime = 0;
        prime = sc.nextInt();
        int a[] = new int [prime];
        for(int i = 0; i < a.length; i++) {
            a[i] = i;
            cont = 0;
            
            for(int y = 1; y< a.length; y++) {
                if(a[i] % y == 0) {
                    cont ++;
                }
            }
            if (cont == 2) {
                sum ++;
            }
        }
        return sum;
    }
}

同时错误标记如下:

Submission Result: Compile Error More Details 
Line 7: error: cannot find symbol [in __Driver__.java] int ret = new Solution().countPrimes(param_1); ^ symbol: method countPrimes(int) location: class Solution
Run Code Status: Runtime Error
×
Run Code Result:
Your input
10
Your answer
java.util.NoSuchElementException
  at line 937, java.base/java.util.Scanner.throwFor
  at line 1594, java.base/java.util.Scanner.next
  at line 2258, java.base/java.util.Scanner.nextInt
  at line 2212, java.base/java.util.Scanner.nextInt
  at line 8, Solution.countPrimes
  at line 54, __DriverSolution__.__helper__
  at line 84, __Driver__.main
Show Diff
Runtime: N/A

请帮忙!

您对 input/output 部分感到困惑:您不需要任何扫描仪来执行此操作,只需:

 class Solution 
 {
      public static int countPrimes(int n) 
       {
          int sum = 0;
           int a[] = new int [n];
           for(int i = 0; i < a.length; i++) {
                a[i] = i;
                int  cont = 0;
            
                for(int y = 1; y< a.length; y++) {
                    if(a[i] % y == 0) {
                       cont++;
                     }
                 }
          if (cont == 2) {
              sum++;
          }
        }
       return sum; //this is the output
    }
        
     public static void main(String args[])
    ´{
        countPrimes(10); //this is the input
     }
}

证明:


瞧瞧。 LeetCode 接受输入 (10) 和输出 (4)。这就是您所需要的:)

您的答案得到一个 scanner 不需要的对象,因此您可以将其删除。

您还创建了一个 array,这会降低性能,我建议您不要使用它,因为您不需要显示质数,而只需跟踪它们。

public static int countPrimes(int n) {

    int sum=0;

    for(int i = n; i > 1; i-- ){
        
        int count = 0; //Keep track of the number of primes.
        
        for(int j = 2; j < i; j++){
            if(i % j == 0){
                count++;
            }
        }
        
        if(count==0) {
            sum++;
        }
      
    }

    return sum;

    }

这也将通过:

public class Solution {
    public static final int countPrimes(int n) {
        // mapping for if the number is divisible by prime numbers, which would make that number a composite number
        boolean[] notPrime = new boolean[n];
        // counting prime numbers
        int count = 0;

        for (int i = 2; i < n; i++) {
            // If the index of notPrime would be false, we have a prime number, we go through the if, otherwise we continue
            if (notPrime[i] == false) {
                // Increment the number of prime numbers
                count++;

                // Look into future numbers
                for (int j = 2; i * j < n; j++) {
                    // find composite numbers and set their indices to true
                    notPrime[i * j] = true;
                }
            }
        }

        return count;
    }
}

参考资料