LeetCode //C - 1291. Sequential Digits

1291. Sequential Digits

An integer has sequential digits if and only if each digit in the number is one more than the previous digit.

Return a sorted list of all the integers in the range low, high inclusive that have sequential digits.

Example 1:

Input: low = 100, high = 300

Output: 123,234

Example 2:

Input: low = 1000, high = 13000

Output: 1234,2345,3456,4567,5678,6789,12345

Constraints:
  • 10 <= low <= high <= 10^9

From: LeetCode

Link: 1291. Sequential Digits


Solution:

Ideas:

Generate every sequential-digit number by choosing its length and starting digit, then keep those within low, high.

Code:
c 复制代码
#include <stdlib.h>

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* sequentialDigits(int low, int high, int* returnSize) {
    int* result = (int*)malloc(36 * sizeof(int));
    *returnSize = 0;

    for (int length = 2; length <= 9; length++) {
        for (int start = 1; start + length - 1 <= 9; start++) {
            int number = 0;

            for (int digit = start; digit < start + length; digit++) {
                number = number * 10 + digit;
            }

            if (number >= low && number <= high) {
                result[(*returnSize)++] = number;
            }
        }
    }

    return result;
}
相关推荐
微尘hjx9 小时前
【Deep-OC-SORT 03】多轨迹算法Deep-OC-SORT-卡尔曼滤波预测过程
算法·yolo·目标跟踪·ultralytics·车跟踪·轨迹算法·deep-oc-sort
老师我太想进步了202611 小时前
【数据结构-3】算法美学之降复杂度
数据结构·算法
机器人零零壹16 小时前
技术白皮书:iRobotCAM如何解决机器人激光切割应用中的核心难点,从CAD离线编程到双目视觉定位与轨迹自适应提升复杂空间曲线切割的精度、效率与柔性化水平
人工智能·算法
布莱克60518 小时前
C/C++ 中变量的声明与定义:区别、联系与常见误区
c语言·开发语言·c++·声明·定义
ZhengEnCi19 小时前
大模型的分类到底怎么分:从基础模型到推理模型、从通用到专用的完整图谱
算法
取名真是20 小时前
Python 算法知识点从基础到进阶
开发语言·python·算法
罗西的思考20 小时前
【轻量化复刻大模型 / KIMI K3】KDA 模块交互式演示
人工智能·算法·机器学习
揽星河..21 小时前
【C语言】底层程序设计
c语言·开发语言
nm_zfyjt1 天前
信息流广告投放优化实践:线索成本评估模型与转化链路重构(家装建材案例)
算法·重构·内蒙古朋友圈广告服务商·内蒙古朋友圈获客
w_zero_one1 天前
算法--构造
java·数据结构·算法