題目:

來源:https://leetcode.com/problems/two-sum/description/

難易度:Easy

Question:Given an array of integers, returnindicesof the two numbers such that they add up to a specific target.

You may assume that each input would haveexactlyone solution, and you may not use thesameelement twice.

給一個整數數,返回兩個數字的值,將它們相加到一個特定的目標。

您可以假設每個輸入都只有一個解決方法,而您可能不會使用相同的元素兩次。

範例:

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

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

Return [0, 1]

我的程式碼:

int* twoSum(int* nums, int numsSize, int target) {
        static int array[2]={0};
            for (int i = 0 ; i < numsSize; i++){

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

results matching ""

    No results matching ""