代码如下:
 /**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target) {
static int a[] = {}; for (int i = ; i < numsSize; i++) {
for (int j = i + ; j < numsSize; j++) {
if (nums[i] + nums[j] == target) {
a[] = i;
a[] = j;
return a;
}
}
}
return ;
}

这道题简单,就不做过多解释。

而另一种方法,则是用到基数排序

 /*************************************************************************
> File Name: 1-1.cpp
> Author: Mr.Lee
> Mail: 18646139976@163.com
> Created Time: 五 5/10 11:10:39 2019
************************************************************************/ void radix_sort(int *arr, int *main_ind, int n) {
#define MAX_N 65536
#define MAX_M 32768
#define L(x) (x & 0xffff)
#define H(x) ((x >> 16) & 0xffff) int cnt[MAX_N] = {}, *p;
int *temp = (int *)malloc(sizeof(int) * n);
int *ind = (int *)malloc(sizeof(int) * n);
for (int i = ; i < n; i++) cnt[L(arr[i])] += ;
for (int i = ; i < MAX_N; i++) cnt[i] += cnt[i - ];
for (int i = n - ; i >= ; i--) {
temp[--(cnt[L(arr[i])])] = arr[i];
ind[cnt[L(arr[i])]] = i;
}
memset(cnt, , sizeof(cnt));
for (int i = ; i < n; i++) cnt[H(temp[i])] += ;
for (int i = MAX_M; i < MAX_M + MAX_N; i++) cnt[i % MAX_N] += cnt[(i - ) % MAX_N];
for (int i = n - ; i >= ; i--) {
arr[--cnt[H(temp[i])]] = temp[i];
main_ind[cnt[H(temp[i])]] = ind[i];
}
free(temp);
free(ind);
return;
} int *twoSum(int *nums, int numsSize, int target, int *returnSize) {
int *ind = (int *)malloc(sizeof(int) * numsSize);
radix_sort(nums, ind, numsSize);
int p = , q = numsSize - ;
while (nums[p] + nums[q] != target) {
if (nums[p] + nums[q] > target) --q;
else ++p;
}
int *ret = (int *)malloc(sizeof(int) * );
ret[] = ind[p];
ret[] = ind[q];
if (ret[] > ret[]) {
ret[] ^= ret[], ret[] ^= ret[], ret[] ^= ret[];
}
free(ind);
returnSize[] = ;
return ret;
}

还有一种方法,则是哈希表

/*************************************************************************
> File Name: 1-2.cpp
> Author: Mr.Lee
> Mail: 18646139976@163.com
> Created Time: 五 5/10 11:30:27 2019
************************************************************************/ typedef struct Data {
int val, ind;
} typedef struct HashTable {
Data *data;
int *flag;
int size;
} HashTable; HashTable *init(int n) {
HashTable *h = (HashTable *)malloc(sizeof(HashTable));
h->data = (Data *)malloc(sizeof(Data) * n);
h->flag = (int *)calloc(sizeof(int), (n / + ) );
h->size = n;
return h;
} int hash(int val) {
return val & 0x7fffffff;
} int check(HashTable *h, int ind) {
int x = ind / , y = ind % ;
return (h->flag[x] & (1LL << y)) != ;
} void set(HashTable *h, int ind, Data d) {
int x = ind / , y = ind % ;
h->data[ind] = d;
h->flag[x] |= (1LL << y);
return;
} void insert(HashTable *h, int ind, Data d) {
Data d = {val, val_ind};
int ind = hash(val) % h->size;
int time = ;
while (check(h, ind)) {
ind += (time * time);
ind %= h->size;
}
set(h, ind, d);
return;
} int query(HashTable *h, int val) {
int ind = hash(val) % h->size;
int time = ;
while (check(h, ind) && h->data[ind].val != val) {
ind += (time * time);
ind %= h->size;
}
if (check(h, ind)) return h->data[ind].ind;
return -;
} void clear(HashTable *h) {
if (h == NULL) return;
free(h->data);
free(h->flag);
free(h);
return;
} int *twoSum(int *nums, int numsSize, int target, int *returnSize) {
HashTable *h = init(numsSize * );
int *ret = (int *)malloc(sizeof(int) * );
for (int i = , ind; i < numsSize; i++) {
if ((ind = query(h, target - nums[i])) == -) {
insert(h, nums[i], i);
continue;
}
ret[] = ind;
ret[] = i;
break;
}
returnSize[] = ;
return ret;
}

