LeetCode //C - 1286. Iterator for Combination

1286. Iterator for Combination

Design the CombinationIterator class:

  • CombinationIterator(string characters, int combinationLength) Initializes the object with a string characters of sorted distinct lowercase English letters and a number combinationLength as arguments.
  • next() Returns the next combination of length combinationLength in lexicographical order.
  • hasNext() Returns true if and only if there exists a next combination.
Example 1:

Input:

"CombinationIterator", "next", "hasNext", "next", "hasNext", "next", "hasNext"

\["abc", 2\], \[\], \[\], \[\], \[\], \[\], \[\]

Output:

null, "ab", true, "ac", true, "bc", false

Explanation:

CombinationIterator itr = new CombinationIterator("abc", 2);

itr.next(); // return "ab"

itr.hasNext(); // return True

itr.next(); // return "ac"

itr.hasNext(); // return True

itr.next(); // return "bc"

itr.hasNext(); // return False

Constraints:
  • 1 <= combinationLength <= characters.length <= 15
  • All the characters of characters are unique.
  • At most 10 4 10^4 104 calls will be made to next and hasNext.
  • It is guaranteed that all calls of the function next are valid.

From: LeetCode

Link: 1286. Iterator for Combination


Solution:

Ideas:

Store the current character indices, then increment the rightmost index that can still move forward.

Code:
c 复制代码
typedef struct {
    char* characters;
    int* indices;
    int n;
    int k;
    bool hasNext;
} CombinationIterator;


CombinationIterator* combinationIteratorCreate(char* characters, int combinationLength) {
    CombinationIterator* obj =
        (CombinationIterator*)malloc(sizeof(CombinationIterator));

    obj->n = strlen(characters);
    obj->k = combinationLength;
    obj->hasNext = true;

    obj->characters = (char*)malloc((obj->n + 1) * sizeof(char));
    strcpy(obj->characters, characters);

    obj->indices = (int*)malloc(obj->k * sizeof(int));

    for (int i = 0; i < obj->k; i++) {
        obj->indices[i] = i;
    }

    return obj;
}


char* combinationIteratorNext(CombinationIterator* obj) {
    char* result = (char*)malloc((obj->k + 1) * sizeof(char));

    for (int i = 0; i < obj->k; i++) {
        result[i] = obj->characters[obj->indices[i]];
    }
    result[obj->k] = '\0';

    int i = obj->k - 1;

    while (i >= 0 &&
           obj->indices[i] == obj->n - obj->k + i) {
        i--;
    }

    if (i < 0) {
        obj->hasNext = false;
    } else {
        obj->indices[i]++;

        for (int j = i + 1; j < obj->k; j++) {
            obj->indices[j] = obj->indices[j - 1] + 1;
        }
    }

    return result;
}


bool combinationIteratorHasNext(CombinationIterator* obj) {
    return obj->hasNext;
}


void combinationIteratorFree(CombinationIterator* obj) {
    free(obj->characters);
    free(obj->indices);
    free(obj);
}
相关推荐
forest_0911 小时前
《AI 编程 Harness 实战手册》04|审批这道门:点了一百次“允许”,还剩多少作用?
人工智能·算法·prompt·agi
H.莓飛11 小时前
【C++】【STL】stack_OJ题
服务器·c语言·开发语言·数据结构·c++·算法·visual studio
不会武功不懂江湖13 小时前
RISC-V链接器与链接脚本
汇编·算法·risc-v
Zwawa14 小时前
卡尔曼到底在算什么:从一维到它的变式以及工程版卡尔曼滤波算法C语言实现
c语言·算法·嵌入式
电科_银尘14 小时前
【大模型算法与工程 14】Tokenizer与Embedding:文本如何变成模型输入
算法·embedding
饮水桶_T15 小时前
Python 面向对象(十七)|继承、封装、多态 笔记
数据结构·笔记·算法
文丑颜不良啊17 小时前
LeetCode3281~3300题目解析
java·leetcode
地平线开发者18 小时前
让 AI Agent 和 征程6 台架高效、稳定、安全地协作
算法·安全·自动驾驶
kyle~19 小时前
SHA-256 哈希去重
算法·哈希算法