LeetCode //C - 38. Count and Say Medium Topics Companies

38. Count and Say

The count-and-say sequence is a sequence of digit strings defined by the recursive formula:

  • countAndSay(1) = “1”
  • countAndSay(n) is the way you would “say” the digit string from countAndSay(n-1), which is then converted into a different digit string.

To determine how you “say” a digit string, split it into the minimal number of substrings such that each substring contains exactly one unique digit. Then for each substring, say the number of digits, then say the digit. Finally, concatenate every said digit.

For example, the saying and conversion for digit string “3322251”:
在这里插入图片描述
Given a positive integer n, return the n t h n^{th} nth term of the count-and-say sequence.
 

Example 1:

Input: n = 1
Output: “1”
Explanation: This is the base case.

Example 2:

Input: n = 4
Output: “1211”
Explanation:
countAndSay(1) = “1”
countAndSay(2) = say “1” = one 1 = “11”
countAndSay(3) = say “11” = two 1’s = “21”
countAndSay(4) = say “21” = one 2 + one 1 = “12” + “11” = “1211”

Constraints:
  • 1 <= n <= 30

From: LeetCode
Link: 38. Count and Say


Solution:

Ideas:
  1. Base Case: If n is 1, the function returns the string “1”, since the first term of the sequence is always “1”.
  2. Recursive Call: For n greater than 1, the function calls itself to calculate the (n-1)th term. This is because to say the nth term, you need to know the (n-1)th term first.
  3. Calculating the Length: It then calculates the length of the (n-1)th term to determine how much memory to allocate for the nth term. The allocation is generous to ensure there’s enough space since the length of the sequence can grow with each term. The malloc function is used to allocate the memory, and the sprintf function is used to convert the counts and digits into a string format.
  4. Building the nth Term: The function iterates through the digits of the (n-1)th term. For each group of the same digit, it counts how many times that digit appears consecutively (count). It then writes the count and the digit itself into the result string. The sprintf function returns the number of characters written (excluding the null terminator), which is used to update the result_index to know where to write the next characters.
  5. Ending the String: Once all groups of digits have been processed, a null terminator (‘\0’) is added to the end of the result string to properly terminate it.
  6. Memory Management: The function then frees the memory allocated for the (n-1)th term since it is no longer needed. This is important to prevent memory leaks.
  7. Return Result: Finally, the nth term, now stored in result, is returned to the caller. The caller, in this case, the main function, is responsible for freeing this memory after it’s done using it.