LeetCode刷题:第一题 两数之和的更多相关文章

  1. Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解

    Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全   Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...

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

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

  3. 【LeetCode】1. Two Sum 两数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...

  4. LeetCode(1): 两数之和

    本内容为LeetCode第一道题目:两数之和 # -*- coding: utf-8 -*- """ Created on Sun Mar 10 19:57:18 201 ...

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

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

  6. Leetcode题库——1.两数之和

    @author: ZZQ @software: PyCharm @file: addTwoNumbers.py @time: 2018/9/18 10:35 要求:给定两个非空链表来表示两个非负整数. ...

  7. [LeetCode] Sum of Two Integers 两数之和

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  8. leetcode刷题笔记-1. 两数之和(java实现)

    题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...

  9. LeetCode 刷题笔记 1. 两数之和(Two Sum)

    tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...

  10. (1)leetcode刷题Python笔记——两数之和

    题目如下: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数 ...

随机推荐

  1. 从零开始学spring cloud(六) -------- Ribbon

    一.Ribbon介绍 Ribbon就是客户端侧负责均衡实现的一种方式,那么Ribbon是什么呢? Ribbon是Netflix发布的云中间层服务开源项目,其主要功能是提供客户端侧负载均衡算法.Ribb ...

  2. python的re模块详解

    一.正则表达式的特殊字符介绍 正则表达式 ^ 匹配行首 $ 匹配行尾 . 任意单个字符 [] 匹配包含在中括号中的任意字符 [^] 匹配包含在中括号中的字符之外的字符 [-] 匹配指定范围的任意单个字 ...

  3. $.ajax()参数详解

    来自于<锋利的jQuery(第2版)>. 参数 类型 说明 accepts Map 内容类型发送请求头,告诉服务器什么样的响应会接受返回. 如果accepts设置需要修改,推荐在$.aja ...

  4. Linux驱动之触摸屏程序编写

    本篇博客分以下几部分讲解 1.介绍电阻式触摸屏的原理 2.介绍触摸屏驱动的框架(输入子系统) 3.介绍程序用到的结构体 4.介绍程序用到的函数 5.编写程序 6.测试程序 1.介绍电阻式触摸屏的原理 ...

  5. 阿里云服务器配置phpstudy实现域名访问【图文教程】

    首先,运行phpStudy,确保Apache和MySql启动,绿色代表正常启动状态. 然后配置站点域名,打开phpStudy的站点域名管理,1.设置域名(你有的域名,最后需要域名解析):2.设置文件的 ...

  6. 带缓冲I/O 和不带缓冲I/O的区别与联系

    首先要明白不带缓冲的概念:所谓不带缓冲,并不是指内核不提供缓冲,而是只单纯的系统调用,不是函数库的调用.系统内核对磁盘的读写都会提供一个块缓冲(在有些地方也被称为内核高速缓存),当用write函数对其 ...

  7. java 实现udp通讯

    需求:应用A(通常有多个)和应用B(1个)进行 socket通讯,应用A必须知道应用B的ip地址(在应用A的配置文件中写死的),这个时候就必须把应用B的ip设成固定ip(但是某些时候如更换路由后要重新 ...

  8. win7系统 LR11 安装与破解

    注意一定要以管理员身份运行启动LR程序 Loadrunner11 安装: 1.运行“setup.exe” 2. 点击安装,其中会有提示缺少“Microsoft Visual C++ 2005 SP1等 ...

  9. mysql约束以及数据库的修改

    一.约束 1.约束保证数据完整性和一致性. 2.约束分为表级约束和列级约束. (1)表级约束(约束针对于两个或两个以上的字段使用) (2)列级约束(针对于一个字段使用) 3.约束类型有: (1)NOT ...

  10. English Conversations You Can Download for Free (Spoken English MP3/Audio Files)

    If you want to download free English conversations, you’ve come to the right place. This page introd ...