从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 ...
随机推荐
- jquery 记住账号 记住密码
<body> <label><input type="checkbox" onclick="loginBtn_user()" /& ...
- 【BZOJ2152】聪聪可可(点分治)
[BZOJ2152]聪聪可可(点分治) 题面 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电 ...
- 【CJOJ2484】【Luogu2805】最小函数值(函数最小值)
题面 Description 有n个函数,分别为F1,F2,...,Fn.定义 Fi(x)=Aix2+Bix+Ci(x∈N∗)Fi(x)=Aix2+Bix+Ci(x∈N∗) .给定这些Ai.Bi和Ci ...
- flask项目部署到阿里云 ubuntu16.04
title: flask项目部署到阿里云 ubuntu16.04 date: 2018.3.6 项目地址: 我的博客 部署思路参考: Flask Web开发>的个人部署版本,包含学习笔记. 开始 ...
- C++学习-9
友元主要用于访问私有变量,友元函数跟所在位置的权限没有任何关系friend+函数声明 友元类通常设计为一种对数据操作或类之间传递消息的辅助类(注意一下顺序) Explicit就是要求严格的匹配,不允许 ...
- Scala学习笔记(二)
Scala面向对象: 类: 声明类(一个源文件中可以包含很多public的类) getter和setter 构造函数(主构造器和附属构造器) 继承(extends) 重写父类方法(override d ...
- (转)regex类(个人理解)
regex类(个人理解) C#regex是正则表达式类用于string的处理,查找匹配的字符串.1,先看一个例子Regex regex=new Regex(@”OK“)://我们要在目标字符串中找 ...
- Problem : (1.2.1) Text Reverse
#include<iostream> using namespace std; void main() { char arr[1000]; int a,n; int s,t; cin> ...
- WordPress评论时一键填入昵称、邮箱和网址
现在很多博客都启用了多说,可是依然有很多博主坚守着wordpress或其主题自带的评论框,这样,每当我们访问这些博客时,发现精彩的内容或者 找到共鸣时.抑或只是想挑逗一下博主,准备在评论处爽爽的来一发 ...
- CMake基本语法
CMake简介 CMake 是做什么的? CMake是一套类似于automake的跨平台辅助项目编译的工具. 我觉得语法更加简单易用. CMake的工作流程 CMake处理顶级目录的CMakeList ...