leetcode 数位和相等数对的最大和,为什么我这么写不对呢?

image.png
这个是题目,
这个是我不通过的测试用例:nums =
[368,369,307,304,384,138,90,279,35,396,114,328,251,364,300,191,438,467,183]
下面是我的代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int sumNumber(int num) {
    int digit;//用于保存每个位数的数字
    int sum = 0;//用于记录每个位数的和
    while (num != 0) {
        digit = num % 10;
        sum += digit;
        num /= 10;
    }
    return sum;
}

int maxNum(int* mid) {
    int i = 0, h = 0, max;
    int length = 0;//记录数组长度
    length = sizeof(mid) / sizeof(mid[0]);
    printf("\n");
    printf("length = %d\n", h);
    if (mid[0] == 0)
        max = -1;
    if (mid[0] !=0 && mid[1] == 0)
        max = mid[0];
    /*if (mid[0] != 0 && mid[1] != 0) {
        max = mid[0];
        for (i = 1; i < m; i++) {
            if (mid[i] > max)
                max = mid[i];
        }
    }
    */
    return max;
}
int maxNumber(int x, int y) {
    return x > y ? x : y;
}
int maximumSum(int* nums, int numsSize) {
    int i = 0, j = 0, h = 0, test = 0, max = 0;
    int hashTable[100000] = { 0 };
    int find = 0;
    int* res = (int*)calloc(numsSize, sizeof(int));
    for (i = 0; i < numsSize; i++) {
        res[i] = sumNumber(nums[i]);
    }
    for (i = 0; i < numsSize; i++) {
        printf("res[i] = %d  ",res[i]);
    }
    printf("\n");
    /*for (i = 0; i < numsSize; i++) {
        for (j = i + 1; j < numsSize; j++) {
            if (res[i] == res[j]) {
                test = nums[i] + nums[j];
                if (test > max)
                    max = test;
            }
        }
    }*/
    for (i = 0; i < numsSize; i++) {
        if (hashTable[res[i]] == 0)
            hashTable[res[i]] = nums[i];
        else {
            hashTable[res[i]] += nums[i];
            max = maxNumber(max, hashTable[res[i]]);
        }
    }
    printf("hast18 = %d", hashTable[18]);
    //maxNum(hashTable);
    if (max == 0)
        return -1;
    return max;
}

int main() {
    int find = 0;
    int nums[19] = { 368,369,307,304,384,138,90,279,35,396,114,328,251,364,300,191,438,467,183 };
    find = maximumSum(nums, 19);
    printf("  find = %d ", find);
}

我运行上面的测试用例后,得到的结果是1044,我感觉没错啊,上面数组中的369,279,396的数位和是18,他们相加的结果是1044,而正确答案是835,即368,438的加和为835。

但是我这样写代码就行,最后运行的结果就是835,求大佬解答。

int sumNumber(int num) {
    int digit;//用于保存每个位数的数字
    int sum = 0;//用于记录每个位数的和
    while (num != 0) {
        digit = num % 10;
        sum += digit;
        num /= 10;
    }
    return sum;
}

int maximumSum(int* nums, int numsSize) {
    int i = 0, j = 0, h = 0, test = 0, max = 0;
    int* res = (int*)calloc(numsSize, sizeof(int));
    for (i = 0; i < numsSize; i++) {
        res[i] = sumNumber(nums[i]);
    }
    for (i = 0; i < numsSize; i++) {
        printf("res[i] = %d  ", res[i]);
    }
    printf("\n");
    for (i = 0; i < numsSize; i++) {
        for (j = i + 1; j < numsSize; j++) {
            if (res[i] == res[j]) {
                test = nums[i] + nums[j];
                printf("test : %d,res[i] = %d\n",test,res[i]);
                if (test > max)
                    max = test;
            }
        }
    }
    if (max == 0)
        return -1;
    return max;
}

image.png

阅读 392
1 个回答

题目要求选出两个下标求和,问和的最大值。你那个是三个,不是两个。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Microsoft
子站问答
访问
宣传栏