【C语言刷LeetCode】604.迭代压缩字符串(EL)

Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext.

The given compressed string will be in the form of each letter followed by a positive integer representing the number of this letter existing in the original uncompressed string.

next() - if the original string still has uncompressed characters, return the next letter; Otherwise return a white space.
hasNext() - Judge whether there is any letter needs to be uncompressed.

Note:
Please remember to RESET your class variables declared in StringIterator, as static/class variables are persisted across multiple test cases. Please see here for more details.

Example:

StringIterator iterator = new StringIterator("L1e2t1C1o1d1e1");

iterator.next(); // return 'L'
iterator.next(); // return 'e'
iterator.next(); // return 'e'
iterator.next(); // return 't'
iterator.next(); // return 'C'
iterator.next(); // return 'o'
iterator.next(); // return 'd'
iterator.hasNext(); // return true
iterator.next(); // return 'e'
iterator.hasNext(); // return false
iterator.next(); // return ' '

1.字符判断,在'a','z','A','Z'之间的便是字符

2.获取数字:ret = ret * 10 + compressedStr[i] - '0';

3.数字的含义是有多少个,所以结构体中添加idx,cnt.

typedef struct {
    char strarr[8096];
    int numarr[8096];
    int idx;
    int cnt;
} CompressedStrIterator;

int Getnum(char* compressedStr, int i, int len) {
    long ret = 0;

    while((i < len) && (compressedStr[i] >= '0') && (compressedStr[i] <= '9')) {
        ret = ret * 10 + compressedStr[i] - '0';
        i++;
    }

    return ret;
}

CompressedStrIterator* CompressedStrIteratorCreate(char* compressedStr)
{
    CompressedStrIterator* obj = (CompressedStrIterator*)malloc(sizeof(CompressedStrIterator));
    int i;
    int len = strlen(compressedStr);
    int cnt = 0;

    memset(obj, 0, sizeof(CompressedStrIterator));

    for (i = 0; i < len; i++) {
        if (((compressedStr[i] >= 'a') && (compressedStr[i] <= 'z')) ||
            ((compressedStr[i] >= 'A') && (compressedStr[i] <= 'Z'))) {
            obj->strarr[cnt] = compressedStr[i];
            obj->numarr[cnt] = Getnum(compressedStr, i+1, len);
            cnt++;
        }
    }

    obj->idx = 0;
    obj->cnt = cnt;

    return obj;
}

char CompressedStrIteratorNext(CompressedStrIterator* obj)
{
    char onech = ' ';

    if (obj->numarr[obj->idx] == 0) { 
        if (obj->idx < obj->cnt) {
            obj->idx = obj->idx + 1;
        }
    }

    if (obj->idx == obj->cnt) {
        return onech;
    }

    onech = obj->strarr[obj->idx];
    obj->numarr[obj->idx] = obj->numarr[obj->idx] - 1;

    return onech;
}

bool CompressedStrIteratorHasNext(CompressedStrIterator* obj)
{
    bool flag;

    if (((obj->idx == obj->cnt -1) && (obj->numarr[obj->idx] == 0)) ||
        (obj->idx == obj->cnt)){
        flag = false;
    } else {
        flag = true;
    }

    return flag;
}

void CompressedStrIteratorFree(CompressedStrIterator* obj)
{
    free(obj);
}

 

你可能感兴趣的:(LeetCode)