0ms runtime for C


  • 0
    S
    char* getHint(char* secret, char* guess) 
    {
      int countSe[10];
      int countGu[10];
      int countA = 0;
      int count = 0;
      int countB;
      int ctAn = 0;
      int ctBn = 0;
      int m, n;
      int i = 0;
      char ch;
      char * res;
      
      for (i = 0; i < 10; i++)
        countSe[i] = countGu[i] = 0;
    
      i = 0;
      while ((ch = *(secret + i)) != '\0')
      {
        switch (ch){
          case '0' : countSe[0]++;
                     break;
          case '1' : countSe[1]++;
                     break;
          case '2' : countSe[2]++;
                     break;
          case '3' : countSe[3]++;
                     break;
          case '4' : countSe[4]++;
                     break;
          case '5' : countSe[5]++;
                     break;
          case '6' : countSe[6]++;
                     break;
          case '7' : countSe[7]++;
                     break;
          case '8' : countSe[8]++;
                     break;
          case '9' : countSe[9]++;
                     break;
          default : break;
        }
        i++;
      }
      
      i = 0;
      while ((ch = *(guess + i)) != '\0')
      {
        switch (ch){
          case '0' : countGu[0]++;
                     break;
          case '1' : countGu[1]++;
                     break;
          case '2' : countGu[2]++;
                     break;
          case '3' : countGu[3]++;
                     break;
          case '4' : countGu[4]++;
                     break;
          case '5' : countGu[5]++;
                     break;
          case '6' : countGu[6]++;
                     break;
          case '7' : countGu[7]++;
                     break;
          case '8' : countGu[8]++;
                     break;
          case '9' : countGu[9]++;
                     break;
          default : break;
        }
        i++;
      }
      
      i = 0;
      while (*(secret + i) != '\0')
      {
        if (*(secret + i) == *(guess + i))
          countA++;
        i++;
      }
      for (i = 0; i < 10; i++)
      {
        if(countSe[i] >= countGu[i])
          count += countGu[i];
        else
          count += countSe[i];
      }
      countB = count - countA;
      
      for (i = 0, n = countA; n > 0; i++)
      {
        n /= 10;
        ctAn++;
      }
      if (ctAn == 0)
        ctAn++;
      for (i = 0, n = countB; n > 0; i++)
      {
        n /= 10;
        ctBn++;
      }
      if (ctBn == 0)
        ctBn++;
      res = (char *) malloc ((ctAn+ctBn+3)*sizeof(char));
      for (i = ctAn-1, n = countA; i >= 0; i--)
      {
        m = n % 10;
        *(res + i) = '0' + m;
        n /= 10;
      }
      *(res + ctAn) = 'A';
      for (i = ctBn-1, n = countB; i >= 0; i--)
      {
        m = n % 10;
        *(res + i + ctAn + 1) = '0' + m;
        n /= 10;
      }
      *(res + ctAn + ctBn + 1) = 'B';
      *(res + ctAn + ctBn + 2) = '\0';
      return res;
    }

Log in to reply
 

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