Runs ok on codeblocks locally, but "RuntimeError" here, the last input:11 'A's.


  • 0
    Z

    /string to number by 4 bits/
    int str2Num(char
    s, int len)
    {
    if(!s)
    return -1;

    int ret = 0;
    int i = 0;
    for(; i < len; i ++)
    {
        ret <<= 2;
        switch(s[i])
        {
            case 'A': ret += 0;break;
            case 'C': ret += 1;break;
            case 'G': ret += 2;break;
            case 'T': ret += 3;break;
            default : break;
        }
    }
    return ret;
    

    }

    /number to string by 4 bits/
    char
    num2Str(int s, int len)
    {
    char* ret = (char*)malloc(len * sizeof(char));
    int i = len - 1;
    for(; i >= 0; i --)
    {
    switch(s % 4)
    {
    case 0: ret[i] = 'A';break;
    case 1: ret[i] = 'C';break;
    case 2: ret[i] = 'G';break;
    case 3: ret[i] = 'T';break;
    default: break;
    }
    s >>= 2;
    }
    return ret;
    }

    /Sub-String/
    char
    subString(char* s, int pos, int size)
    {
    if(!s)
    return NULL;

    if(strlen(s) < pos + size)
        return NULL;
    char * ret = (char*)malloc(size * sizeof(char));
    
    int i = 0;
    for(; i < size; i ++)
        ret[i] = s[i + pos];
    
    return ret;
    

    }

    /Find repeated DNA sequence/
    char
    * findRepeatedDnaSequences(char* s, int* returnSize)
    {
    if(!s)
    return NULL;
    int len = strlen(s), temp = 0, itmp = 1;

    if(len < 10)
        return NULL;
        
    int map[64] = {0};
    char** ret = (char**)malloc(sizeof(char*));
    char** p = ret;
    
    int i = 0;
    for(; i < len - 10 + 1; i ++)
    {
        temp = str2Num(subString(s, i, 10), 10);
        if(temp == -1)
            break;
    
        if(map[temp])
        {
            if(map[temp] != 1)
                continue;
            
            map[temp] += 1;
            *p = num2Str(temp, 10);
            ret = (char**)realloc(ret, ++itmp * sizeof(char**));
            p = ret + itmp - 1;
        }
        else
            map[temp] += 1;
    }
    free(p);
    p = NULL;
    ret[itmp - 1] = NULL;
    *returnSize = itmp - 1;
    return ret;
    

    }enter code here


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.