c - 递归算法计算小于给定值的节点
问题描述
你好,我正在准备期末考试,所以从我之前的考试中,我得到了这个问题的部分学分。递归算法,以便计算给定链表中有多少节点的信息值小于给定阈值
typedef struct LLNode {
int info;
struct LLNode* pNext;
}LLNode;
int recCount(LLNode *front, int threshold)
{
}
我的回答是
int count = 0;
int total_less;
if(front == NULL)
return 0 ;
if(count < threshold)
count = 1 + recCount(front->next, front->info);
total_less++;
return total_
解决方案
较短的版本:
如果 value 较小,则将 +1 添加到结果并检查列表中的下一个节点,否则只检查下一个节点而不添加到计数。
int recCount(struct NODE *N, int value)
{
if(N == NULL)
return 0;
if(N->data < value)
return 1 + recCount(N->next, value);
return recCount(N->next, value);
}
示例代码: http ://tpcg.io/36qFkO
推荐阅读
- java - javafx中的线程
- sql - SQL - 根据 2 个日期之间的天数从 1 个记录创建多个记录
- python - 在 conda 虚拟环境中以 pip 兼容格式创建 requirements.txt
- javascript - Bookshelf.js fetchAll() 性能
- r - 如何更正箱线图的标签获取 R 中每对的 p 值
- scala - Scala递归api调用
- excel - 使用 MSHTML.HTMLDocument 时强制 documentMode
- r - knitr 中的数字大小与拼凑或牛图
- amazon-web-services - AWS 生命周期管理器将其作为备份的快照保存在哪里
- powershell - 在扫描 TCP 和 UDP 端口脚本中缺少成员的 psobjects 数组