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. Swift-8-枚举

    // Playground - noun: a place where people can play import UIKit // 枚举语法 enum SomeEnumeration { // e ...

  2. JDBC(Java Database Connectivity,Java数据库连接)API是一个标准SQL(Structured Query Language

    JDBC(Java Database Connectivity,Java数据库连接)API是一个标准SQL(Structured Query Language,结构化查询语言)数据库访问接口,它使数据 ...

  3. AWT从概念产生到完成实现只用了一个月

    这种糟糕的设计选择使得那些拥护Java“一次编写,到处运行 (write once, run anywhere)”信条的程序员们过得并不舒畅,因为AWT并不能保证他们的应用在各种平台上表现得有多相似. ...

  4. Python实现生命游戏

    1. 生命游戏是什么 生命游戏是英国数学家约翰·何顿·康威在1970年发明的细胞自动机.它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞.一个细胞在下一个时刻生死取决于相邻八个 ...

  5. 【BZOJ4688】One-Dimensional 矩阵乘法

    [BZOJ4688]One-Dimensional Description 考虑一个含有 N 个细胞的一维细胞自动机.细胞从 0 到 N-1 标号.每个细胞有一个被表示成一个小于 M 的非负整数的状态 ...

  6. SharePoint BI

    本篇博客主要针对SharePoint BI整体结构进行整理,为读者分析几种Sharepoint BI场景 先附一张自己做的结构图:

  7. HDU 5157 Harry and magic string(回文树)

    Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  8. Pycharm如何取消自动换行

    1.只对当前文件有效的操作是: 菜单栏->View -> Active Editor -> Use Soft Wraps (不选中) 2.要是想对所有文件都起到效果,就要在setti ...

  9. ThinkPHP中通过URL重写隐藏应用的入口文件index.php的相关服务器的配置

    [ Apache ] 将httpd.conf配置文件中mod_rewrite.so所在行前面的‘#’去掉 AllowOverride None 将None改为 All 效果图

  10. php 使用imagettftext()函数出问题的原因

    <?php header('Content-type: image/png'); $im = imagecreatetruecolor(400, 300); //创建画布 $white = im ...