leetcode 537. Complex Number Multiplication

Orignal question can be found here.

My solution is as follows:

char* complexNumberMultiply(char* a, char* b)
{
  int a_real, a_vir, b_real, b_vir;
  char *result = (char *) malloc(1024 * sizeof(char));
  sscanf(a, "%d+%di", &a_real, &a_vir);
  sscanf(b, "%d+%di", &b_real, &b_vir);
  sprintf(result, "%d+%di", a_real *  b_real - a_vir * b_vir, a_real * b_vir + a_vir * b_real);
  return result;
}

leetcode 482. License Key Formatting

Orignal question can be found here.

My solution is as follows:

char* licenseKeyFormatting(char* S, int K)
{
  int s_len = strlen(S);
  int len = 0;
  for(int i=0; i<s_len; ++i)
    {
      if(S[i] != '-')
        ++len;
    }
  int result_len = 0;
  int cur = 0;
  if(len<=K)
    {
      char *res = (char *)malloc((len+1) * sizeof(char));
      res[len] = 0;
      for(int i=0; i<s_len; ++i)
        {
          if(S[i] != '-')
            {
              if(S[i]>='a' && S[i]<='z')
                res[cur] = S[i] - 'a' + 'A';
              else
                res[cur] = S[i];
              ++cur;
            }
        }
      return res;
    }
  if(len%K== 0)
    result_len = len/K+len-1;
  else
    result_len = len/K+len;
  char *result = (char *)malloc((result_len+1) * sizeof(char));
  result[result_len] = 0;

  cur = 0;
  for(int i=s_len-1; i>=0; --i)
    {
      if(S[i] != '-')
        {
          if((cur+1)%(K+1) == 0)
            {
              result[result_len - 1 - cur] = '-';
              ++cur;
            }
          if(S[i]>='a' && S[i]<='z')
            result[result_len -1 - cur] = S[i] - 'a' + 'A';
          else
            result[result_len -1 - cur] = S[i];
          ++cur;
        }
    }
  return result;
}

leetcode 94. Binary Tree Inorder Traversal

Orignal question can be found here.

My solution is as follows:

int* inorderTraversal(struct TreeNode* root, int* returnSize)
{
        int* result=(int*)malloc(sizeof(int)*512);
        *returnSize=0;
        if(root == NULL)
                return result;
        inorderTraversalCore(root, result, returnSize);
        return result;
}

void inorderTraversalCore(struct TreeNode* root, int* array, int* returnSize)
{
        if(root->left != NULL)
                      inorderTraversalCore(root->left, array, returnSize);
        array[*returnSize] = root->val;
        ++(*returnSize);
        if(root->right != NULL)
                       inorderTraversalCore(root->right, array, returnSize);
}

leetcode 404. Sum of Left Leaves

Orignal question can be found here.

My solution is as follows:

int sumOfLeftLeaves(struct TreeNode* root)
{
  int result = 0;
  int *result_p = &result;
  sumCore(root, 0, result_p);
  return result;
}

void sumCore(struct TreeNode* root, int flag, int *result_p)
{
  if(root == NULL)
    return;
  if(flag==1 && root->left==NULL && root->right==NULL)
    {
      *result_p += root->val;
      return;
    }
  if(root->left != NULL)
    sumCore(root->left, 1, result_p);
  if(root->right != NULL)
    sumCore(root->right, 0, result_p);
}

leetcode 409. Longest Palindrome

Orignal question can be found here.

My solution is as follows:

int longestPalindrome(char* s)
{
  int result = 0;
  int one_more = 0;
  int hash[52]={0};
  for (int i=0; i<strlen(s); ++i)
    {
      if(s[i]>='a' && s[i]<='z')
        ++hash[s[i]-'a'];
      else if(s[i]>='A' && s[i]<='Z')
        ++hash[s[i]-'A'+26];
    }
  for (int i=0; i<52; ++i)
    {
      if(hash[i]%2 == 1)
        one_more = 1;
      result += (hash[i]/2)*2;
    }
  if(one_more == 1)
    ++result;
  return result;
}