题解 | #两数之和#
两数之和
https://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param numbers int整型一维数组 * @param numbersLen int numbers数组长度 * @param target int整型 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* twoSum(int* numbers, int numbersLen, int target, int* returnSize ) { int* two = malloc(3 * sizeof(int)); int flag = 0; for (int i = 0; i < numbersLen - 1; i++) { if(numbers[i] > target + 10){ continue; } for (int j = i + 1; j < numbersLen; j++) { if (numbers[i] + numbers[j] == target) { two[0] = i + 1; two[1] = j + 1; flag = 1; } } if (flag == 1) { break; } } *returnSize = 2; return two; // write code here }
这题只能用hash表,否则时间复杂度降不下来,我这也是设置条件蒙混过去的