LeetCode刷题:第一题 两数之和
代码如下:
/**
* 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刷题:第一题 两数之和的更多相关文章
- Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解
Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全 Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- 【LeetCode】1. Two Sum 两数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...
- LeetCode(1): 两数之和
本内容为LeetCode第一道题目:两数之和 # -*- coding: utf-8 -*- """ Created on Sun Mar 10 19:57:18 201 ...
- Leetcode#1.Two Sum(两数之和)
题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], ta ...
- Leetcode题库——1.两数之和
@author: ZZQ @software: PyCharm @file: addTwoNumbers.py @time: 2018/9/18 10:35 要求:给定两个非空链表来表示两个非负整数. ...
- [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 ...
- leetcode刷题笔记-1. 两数之和(java实现)
题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...
- LeetCode 刷题笔记 1. 两数之和(Two Sum)
tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...
- (1)leetcode刷题Python笔记——两数之和
题目如下: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数 ...
随机推荐
- 【Spring学习】Spring的源码解析之路
缘起:=====>>>> 在项目中实际上是用到了Spring的内容,只是直接用的SpringBoot,不管是Eclipse中还是在Intellig IDEA中,应该都比较容易能 ...
- (4)Linux常用基本操作
1.ping和traceroute 指定源IP ping:ping -I 源 目的 #I为大写的i 带源地址路由tracert:traceroute -d <目标地址> -s < ...
- MySQL主备
服务器基本环境: 两台centos6.4.iptables diabled .selinux disabled 两台的hosts解析 #yum install mysql -y //这个一定要装 ...
- 获取网页title(还有一坑未填)
def getTitle(self,url): #get title title = 'time out' try: self.res = requests.get(url,timeout=5) so ...
- 处理ajax数据;数据渲染
当我们用ajax把数据拿到前台,该如何渲染到页面,有以下几种方式: 一:使用字符串拼接的方法 声明一个空变量,然后拼接 var st=""; st+="<div&g ...
- 面试简单整理之IO
1.字节流,字符流 整个Java IO体系都是基于字节流(InputStream/OutputStream) 和 字符流(Reader/Writer)作为基类,根据不同的数据载体或功能派生出来的. 2 ...
- pytest 一.安装和使用入门
pytest --fixtures Python版本: Python 2.7.3.4.3.5.3.6.Jython.PyPy-2.3 平台:Unix / Posix和windows PyPI包名称:p ...
- Python之路(第三十二篇) 网络编程:udp套接字、简单文件传输
一.UDP套接字 服务端 # udp是无链接的,先启动哪一端都不会报错 # udp没有链接,与tcp相比没有链接循环,只有通讯循环 server = socket.socket(socket.AF_I ...
- IDEA集成git方法
一.IDEA集成git方法 首先idea集成git我们需要先下载一个小软件,git bash 地址:https://git-scm.com/downloads .下载好了之后直接下一步下一步傻瓜试 ...
- java34
局部内部类:定义在方法中的类 -1局部类中可引用局部变量(定义在方法中的变量),但是局部变量必须已经初始化, 因为局部变量前默认带着final. 2.局部内部类的东西只能在定义的方法中使用(在方法中创 ...