【leetcode】89. 格雷编码

int* grayCode(int n, int* returnSize){
    int cnt=pow(2,n);
    *returnSize=cnt;
    int* arr=(int*)calloc(cnt,sizeof(int));
    int i, j, temp;
    int hash[10000]={0};
    hash[0]=1;
    for(i=1; i<cnt; i++){        
        for(j=0; j<31; j++){
            temp = ( arr[i-1]&(1 << j) )?-(1 << j) :(1 << j);
            if( hash[arr[i-1]+temp] ==0 ){
                hash[arr[i-1]+temp]++;
                arr[i]=arr[i-1]+temp;
                break;
            }
        }
    }    
    return arr;
}
原文地址:https://www.cnblogs.com/ganxiang/p/14130076.html