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;
}
相关推荐
pride.li5 小时前
第03章 Python 教程-基础语法:注释与变量
服务器·前端·后端·python·算法·计算机视觉
旺仔仔仔6 小时前
机器人项目实战(5):从单次抓取,到接收指令连续做事
算法
旺仔仔仔6 小时前
机器人项目实战(2):识别物体、提取点云,再到抓取姿态
算法
姜鱼问生6 小时前
密码明明一样,为什么 GPG 说“错的“?
后端·算法·架构
启观川6 小时前
数据结构与算法-第 3 章 常用算法-贪心算法
数据结构·笔记·python·算法·贪心算法
对空六课6 小时前
指标口径生效时间的版本查询实现
大数据·服务器·前端·算法·数据分析
对空六课6 小时前
RFM分层边界变化的回溯计算
大数据·服务器·前端·数据库·人工智能·算法·数据分析
一勺思维7 小时前
我为什么把 Agent 的核心逻辑从 LangChain 里搬出来手写
算法
水饺编程7 小时前
第2章:Linux 基础,clear 命令
linux·c语言·汇编