Code:
char* countAndSay(int n) {if(n == 1) return strdup("1");// Recursively call countAndSay to get the previous termchar* prev_term = countAndSay(n - 1);int length = strlen(prev_term);// Calculate the maximum length of the result// In the worst case, the length doubles (e.g., "1" -> "11")char* result = malloc(2 * length + 1);int result_index = 0;for(int i = 0; i < length; i++) {int count = 1;// Count the number of identical digitswhile(i + 1 < length && prev_term[i] == prev_term[i + 1]) {count++;i++;}// Append count and digit to the result stringresult_index += sprintf(result + result_index, "%d%c", count, prev_term[i]);}// Free the memory allocated for previous termfree(prev_term);// Add the null terminator to the result stringresult[result_index] = '\0';return result;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/828891.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端开发禁用F12和右键检查元素处理

只需要对应的页面引入西面这个js文件就行(创建一个.js文件,将下面代码粘贴进去页面引用后使用) window.onload function () {document.onkeydown function () {var e window.event || arguments[0];//屏蔽F12if (e.keyCode 123) {return false;//屏蔽CtrlShiftI} else if …

【ARMv9 DSU-120 系列 10 -- PMU 详细介绍】

请阅读【Arm DynamIQ™ Shared Unit-120 专栏 】 文章目录 DSU-120 PMUPMU features事件接口系统寄存器计数器PMU寄存器接口PMU eventsPMU interruptExternal cluster PMU registersDSU-120 PMU DynamIQ™共享单元-120(DSU-120)包括性能监视器,这些监视器使您能够在运行时收…

Laravel 6 - 第十七章 配置数据库

​ 文章目录 Laravel 6 - 第一章 简介 Laravel 6 - 第二章 项目搭建 Laravel 6 - 第三章 文件夹结构 Laravel 6 - 第四章 生命周期 Laravel 6 - 第五章 控制反转和依赖注入 Laravel 6 - 第六章 服务容器 Laravel 6 - 第七章 服务提供者 Laravel 6 - 第八章 门面 Laravel 6 - …

《动手学深度学习(Pytorch版)》Task01:初识深度学习——4.22打卡

《动手学深度学习&#xff08;Pytorch版&#xff09;》Task01&#xff1a;初识深度学习 深度学习介绍AI地图深度学习任务图片分类物体检测和分割样式迁移人脸合成文字生成图片文字生成无人驾驶 案例&#xff1a;广告点击完整过程 QAQ&#xff1a;机器学习的可解释性&#xff1a…

electron使用typescript

引入 TypeScript 到 Electron 项目中是一个增强代码质量和开发体验的好方法&#xff0c;因为 TypeScript 提供了静态类型检查、接口和类等强大的语言特性。下面是将 TypeScript 集成到 Electron 项目中的步骤&#xff1a; 1. 初始化项目 如果你还没有创建 Electron 项目&…

【C++ STL序列容器】list 双向链表

文章目录 【 1. 基本原理 】【 2. list 的创建 】2.1 创建1个空的 list2.2 创建一个包含 n 个元素的 list&#xff08;默认值&#xff09;2.3 创建一个包含 n 个元素的 list&#xff08;赋初值&#xff09;2.4 通过1个 list 初始化另一个 list2.5 拷贝其他类型容器的指定元素创…

速盾:ddos高防ip原理

DDoS&#xff08;分布式拒绝服务攻击&#xff09;是一种常见的网络攻击方式&#xff0c;通过向目标服务器发送大量的请求&#xff0c;使其无法正常处理合法用户的请求&#xff0c;从而导致服务不可用。为了应对这种攻击&#xff0c;高防IP技术应运而生。 高防IP是一种专门为抵…

oracle--merge into :匹配则更新不匹配则插入

merge into &#xff1a;匹配则更新不匹配则插入 --语法 merge into 目标表 using &#xff08;增量&#xff09; on (匹配字段&#xff09; where matched then update set --update和sel直接不需要加表名 when not matched then insert values--insert和values之间不需要加i…

swagger文档接口根据包分组配置

文章目录 一、引言二、配置2.1 原本配置及效果2.2 更改后配置及效果 三、结束 一、引言 关于接口文档的详细配置可参见文章API文档生成工具-----Knife4j的详细介绍、配置及应用 此文章是基于已经完成基础配置的前提下,如何根据不同包进行分组 二、配置 2.1 原本配置及效果 …

Hadoop实战——MapReduce-字符统计(超详细教学,算法分析)

目录 一、前提准备工作 启动hadoop集群 二、实验过程 1.虚拟机安装先设置端口转发 2.上传对应文件 3.编写Java应用程序 4. 编译打包程序 5. 运行程序 三、算法设计和分析 算法设计 算法分析 四、实验总结 实验目的&#xff1a;给定一份英文文本&#xff0c;统计每个…

matlab新手快速上手5(蚁群算法)

本文根据一个较为简单的蚁群算法框架详细分析蚁群算法的实现过程&#xff0c;对matlab新手友好&#xff0c;源码在文末给出。 蚁群算法简介&#xff1a; 蚁群算法是一种启发式优化算法&#xff0c;灵感来源于观察蚂蚁寻找食物的行为。在这个算法中&#xff0c;解决方案被看作是…

数学分析复习:中值定理、反函数定理

文章目录 中值定理、反函数定理 本篇文章适合个人复习翻阅&#xff0c;不建议新手入门使用 中值定理、反函数定理 定理&#xff1a;Rolle&#xff08;罗尔&#xff09;中值定理 设实值函数 f ∈ C 0 [ a , b ] f\in C^0[a,b] f∈C0[a,b] 且在 ( a , b ) (a,b) (a,b) 上可微&…

平衡二叉树、红黑树、B树、B+树

Tree 1、前言2、平衡二叉树和红黑树3、B树和B树3.1、B树的构建3.2、B树和B树的区别3.3、数据的存储方式 1、前言 本文侧重在理论方面对平衡二叉树、红黑树、B树和B树的各方面性能进行比较。不涉及编程方面的实现。而关于于平衡二叉树在C中的实现&#xff0c;我的上一篇文章平衡…

Golang实现一个批量自动化执行树莓派指令的软件(4)上传

简介 话接上篇 Golang实现一个批量自动化执行树莓派指令的软件(3)下载 &#xff0c; 继续实现上传 环境描述 运行环境: Windows&#xff0c; 基于Golang&#xff0c; 暂时没有使用什么不可跨平台接口&#xff0c; 理论上支持Linux/MacOS 目标终端&#xff1a;树莓派DebianOS(主…

【JS】前端文件读取FileReader操作总结

前言&#xff1a;开发中经常遇到文件上传的场景&#xff0c;有时需要前端将文件内容读取出来再以base64格式传到接口。 目录 FileReader主要方法readAsArrayBuffer(blob)readAsText(blob, [encoding])readAsDataURL(blob) 主要事件React antd Upload 组件示例 FileReader Fil…

Vue Router基础知识整理

Vue Router基础知识整理 1. 安装与使用&#xff08;Vue3&#xff09;安装使用 2. 配置路径别名和VSCode路径提示&#xff08;了解&#xff09;3. 使用查询字符串或路径传参query动态路由 与 params 4. router-link、定义别名、定义路由名称、编程式导航定义别名 aliasrouter-li…

李沐66_使用注意力机制的seq2seq——自学笔记

加入注意力 1.编码器对每次词的输出作为key和value 2.解码器RNN对上一个词的输出是query 3.注意力的输出和下一个词的词嵌入合并进入RNN 一个带有Bahdanau注意力的循环神经网络编码器-解码器模型 总结 1.seq2seq通过隐状态在编码器和解码器中传递信息 2.注意力机制可以根…

ELK技术介绍:背景、功能及应用场景全面解析

一、ELK概述 ELK是由Elasticsearch、Logstash和Kibana三个开源软件组成的日志管理解决方案&#xff0c;这一组合在近年来得到了广泛的关注和应用。ELK的出现&#xff0c;源于大数据和云计算技术的快速发展&#xff0c;以及对高效日志管理的迫切需求。 随着企业信息化程度…

【10-10-10旁观思维】项目管理必会的思维分析工具 08(送模板~)

&#x1f468;‍&#x1f4bb;&#x1f469;‍&#x1f4bb;面对一个决策或选择&#xff0c;当你犹豫不决时&#xff0c;可以想一下 ⏰10分钟后&#xff0c;自己是怎么看待自己现在的决策&#xff0c;依然保持一致亦或会后悔&#xff1b; ⏰10个月后&#xff0c;你又会如何思…

Javascript 插值搜索与二分搜索

插值搜索和二分搜索都是在有序数组中查找目标元素的算法。它们之间的核心区别在于确定中间元素的方式。 1、二分搜索&#xff08;Binary Search&#xff09;&#xff1a;二分搜索是一种通过将目标值与数组中间元素进行比较&#xff0c;然后根据比较结果缩小搜索范围的算…