代码如下:
 /**
* 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. redis+Keepalived主从热备切换实例

    [root@cache-redis-~/]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived globa ...

  2. ThinkPHP学习笔记

    1.什么是框架? 特征一:是一对代码的集合: 特征二:一个半成品的应用: 特征三:包含了一些优秀的设计模式: 定义:框架是一堆包含了常量.方法和类等代码的集合,它是一个半成品的应用,只包含了一些项目开 ...

  3. 牛客小白月赛13 小A的最短路(lca+RMQ)

    链接:https://ac.nowcoder.com/acm/contest/549/F来源:牛客网 题目描述 小A这次来到一个景区去旅游,景区里面有N个景点,景点之间有N-1条路径.小A从当前的一个 ...

  4. MAC终端如何使用rar和unrar

    一.MAC具体安装见下面两个博客分享: Homebrew介绍和使用:https://www.jianshu.com/p/de6f1d2d37bf Mac 压缩 / 解压缩工具解决方案:https:// ...

  5. sql转百分比并保留两位小数

    --转百分比并保留两位小数 select ProfitRate =Convert(nvarchar(20), (Convert(decimal(18,2),((DayPrice -MyPrice)*1 ...

  6. ES查询-term VS match (转)

    原文地址:https://blog.csdn.net/sxf_123456/article/details/78845437 elasticsearch 中term与match区别 term是精确查询 ...

  7. 压缩软件WinRar 5.5 x64去广告方式【窗口类名下断】

    工具及使用软件逆向逻辑原始软件使用效果:查看软件窗口类名查看WinRAR.exe信息x64dbg逆向破解软件(非附加调试)处理掉广告注册函数处理掉广告创建函数保存修改后的镜像破解效果 工具及使用软件 ...

  8. Redis:MySQL算老几?

    原创: 码农翻身刘欣 前言:上一篇<MySQL:缓存算什么东西?>里挖了一个坑,也有很多人说没看过瘾,今天接着写,把坑填上,不过得把视角换一下,让Redis上台发言. 我知道MySQL看我 ...

  9. web自动化上传附件 2

    当我们进行某一项web自动化脚本编写时,有上传附件操作,点击附件直接打开了windows窗口,而有的点击添加附件打开一个小窗体,再点击‘浏览’才打开windows窗口, 中间多了这么一个小窗体的操作, ...

  10. Linux permission denied问题

    初试Linux 本来想试一试递归的mkdir -p结果遇到了permission denied,但是自己明明是管理员啊 查了博客,在文件名前加上chmod 777 ,例如chmod 777  temp ...