What's wrong with my c code ?


  • 0
    Q
    int* grayCode(int n, int* returnSize) {
    
    int i = 2, j = 0, start = 1;
    if (n == 0) return returnSize;
    int* res = (int *)malloc(*returnSize);
    memset(res, 0, *returnSize);
    res[0] = 0;
    res[1] = 1;
    while (i <= n) {
    	start = 2 * start;
    	j = 0;
    	while (j < start) {
    		res[start + j] = res[start - 1 - j] | (1 << (i - 1));
    		j++;
    	}
    	i++;
    }
    return res;
    
    }
    

    when i run my code , there is nothing show in the result box , but it's work fine on vs2015, looking for you help,thx!


  • 0
    Q
    int* grayCode(int n, int* returnSize) {
    int i = 2, j = 0, start = 1;
    int* res;
    *returnSize = 1 << n;
    res = (int *)malloc(sizeof(int) * *returnSize);
    memset(res, 0, sizeof(int) * *returnSize);
    res[0] = 0;
    if (n == 0) return res;
    res[1] = 1;
    while (i <= n) {
    	start = 2 * start;
    	j = 0;
    	while (j < start) {
    		res[start + j] = res[start - 1 - j] | (1 << (i - 1));
    		j++;
    	}
    	i++;
    }
    return res;
    }
    

    i solved it by my self.


Log in to reply
 

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