Does LeetCode OJ allow us to use Global Variables while we're programming in C ?


  • 1
    B
    int countPrimes(int n) {
        if (n < 2)
            return 0;
        bool tmp[n];
        int i, j, res;
    
        for (i = 0; i < n; ++i)
            tmp[i] = 1;
        tmp[0] = tmp[1] = 0;
    
        for (i = 2; i < n; ++i){
            if (tmp[i]){
                for (j = 2 * i; j < n; j += i)
                    tmp[j] = 0;
            }
        }
    
        res = 0;
        for (i = 2; i < n; ++i){
            if (tmp[i])
                ++res;
        }
    
        return res;
    
    }
    

    For example,in this function, if we can use a global array to preserve the prime numbers, we can get
    this program run more fast! Becasue we can just search in this array and do not need to calculate
    primes in every test .


  • -1
    Y
    This post is deleted!

  • 0

    Are you really sure you're the author of that text and didn't steal it from the FAQ?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.