https://leetcode.com/problems/two-sum/description/

第一种方法  遍历查找

//
// main.m
// HFCDemo
//
// Created by HF on 2018/9/5.
// Copyright © 2018年 HF. All rights reserved.
// #import <Foundation/Foundation.h> /**
* Note: The returned array must be malloced, assume caller calls free().
* int *array = (int *)malloc(sizeof(int) * 2);
*/
int* twoSum(int* nums, int numsSize, int target) {
int oneIndex = 0;
int twoIndex = 0;
for (int i = 0;i < numsSize; i ++) {
int one = nums[i];
int two = target - one;
for (int j = 0; j < numsSize; j ++) {
if (j > i && nums[j] == two) {
oneIndex = i;
twoIndex = j;
break;
}
}
}
//要求必须动态分配空间数组
int *array = (int *)malloc(sizeof(int) * 2);
array[0] = oneIndex;
array[1] = twoIndex;
return array;
} int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!\n");
int numSize = 4;
int nums[4] = {2, 7, 11, 15};
int target = 9;
int *array = twoSum(nums, numSize, target);
printf("%d %d\n",array[0],array[1]);
free(array);
}
return 0;
}

第二种:哈希

待完成

LeetCode 1. Two Sum 找到两数字求和索引集合的更多相关文章

  1. LeetCode 1. Two Sum (两数之和)

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  2. Leetcode#1.Two Sum(两数之和)

    题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], ta ...

  3. [LeetCode] 1. Two Sum 两数和

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  4. LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  5. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...

  6. [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  7. [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  8. [LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)

    题目描述: /* Given an array of integers, return indices of the two numbers such that they add up to a sp ...

  9. [LeetCode] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

随机推荐

  1. Java释出的时候,AWT作为Java最弱的组件受到不小的批评

    Java释出的时候,AWT作为Java最弱的组件受到不小的批评. 最根本的缺点是AWT在原生的用户界面之上仅提供了一个非常薄的抽象层. 例如,生成一个AWT的 复选框会导致AWT直接调用下层原生例程来 ...

  2. String painter (hdu 2476 DP好题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意: 给出两个等长的串S, T, 要将S变成T, 每次可以把S的连续的一段变成相同的字母 ...

  3. Androidz之字符串国际化问题

    (1)字符串的国际化 在res下写两个values 注意:命名方式都是固定的 一个中文版:values-zh 一个英文版:values-en 可是<string name  要写成一样的,这样会 ...

  4. 【统计分析】ROC曲线

    http://baike.baidu.com/link?url=O8nVf39qW4UpYAegk9cJfYARCFDg8YHQ6p5wFnWxYvo151doXo-WvG5Rfz0j4R-r 受试者 ...

  5. Multiview

    新建3个ViewController的类 新建main.stroyboard,并配置好相应的布局(编辑界面,连接视图与类,ViewController的Storyboard ID,连接3个Button ...

  6. MathType输入补集符号的步骤有哪些

    集合符号在很多的数学领域都会用到,其基本的集合运算可以分为交.并.补这三种.但是一些用户朋友们在编辑文档的时候想输入集合符号这个时候就需要用到数学公式编辑器MathType,但是很多人能够快速地编辑出 ...

  7. ios --转载-从URL中截取所包含的参数,并且以字典的形式返回和参数字典转URL

    - (NSString *)keyValueStringWithDict:(NSDictionary *)dict { if (dict == nil) { return nil; } NSMutab ...

  8. CentOS 安装 dotnetcore

    参考官方教程:https://www.microsoft.com/net/core#linuxcentos 安装.NET CORE SDK sudo yum install libunwind lib ...

  9. iOS Search bar 输入空字符串也可以搜索

    Search bar delegate - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { UITextField *sea ...

  10. jquery全景拖动查看效果

    http://sc.chinaz.com/jiaoben/140722166830.htm