LeetCode 1. Two Sum 找到两数字求和索引集合
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 找到两数字求和索引集合的更多相关文章
- LeetCode 1. Two Sum (两数之和)
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- Leetcode#1.Two Sum(两数之和)
题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], ta ...
- [LeetCode] 1. Two Sum 两数和
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- 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 ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- [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 ...
- [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- [LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)
题目描述: /* Given an array of integers, return indices of the two numbers such that they add up to a sp ...
- [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 ...
随机推荐
- 【PM面试题】如果让你创业,你会选择什么?
答案及理由 我会选择可穿戴设备 ,理由有三: 互联网与硬件的结合是未来的大势所趋,通过硬件来采集数据,而通过互联网或者移动互联网将这些设备连接起来,交换数据,让其形成流动的信息. 未来会从卖产品的阶段 ...
- php 判断白天黑夜
<?php $h=date('H'); if($h>=8 && $h<=20) echo '白天'; else echo '夜晚'; ?>
- Torch-RNN运行过程中的坑 [1](读取Lua非空table,size为0)
0.踩坑背景 执行Torch-RNN的时候,在LanguageModel.lua中的encode_string函数中,对start_text的各个character进行id映射编码,实现功能类似“北京 ...
- datagrid加分组后的效果
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAd8AAADdCAIAAAB13e+wAAAZgElEQVR4nO2d/28b533Hn7+APxnYgL ...
- sublime text3 插件CTags
1.打开Sublime Text 2/3软件,在Preferences(设置)菜单中打开Package Control(插件管理器)打开菜单后找到install packages,搜索ctags, 回 ...
- mybatis的dao的注解
import com.jianwu.domain.metting.model.CallPreMember;import com.jianwu.domain.metting.model.CallPreM ...
- python的三元运算
python的三元运算是先输出结果,再判定条件.其格式如下: >>> def f(x,y): return x - y if x>y else abs(x-y) #如果x大于y ...
- Openstack深入了解虚拟机
续Openstack虚拟机创建流程 在控制节点也安装一个计算服务也变成一个计算节点 yum -y install openstack-nova-compute 启动并且设置开机启动 systemctl ...
- 并发编程6 锁&进程&队列
1.进程的其他方法 2.验证进程空间隔离和守护进程 3.孤儿进程和僵尸进程 4.锁 for循环加join数据共享 5.进程队列的简单应用 6.通过队列实现进程间的通信 7.生产者消费者模型及Queue ...
- Akka Essentials - 1
参考Akka Essentials 1 Introduction to Akka Actor Model Actor模式的由来 In 1973, Carl Hewitt, Peter Bishop ...