Leetcode question 1: twoSum | Source code and comment (C language)

刚好最近得空进去Leetcode做题目,顺便把自己做的答案放在Blog记录下,要不然看不到进度的话我会懒惰的哈哈,有需要的自己看,Source code和comment都放在下面了。对了我用的programming language是C language。

QUESTION 1: TwoSum

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to the target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

Test case:

1. nums = [2,7,11,15], target = 9

2. nums = [3,2,4], target = 6

3. nums = [3,3], target = 6

Example:

Input: nums = [2,7,11,15], target = 9 

Output: [0,1] 

Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Source code and comments:

Function twoSum

Main function

Result for test case 1


评论