LeetCode 1 Two Sum
题目
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;
}

浙公网安备 33010602011771号