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;
}
相关推荐
小飞在努力12 分钟前
【实现负载均匀在线判题③】从零散模块到统一,编译运行全链路封装
c语言·开发语言·c++·笔记
民乐团扒谱机4 小时前
【超详细】基频音高提取算法之SWIPE 算法深度解析:频谱匹配的精度极限与工程边界
人工智能·算法
我是码代码4 小时前
高分辨率ADC布局详解:走线压降、回流路径与地平面处理
linux·c语言·嵌入式
饮水桶_T4 小时前
C++ stack & queue复习笔记
开发语言·笔记·算法
炸膛坦客4 小时前
单片机/C/C++八股:(三十四)C 语言函数参数入栈顺序
c语言·开发语言·c++
wy_hhxx5 小时前
[Leetcode] 滑动窗口
算法·leetcode·滑动窗口
码途行者5 小时前
每天10分钟学Go:第20课 泛型
c++·算法·golang
AIgorithmGEEK7 小时前
算法·哈希表|从“逐个比”到“按 key 算位置”
数据结构·c++·算法·leetcode·hash
风落青芜9 小时前
排序算法(二):选择排序、堆排序与冒泡排序
c语言·开发语言·数据结构·算法·排序算法