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. socket.io 中文api

    1. 服务端 io.on('connection',function(socket)); 监听客户端连接,回调函数会传递本次连接的socket io.sockets.emit('String',dat ...

  2. 一起学android之设置ListView数据显示的动画效果(24)

    效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFpX3FpbmdfeHVfa29uZw==/font/5a6L5L2T/fontsize/40 ...

  3. JZOJ.5286【NOIP2017模拟8.16】花花的森林

    Description

  4. cascade(级联)和inverse关系详解

    序言 写这篇文章之前,自己也查了很多的资料来搞清楚这两者的关系和各自所做的事情,但是百度一搜,大多数博文感觉说的云里雾里,可能博主自己清楚是怎么一回事,但是给一个不懂的人或者一知半解的人看的话,别人也 ...

  5. 【BZOJ4566】[Haoi2016]找相同字符 后缀数组+单调栈

    [BZOJ4566][Haoi2016]找相同字符 Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同 ...

  6. Web移动端使用localStorage缓存Js和css文件 | 强制不要缓存

    1.尽量不把js放在onload事件中,而是放在由用户主动触发的事件 2.加时间戳,时间不同则会加载js而非使用缓存 强制不要缓存: <meta http-equiv=Cache-Control ...

  7. 160726、jQuery常用操作

    一.简介   定义  jQuery创始人是美国John Resig,是优秀的Javascript框架: jQuery是一个轻量级.快速简洁的javaScript库. jQuery对象  jQuery产 ...

  8. MySQL主从不一致修复

    场景: 线上正在服务的库由于紧急主从切换导致主从不一致,报错信息如下: Last_Error: Coordinator stopped because there were error(s) in t ...

  9. slave_exec_mode参数对主从复制的影响

    主从复制中常会遇到的问题就是1062主键重复.1032 slave上相关记录没找到 如果在读写分离的架构中,slave同步失败会对业务造成很大的影响的(比如主写入了一条数据,从上无法读取到这样对业务影 ...

  10. link options and how g++ is invoked gcc g++

    yum install gcc yum install gcc-c++ yum reinstall gcc gcc-c++ Downloading packages:(1/2): gcc-c++-4. ...