LeetCode两数之和

avatar
作者
筋斗云
阅读量:1

C

/**  * Note: The returned array must be malloced, assume caller calls free().  */ int* twoSum(int* nums, int numsSize, int target, int* returnSize) {     for (int i = 0;i<numsSize;i++){         for (int j = i+1;j<numsSize; j++){             if(nums[i] + nums[j] == target){                 int *ret = malloc(sizeof(int)*2);                 ret[0] = i, ret[1] = j;                 *returnSize = 2;                 return ret;             }         }     }     *returnSize = 0;     return NULL; }

Python

class Solution:     def twoSum(self, nums: List[int], target: int) -> List[int]:         n = len(nums)         for i in range(n):             for j in range(i+1 ,n):                 if nums[i] + nums[j] == target:                     return [i,j]          return []

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!