clean c, please give some advice


  • 0
    P
    int table[] = {1, 2, 4, 8, 16, 32, 100, 200, 400, 800, 0};
    int size;
    char** ans;
    
    #define VALID(sum) ((sum) < 1200 && ((sum) % 100) < 60)
    
    void insert(int sum) {
      char* tmp = malloc(sizeof(char) * 6);
      sprintf(tmp, "%d:%02d", sum/100, sum%100);
      ans[size++] = tmp;
    }
    
    void backtrack(int num, int sum, int deep) {
      sum += table[num];
      if(0 == deep && VALID(sum)) insert(sum);
      else if(!VALID(sum)) return;
      int i;
      for(i = 0; i < num; i++)
        backtrack(i, sum, deep - 1);
    }
    
    char** readBinaryWatch(int num, int* returnSize) {
      size = 0; 
      ans = malloc(sizeof(char *) * 1024);
      backtrack(10, 0, num);
      *returnSize = size;
      return ans;
    }
    

  • 0

    All pointers point to the same address tmp
    define the tmp like this tmp[400][6],
    and each pointer points to one of them like ans[size++]=tmp[size]


Log in to reply
 

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