码迷,mamicode.com
首页 > 其他好文 > 详细

LC1 Two Sum

时间:2017-03-29 14:03:17      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:size   层遍历   算法   查找   har   tar   get   style   函数   

1、描述

技术分享

2、算法

两层for循环查找,第一层遍历,第二层从第一层的后一个数开始找;

一旦求和,返回。

【接口函数】

 1 int* twoSum(int* nums, int numsSize, int target) {
 2     int i, j;
 3     int *ans;
 4     ans = (int*)malloc(sizeof(int)* 2);
 5     for (i = 0; i < numsSize; i++)
 6     for (j = i + 1; j < numsSize; j++)
 7     if (nums[i] + nums[j] == target)
 8     {
 9         ans[0] = i;
10         ans[1] = j;
11         return ans;
12     }
13     return NULL;
14 }

【其他函数】

 1 int main()
 2 {
 3     int n=-1;
 4     int a[1000];
 5     int *p;
 6     char ch;
 7     int i, j;
 8     int sum;
 9 
10     scanf("%c", &ch);
11     while (ch != ])
12     {
13         n++;
14         scanf("%d%c", &a[n], &ch);
15     }
16     scanf("%d", &sum);
17     p=twoSum(a, n+1, sum);
18     return 0;
19 }

3、心得

LeetCode只需要实现接口。

LC1 Two Sum

标签:size   层遍历   算法   查找   har   tar   get   style   函数   

原文地址:http://www.cnblogs.com/BEWINDOWEB/p/6639416.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!