1.Two Sum

  • 题目

** Example **:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Python

  • My Code:
class Solution:
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        
        for i in range(len(nums)):
            for j in range(i+1,len(nums)):
                if nums[i] + nums[j] == target:
                    return [i,j]
        

C Language

int* twoSum(int* nums, int numsSize, int target) {
    int *result = (int*)malloc(2 * sizeof(int));

    for (int i = 0; i < numsSize; i++) {
        int tmp = target - *(nums + i);
        for (int j = i + 1; j < numsSize; j++) {
            if (*(nums + j) == tmp) {
                result[0] = i;
                result[1] = j;
                return result;
            }
        }
    }

    result[0] = 33;
    result[0] = 34;
    return result;
}