題目 Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have ...
題目
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
這好像是在前排的Easy的一題
設計知識點:
- 動態數組的開闢
- 暴力法
C語言結局的思路如下:
開闢一個新的數組(動態數組的開闢):用來放返回的數組,在應用暴力法計算相應的數值。
新知識點:動態數組的開闢。
代碼:
int* twoSum(int* nums, int numsSize, int target) {
int *re=(int*)malloc(2*sizeof(int));
for(int i=0;i<numsSize;i++)
{
for(int j=i+1;j<numsSize;j++)
{
if(nums[i]+nums[j]==target)
{
re[0]=i;
re[1]=j;
}
}
}
return re;
}