Run code not working or I'm missing something obvious?


  • 0
    A

    I'm trying to just test out this platform. I implemented fizzbuzz in c++:

    class Solution {
    public:
        vector<string> fizzBuzz(int n) {
            
            vector<string> f(n);
    
            for(unsigned a = 1; a <= n; ++a ) {
    
                string s = "";
                if(a % 3==0){
                    s+="Fizz";
                }
                if(a % 5 ==0){
                    s+="Buzz";
                }
                if(a % 3 != 0 && a % 5 !=0){
                    s+=to_string(a);
                }
    
                f[a] = s;
    
                cout << f[a] << endl;
    
            }
    
            return f;
        }
    };
    

    And it works locally but when I run it here I get no output. What am I missing?

    https://leetcode.com/problems/fizz-buzz/


  • 0

    @Azurous I just tested the platform with your code by Run Code, which turned out that your code just encountered Runtime Error 0_1481354088725_Screen Shot 2016-12-10 at 15.14.25.png


  • 0
    A

    @LHearen right, that runtime error is what I'm asking about..


  • 0

    @Azurous Then it's your duty to find out the exact problem, right? Since your local test is really limited, that's why we need a judging system.


  • 0

    @Azurous You are getting segmentation fault due to array out of bound indexing. When n = 1, your array f has been initialized with size = 1. But you are accessing f[1] inside the for loop, which causes the runtime error. Hope this helps.


Log in to reply
 

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