开发者社区> 问答> 正文

leetCode上刷题碰到的问题,Create Maximum Number

这是题目的网址,感兴趣可以去试试 : 原题网址

题目 :

Given two arrays of length m and n with digits 0-9 representing two

  1. Create the maximum number of length k <= m + n from digits of
    1. The relative order of the digits from the same array must be
  2. Return an array of the k digits. You should try to optimize
  3. time and space complexity.

Example 1: nums1 = [3, 4, 6, 5] nums2 = [9, 1, 2, 5, 8, 3] k = 5
return [9, 8, 6, 5, 3]

Example 2: nums1 = [6, 7] nums2 = [6, 0, 4] k = 5 return [6, 7, 6, 0,
4]

Example 3: nums1 = [3, 9] nums2 = [8, 9] k = 3 return [9, 8, 9]
网上我搜索了很久都没有搜索到C语言的答案,自己写了一个答案但是没用过不了,我和这个人碰到了一样的问题,这里是这个人问题的链接。。想问问是我理解题目有问题还是怎么,难道它不是要我返回一个指针吗?

展开
收起
a123456678 2016-06-12 11:20:10 2421 0
1 条回答
写回答
取消 提交回答
  • 这个问题我只能想出O(nk)的算法,写完之后直接去交,遇到了跟题主一样的问题,然后查了下其他题目的代码,发现要将returnSize赋值成那个数组的大小。。。下面上代码:

     int* calc(int* nums,int n,int k,int *rec[10],int *head,int *tail)
        {
            if (k == 0)
                return NULL;
            int *cur = (int *)malloc(k*sizeof(int));
            for (int i = 0; i<10; i++)
            {
                head[i] = tail[i] = 0;
            }
            int curSize = 0;
            for(int i = 0; i<n - k; i++)
            {
                int u = nums[i];
                rec[u][tail[u]++] = i;
            }
            for (int i = 0, last = 0; i<k; i++)
            {
                int u = nums[i + n - k];
                rec[u][tail[u]++] = i + n - k;
                for (int j = 9; j >= 0; j--)
                {
                    if (head[j] != tail[j])
                    {
                        cur[curSize++] = j;
                        int end = rec[j][head[j]];
                        while (last <= end)
                        {
                            int u = nums[last++];
                            head[u]++;
                        }
                        break;
                    }
                }
            }
            return cur;
        }
        int* maxNumber(int* nums1, int nums1Size, int* nums2, int nums2Size, int k, int* returnSize) {
            int n = nums1Size>nums2Size ? nums1Size : nums2Size;
            int *ans=NULL,*rec[10], head[10], tail[10];
            for (int i = 0; i < 10; i++)
            {
                rec[i] = (int*)malloc(n*sizeof(int));
            }
        
            for (int k1 = 0; k1 <= k&&k1 <= nums1Size; k1++)
            {
                int k2 = k - k1;
                if (k2>nums2Size)
                    continue;
                int *cur1 = calc(nums1, nums1Size, k1, rec, head, tail), *cur2 = calc(nums2, nums2Size, k2, rec, head, tail), *curans = (int*)malloc(k*sizeof(int)), flag = 1,flag1=1;
                
                for (int i = 0, j1 = 0, j2 = 0; i < k; i++)
                {
                    
                    if (j1 < k1&&j2 < k2)
                    {
                        int f = 1;
                        for (int u1 = j1, u2 = j2; u1 < k1&&u2 < k2; u1++, u2++)
                        {
                            if (cur1[u1]>cur2[u2])
                            {
                                curans[i] = cur1[j1++];
                                f = 0;
                                break;
                            }
                            else if (cur1[u1] < cur2[u2])
                            {
                                curans[i] = cur2[j2++];
                                f = 0;
                                break;
                            }
                        }
                        if (f)
                        {
                            if (k1 - j1>k2 - j2)
                                curans[i] = cur2[j2++];
                            else
                                curans[i] = cur1[j1++];
                        }
                    }
                    else if (j1 < k1)
                    {
                        curans[i] = cur1[j1++];
                    }
                    else
                    {
                        curans[i] = cur2[j2++];
                    }
                    if (flag1&&ans!=NULL)
                    {
                        if (curans[i] < ans[i])
                        {
                            flag = 0;
                            break;
                        }
                        else if (curans[i]>ans[i])
                        {
                            flag1 = 0;
                        }
                    }
                    
                }
                if (flag)
                {
                    free(ans);
                    ans = curans;
                }
                else
                {
                    free(curans);
                }
            }
            *returnSize = k;
            return ans;
        }
    2019-07-17 19:33:33
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
High-Performance Time Series 立即下载
Show Me The Money! Cost & Reso 立即下载
Tracking-Ransomware-End-To-End 立即下载