LeetCode //C - 412. Fizz Buzz

412. Fizz Buzz

Given an integer n, return a string array answer (1-indexed) where:

  • answeri == "FizzBuzz" if i is divisible by 3 and 5.
  • answeri == "Fizz" if i is divisible by 3.
  • answeri == "Buzz" if i is divisible by 5.
  • answeri == i (as a string) if none of the above conditions are true.
Example 1:

Input: n = 3
Output: "1","2","Fizz"

Example 2:

Input: n = 5
Output: "1","2","Fizz","4","Buzz"

Example 3:

Input: n = 15
Output:

"1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"

Constraints:
  • 1 < = n < = 1 0 4 1 <= n <= 10^4 1<=n<=104

From: LeetCode

Link: 412. Fizz Buzz


Solution:

Ideas:

1. Memory Allocation:

  • The function returns an array of strings, so we first allocate memory for the array result that will hold the strings.
  • Each string requires memory allocation separately. Since the maximum string length is 8 characters (for "FizzBuzz") plus 1 for the null terminator (\0), we allocate space for each string using malloc(9 * sizeof(char)).

2. FizzBuzz Logic:

  • For each number from 1 to n, we check:
    • If the number is divisible by both 3 and 5, we assign "FizzBuzz".
    • If it's divisible by only 3, we assign "Fizz".
    • If it's divisible by only 5, we assign "Buzz".
    • Otherwise, we convert the number to a string using snprintf and store it in the array.

3. Returning the Result:

  • The function returns the result array, and the size of the array (n) is returned through the returnSize pointer.
  • The main function demonstrates how to call this function and free the allocated memory properly.
Code:
c 复制代码
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
char** fizzBuzz(int n, int* returnSize) {
    *returnSize = n;
    char** result = (char**)malloc(n * sizeof(char*)); // Allocate memory for the result array

    for (int i = 1; i <= n; i++) {
        result[i - 1] = (char*)malloc(9 * sizeof(char)); // Allocate memory for each string (maximum length is 8 for "FizzBuzz" + 1 for '\0')

        if (i % 3 == 0 && i % 5 == 0) {
            strcpy(result[i - 1], "FizzBuzz");
        } else if (i % 3 == 0) {
            strcpy(result[i - 1], "Fizz");
        } else if (i % 5 == 0) {
            strcpy(result[i - 1], "Buzz");
        } else {
            snprintf(result[i - 1], 9, "%d", i); // Convert the integer to a string
        }
    }

    return result;
}
相关推荐
2601_951643776 分钟前
Python第一,Java跌出前三,C语言杀回来了
java·c语言·python·编程语言排行·技术趋势
AI科技星1 小时前
数术工坊 · 第四卷 橡皮泥江湖(拓扑学)【完整定稿】
c语言·开发语言·汇编·electron·概率论·拓扑学
一只齐刘海的猫2 小时前
【Leetcode】找到字符串中所有字母异位词
算法·leetcode·职场和发展
海清河晏1113 小时前
数据结构 | 八大排序
数据结构·算法·排序算法
AI科技星3 小时前
数术工坊第八卷:算力革命
c语言·开发语言·网络·量子计算·agi
IronMurphy4 小时前
【算法五十七】146. LRU 缓存
算法·缓存
凌波粒4 小时前
LeetCode--108.将有序数组转换为二叉搜索树(二叉树)
算法·leetcode·职场和发展
liulilittle4 小时前
KCC:在 BBR 思路上的一次探索
网络·tcp/ip·算法·bbr·通信·拥塞控制·kcc
浦信仿真大讲堂5 小时前
达索系统SIMULIA Abaqus 2026接触和约束的增强新功能介绍
人工智能·python·算法·仿真软件·达索软件
点云侠5 小时前
PCL 生成三棱锥点云
c++·算法·最小二乘法