Beats 100% || 3 Approaches || Full Explained  || [Java/C++/Python/JavaScript]
Approach\n1. List Initialization: \nThe code starts by creating an ArrayList named ans to store the result of FizzBuzz for numbers from 1 to N.\n\n2. Loop Throu
# Approach\n1. **List Initialization:** \nThe code starts by creating an ArrayList named ans to store the result of FizzBuzz for numbers from 1 to N.\n\n2. **Loop Through Numbers:**\nThe for loop iterates from 1 to n (inclusive), representing the numbers for which FizzBuzz needs to be computed.\n\n3. **Conditions for Fizz, Buzz, and FizzBuzz:**\n- The code checks if the current number is a multiple of both 3 and 5 (i % 3 == 0 && i % 5 == 0). If true, "FizzBuzz" is added to the result list.\n- If not, it checks if the current number is a multiple of 3 (i % 3 == 0). If true, "Fizz" is added to the result list.\n- Similarly, it checks if the current number is a multiple of 5 (i % 5 == 0). If true, "Buzz" is added to the result list.\n- Default Case: If the current number is not a multiple of 3 or 5, it adds the string representation of the number to the result list using Integer.toString(i).\n\n5. **Return Result List:** \n- The final result, which is a list of strings representing the FizzBuzz output for numbers from 1 to N, is returned.\n\n# Code using if else\n```Java []\nclass Solution {\n    public List<String> fizzBuzz(int n) {\n        List<String> ans = new ArrayList<>();\n\n        for(int i=1; i<=n; i++) {\n            if(i%3 ==0 && i%5==0) {\n                ans.add("FizzBuzz");\n            }\n            else if(i%3==0) {\n                ans.add("Fizz");\n            }\n            else if(i%5==0) {\n                ans.add("Buzz");\n            }\n            else {\n                ans.add(Integer.toString(i));\n            }\n        }\n        return ans;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    vector<string> fizzBuzz(int n) {\n        vector<string> ans;\n\n        for (int i = 1; i <= n; i++) {\n            if (i % 3 == 0 && i % 5 == 0) {\n                ans.push_back("FizzBuzz");\n            } else if (i % 3 == 0) {\n                ans.push_back("Fizz");\n            } else if (i % 5 == 0) {\n                ans.push_back("Buzz");\n            } else {\n                ans.push_back(to_string(i));\n            }\n        }\n\n        return ans;\n    }\n};\n```\n```Python3 []\nclass Solution:\n    def fizzBuzz(self, n: int) -> List[str]:\n        ans = []\n\n        for i in range(1, n+1):\n            if i % 3 == 0 and i % 5 == 0:\n                ans.append("FizzBuzz")\n            elif i % 3 == 0:\n                ans.append("Fizz")\n            elif i % 5 == 0:\n                ans.append("Buzz")\n            else:\n                ans.append(str(i))\n\n        return ans\n```\n```JavaScript []\n/**\n * @param {number} n\n * @return {string[]}\n */\nvar fizzBuzz = function(n) {\n    var ans = [];\n\n    for (var i = 1; i <= n; i++) {\n        if (i % 3 === 0 && i % 5 === 0) {\n            ans.push("FizzBuzz");\n        } else if (i % 3 === 0) {\n            ans.push("Fizz");\n        } else if (i % 5 === 0) {\n            ans.push("Buzz");\n        } else {\n            ans.push(i.toString());\n        }\n    }\n\n    return ans;\n};\n```\n\n# No % :\n```Java []\nclass Solution {\n    public List<String> fizzBuzz(int n) {\n        List<String> res = new ArrayList<>();\n        int i = 1, fizz = 0, buzz = 0;\n        while (i <= n){\n            fizz++; buzz++;\n            if (fizz == 3 && buzz == 5) {\n                res.add("FizzBuzz");\n                fizz = buzz = 0;\n            } else if (fizz == 3) {\n                res.add("Fizz");\n                fizz = 0;\n            } else if (buzz == 5) {\n                res.add("Buzz");\n                buzz = 0;\n            } else {\n                res.add(String.valueOf(i));\n            }\n            i++;\n        }\n\n        return res;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    std::vector<std::string> fizzBuzz(int n) {\n        std::vector<std::string> res;\n        int i = 1, fizz = 0, buzz = 0;\n\n        while (i <= n) {\n            fizz++;\n            buzz++;\n\n            if (fizz == 3 && buzz == 5) {\n                res.push_back("FizzBuzz");\n                fizz = buzz = 0;\n            } else if (fizz == 3) {\n                res.push_back("Fizz");\n                fizz = 0;\n            } else if (buzz == 5) {\n                res.push_back("Buzz");\n                buzz = 0;\n            } else {\n                res.push_back(std::to_string(i));\n            }\n\n            i++;\n        }\n\n        return res;\n    }\n};\n```\n```Python3 []\nclass Solution:\n    def fizzBuzz(self, n: int) -> List[str]:\n        res = []\n        i, fizz, buzz = 1, 0, 0\n\n        while i <= n:\n            fizz += 1\n            buzz += 1\n\n            if fizz == 3 and buzz == 5:\n                res.append("FizzBuzz")\n                fizz = buzz = 0\n            elif fizz == 3:\n                res.append("Fizz")\n                fizz = 0\n            elif buzz == 5:\n                res.append("Buzz")\n                buzz = 0\n            else:\n                res.append(str(i))\n\n            i += 1\n\n        return res\n```\n```JavaScript []\n/**\n * @param {number} n\n * @return {string[]}\n */\nvar fizzBuzz = function(n) {\n    var res = [];\n    var i = 1, fizz = 0, buzz = 0;\n\n    while (i <= n) {\n        fizz++;\n        buzz++;\n\n        if (fizz === 3 && buzz === 5) {\n            res.push("FizzBuzz");\n            fizz = buzz = 0;\n        } else if (fizz === 3) {\n            res.push("Fizz");\n            fizz = 0;\n        } else if (buzz === 5) {\n            res.push("Buzz");\n            buzz = 0;\n        } else {\n            res.push(i.toString());\n        }\n\n        i++;\n    }\n\n    return res;\n};\n```\n\n\n# Ternairy operator:\n```Java []\nclass Solution {\n    public List fizzBuzz(int n) {\n        List ans = new ArrayList<>();\n        for(int i = 1; i <= n; i++){\n            ans.add(\n                i % 15 == 0 ? "FizzBuzz" :\n                i % 5 == 0  ? "Buzz" :\n                i % 3 == 0  ? "Fizz" :\n                String.valueOf(i)\n            );\n        }\n\n        return ans;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    vector<string> fizzBuzz(int n) {\n        vector<string> ans;\n\n        for (int i = 1; i <= n; i++) {\n            ans.push_back(\n                (i % 15 == 0) ? "FizzBuzz" :\n                (i % 5 == 0)  ? "Buzz" :\n                (i % 3 == 0)  ? "Fizz" :\n                to_string(i)\n            );\n        }\n\n        return ans;\n    }\n};\n```\n```Python []\nclass Solution:\n    def fizzBuzz(self, n: int):\n        ans = []\n        for i in range(1, n + 1):\n            ans.append(\n                "FizzBuzz" if i % 15 == 0 else\n                "Buzz" if i % 5 == 0 else\n                "Fizz" if i % 3 == 0 else\n                str(i)\n            )\n        return ans\n```\n```JavaScript []\n/**\n * @param {number} n\n * @return {string[]}\n */\nvar fizzBuzz = function(n) {\n    var ans = [];\n    for (var i = 1; i <= n; i++) {\n        ans.push(\n            (i % 15 === 0) ? "FizzBuzz" :\n            (i % 5 === 0) ? "Buzz" :\n            (i % 3 === 0) ? "Fizz" :\n            i.toString()\n        );\n    }\n    return ans;\n};\n```\n\n---\n\n\n---\n\n![5c63d377-8ef4-4beb-b09d-0edb07e09a41_1702955205.6568592.png](https://assets.leetcode.com/users/images/28cc1570-ac06-4021-a7bb-c97f40c4d7ef_1704360756.0077145.png)\n\n
['Python', 'C++', 'Java', 'Python3', 'JavaScript']