C++ solution, 3ms


  • 7
    Z
    class Solution {
    public:
        vector<string> fizzBuzz(int n) {
            vector<string> ret_vec(n);
            for(int i=1; i<=n; ++i)
            {
                if(i%3 == 0)
                {
                    ret_vec[i-1] += "Fizz";
                }
                if(i%5 == 0)
                {
                    ret_vec[i-1] += "Buzz";
                }
                if(!ret_vec[i-1].size())
                {
                    ret_vec[i-1] += to_string(i);
                }
            }
            return ret_vec;
        }
    };
    

  • 0
    N

    The same idea. Java Version.

    public class Solution {
        public List<String> fizzBuzz(int n) {
            List<String> res = new ArrayList<>();
            for(int i = 1; i <= n ; i++)
            {   
                StringBuilder cur = new StringBuilder();
                if(i % 3 == 0)  cur.append("Fizz");
                if(i % 5 == 0)  cur.append("Buzz");
                if(cur.length() == 0)    cur.append(String.valueOf(i));
                res.add(cur.toString());
            }
            return res;
        }
    }
    

  • -2
    A

    Your solution is shown as 3ms Top Solution. Mine also reports as 3ms but on the graph it is reported as "better than 59%". When I run my test below I see that mine runs 20% faster than yours. I see similar experience with other tasks. This web site needs considerably better performance comparison model.

    class Solution
    {
    public:
        vector<string> fizzBuzz(int n)
        {
            static const char DigitChars[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    
            vector<string> r;
            r.resize(n);
    
            for (int i = 3; i <= n; i += 3)
                r[i - 1] = "Fizz";
    
            for (int i = 5; i <= n; i += 5)
                r[i - 1].append("Buzz");
    
            int digits[12];
            digits[0] = 0;
            int maxDigits = 1;
    
            for (auto& s : r)
            {
                int carry = 1;
                for (int i = 0; i < maxDigits; ++i)
                {
                    if (!carry)
                        break;
    
                    digits[i] += carry;
                    if (digits[i] == 10)
                    {
                        digits[i] = 0;
                        carry = 1;
                    }
                    else
                    {
                        carry = 0;
                    }
                }
    
                if (carry)
                    digits[maxDigits++] = 1;
    
                if (s.empty())
                {
                    for (int i = maxDigits - 1; i >= 0; --i)
                        s.append(1, DigitChars[digits[i]]);
                }
            }
    
            return r;
        }
    };
    
    // Perf test
    int main()
    {
        Solution s;
    
        int sum = 0;
        for (int t = 0; t < 1000; ++t)
        {
            sum += s.fizzBuzz(100000).size();
        }
    
        printf("%d", sum);
        return 0;
    }
    

  • 0
    A

    And a more compact version...

    class Solution
    {
    public:
        vector<string> fizzBuzz(int n)
        {
            static const char DigitChars[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    
            vector<string> r;
            r.resize(n);
    
            for (int i = 3; i <= n; i += 3)
                r[i - 1] = "Fizz";
    
            for (int i = 5; i <= n; i += 5)
                r[i - 1].append("Buzz");
    
            int digits[12];
            digits[0] = 0;
            int maxDigits = 1;
    
            for (auto& s : r)
            {
                bool carry = true;
                for (int i = 0; carry && i < maxDigits; ++i)
                {
                    if (++digits[i] == 10)
                    {
                        digits[i] = 0;
                        carry = true;
                    }
                    else
                    {
                        carry = false;
                    }
                }
    
                if (carry)
                    digits[maxDigits++] = 1;
    
                if (s.empty())
                {
                    for (int i = maxDigits - 1; i >= 0; --i)
                        s.append(1, DigitChars[digits[i]]);
                }
            }
    
            return r;
        }
    };
    

Log in to reply
 

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