暴力破解法:

    1. /**
    2. * Note: The returned array must be malloced, assume caller calls free().
    3. */
    4. int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    5. int i, j;
    6. for (i = 0; i < numsSize - 1; ++i) {
    7. for (j = i + 1;j < numsSize; ++j) {
    8. if ((nums[i] + nums[j]) == target) {
    9. int* result = (int*)malloc(2 * sizeof(int));
    10. result[0] = i;
    11. result[1] = j;
    12. *returnSize = 2;
    13. return result;
    14. }
    15. }
    16. }
    17. return NULL;
    18. }

    结果:

    通过 92 ms 6 MB C

    下一步看看能否优化