从0开始的LeetCode生活—001-Two Sum
题目:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
题目大意:
给你一个数组nums和一个数target,找出数组里面两个相加等于target的数并返回其下标。比如数组nums为[2,7,11,15],target=9,因为2+7等于9,2的下标为0,7的下标为1,所以返回0和1.
解法:
方法一:
最容易想到的方法就是遍历整个数组,嵌套循环,先取出nums里面的一个数然后循环分别加加看后面的数,看是否等于target,这种方法的时间复杂度为o(n2)。代码如下:
int* twoSum(int* nums, int numsSize, int target) { ); ;i<numsSize;++i) ;j<numsSize;++j) if(nums[i]+nums[j]==target){ ans[]=i; ans[]=j; return ans; } return ans; }
方法二:
原本应该是属于数据结构哈希表的方法,但是我还不会写哈希表,所以就利用了数组下标(和哈希表原理一样,但是这样会造成大量空间浪费)。我们从数组里面第一个数开始看,拿到一个数就看一下哈希表内target-当前数字的那个数是否存在。这个方法的时间复杂度为O(n)。代码如下:
int* twoSum(int* nums,int numsSize,int target){ *sizeof(int)); memset(fuck,-,*sizeof(int)); *sizeof(int)); ;i<numsSize;i++){ ]==-) fuck[nums[i]+]=i; else{ ans[]=fuck[target-nums[i]+]; ans[]=i; free(fuck); return ans; } } return ans; }
说明:Fuck存储了哈希表,对于不存在的元素就标记其下标,因为下标不能为负数,所以我以40000为0.
我写的代码还有缺陷,就是如果给的数组里面如果有-1,这个代码就出错了,也就是说我刚好避开了测试数据。运气好。
从0开始的LeetCode生活—001-Two Sum的更多相关文章
- 从0开始的LeetCode生活—9. Palindrome Number(回文数)
题目大意: 判断输入的数字是不是回文数.所谓回文数就是正反读都一样的数字,比如说11,121,1221这样子的数字.负数不会是回文数. 解题思路: 思路一:如果这个数是负数,则返回false,否则用一 ...
- 从0开始的LeetCode生活—461-Hamming Distance(汉明距离)
题目: The Hamming distance between two integers is the number of positions at which the corresponding ...
- 【LeetCode】001. Two Sum
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
- [从 0 开始的 Angular 生活]No.38 实现一个 Angular Router 切换组件页面(一)
前言 今天是进入公司的第三天,为了能尽快投入项目与成为团队可用的战力,我正在努力啃官方文档学习 Angular 的知识,所以这一篇文章主要是记录我如何阅读官方文档后,实现这个非常基本的.带导航的网页应 ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- leetcode 练习1 two sum
leetcode 练习1 two sum whowhoha@outlook.com 问题描述 Given an array of integers, return indices of the tw ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【一天一道LeetCode】#303.Range Sum Query - Immutable
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
随机推荐
- 一:学习Linux前准备工作
1:虚拟机,网站上有很多种类的虚拟机,找一款适合自己用的.我这里使用的是 Virtual Box 下载地址:https://www.virtualbox.org/ 安装虚拟机 一路 Next 就可以. ...
- 用css以写代码形式画一个皮卡丘
我的个人网站是通过写代码的形式来形成一个网站的,前一阵子在某个大神的github上看到他用写代码的形式来完成一个皮卡丘,于是心血来潮花了半个小时,也完成了一个作品. 这其中涉及到的知识点也不是很复杂, ...
- js常见排序
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- 【BZOJ3160】万径人踪灭(FFT,Manacher)
[BZOJ3160]万径人踪灭(FFT,Manacher) 题面 BZOJ 题解 很容易想到就是满足条件的子序列个数减去回文子串的个数吧... 至于满足条件的子序列 我们可以依次枚举对称轴 如果知道关 ...
- 【BZOJ3884】上帝与集合的正确用法(欧拉定理,数论)
[BZOJ3884]上帝与集合的正确用法(欧拉定理,数论) 题面 BZOJ 题解 我们有欧拉定理: 当\(b \perp p\)时 \[a^b≡a^{b\%\varphi(p)}\pmod p \] ...
- Bzoj2337:[HNOI2011]XOR和路径
题面 bzoj Sol 设\(f[i]\)表示\(i到n\)的路径权值某一位为\(1\)的期望 枚举每一位,高斯消元即可 不要问我为什么是\(i\ - \ n\)而不可以是\(1\ - \ i\) # ...
- [BZOJ1024] [SCOI2009] 生日快乐 (搜索)
Description windy的生日到了,为了庆祝生日,他的朋友们帮他买了一个边长分别为 X 和 Y 的矩形蛋糕.现在包括windy,一共有 N 个人来分这块大蛋糕,要求每个人必须获得相同面积的蛋 ...
- [BZOJ3751] [NOIP2014] 解方程 (数学)
Description 已知多项式方程:$a_0+a_1*x+a_2*x^2+...+a_n*x^n=0$ 求这个方程在[1,m]内的整数解(n和m均为正整数). Input 第一行包含2个整数n.m ...
- PHP 秒数 转时分秒 函数
function secondsToHour($seconds){ if(intval($seconds) < 60) $tt ="00时00分".sprintf(" ...
- 将PPT文件内容转换为图片放在Email邮件正文中发送
通过Email推送统计报告.一般除了要求将PPT报告文件作为附件发给用户,同时希望将报告内容在邮件中直观展示. 一份统计报告中经常包含柱状图.饼图.好看的图表,这些信息要直接在Email中展示比较复杂 ...