问题描述:
给定一个数组,找出两个元素,其相加等于一个给定的目标值,返回这两个元素的索引值。
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].
分析:
1、找出数组的两个索引值;
2、返回两个索引值,可以采用结构体指针、数组形式,本例采用前一种。
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target) {
int i,j;

博客介绍了LeetCode中的一道题目——Two Sum,通过C语言实现找到数组中两个数相加等于目标值的索引。文章指出原始代码存在的数组越界问题,并提供了修正后的正确代码。
最低0.47元/天 解锁文章
3654

被折叠的 条评论
为什么被折叠?



