letCode-1
日前,使用暴力法破解此题,认为这是很简单的算法,但是所有人都能想出来的算法,凭什么优秀?所以在看到了大神“Grandyang”的博客上精妙的解法,实在是认为自己需要修炼,在此写在这里是为了做笔记,加深理解附上网址:http://www.cnblogs.com/grandyang/p/4130379.html
题目描述:
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。
你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。
示例:
给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
//以下为大神理解
一般来说,我们为了提高时间的复杂度,需要用空间来换,这算是一个trade off吧,我们只想用线性的时间复杂度来解决问题,那么就是说只能遍历一个数字,那么另一个数字呢,我们可以事先将其存储起来,使用一个HashMap,来建立数字和其坐标位置之间的映射,我们都知道HashMap是常数级的查找效率,这样,我们在遍历数组的时候,用target减去遍历到的数字,就是另一个需要的数字了,直接在HashMap中查找其是否存在即可,注意要判断查找到的数字不是第一个数字,比如target是4,遍历到了一个2,那么另外一个2不能是之前那个2,整个实现步骤为:先遍历一遍数组,建立HashMap映射,然后再遍历一遍,开始查找,找到则记录index。
其参考答案:
参考答案一:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
//哈希定义方法现在是这样
hash_map<int, int> m;
vector<int> res;
for (int i = ; i < nums.size(); ++i) {
m[nums[i]] = i; //先遍历一遍数组,建立HashMap映射
}
//然后再遍历一遍,开始查找,找到则记录index
for (int i = ; i < nums.size(); ++i) {
//总值-元素 = 目标值
int t = target - nums[i];
//查找目标值是否存在,在哈希中查找 t 与 i 存在关系,如果m[t]=i 说明是当前数字自己加了两次等于目标值
//hash查找的是key值,也就是nums
if (m.count(t) && m[t] != i) {//if里面的条件用于判断查找到的数字不是第一个数字
res.push_back(i);
res.push_back(m[t]);
break;
}
}
return res;
}
};
参考答案二:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
for (int i = ; i < nums.size(); ++i) {
if (m.count(target - nums[i])) {
return {i, m[target - nums[i]]};
}
m[nums[i]] = i;
}
return {};
}
};
解法一样,第二种较为简洁。
letCode-1的更多相关文章
- letcode刷题之两数相加
letcode里面刷题,坑还是链表不熟,(1)头结点还是有必要设置,否则返回的时候找不到位置:(2)先设置next到新节点再next到下一个节点:都是基础知识 /* * * You are given ...
- [py]letcode第一题求和
letcode第一题, tm的不好弄. 想了很久想到了一个粗蠢的解决办法. Given an array of integers, return indices of the two numbers ...
- (letcode)String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [Letcode] 1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- 迭代法与开根号求值(letcode 69)
p { margin-bottom: 0.25cm; line-height: 120% } 一.理论证明 p { margin-bottom: 0.25cm; line-height: 120% } ...
- 【letcode】5-LongestPalindromicSubstring
回文串 回文串(palindromic string)是指这个字符串无论从左读还是从右读,所读的顺序是一样的:简而言之,回文串是左右对称的.一般求解一个字符串的最长回文子串问题. problem:Lo ...
- letcode code]Maximum Subarray
1 题目: Find the contiguous subarray within an array (containing at least one number) which has the la ...
- letCode(771 Jewels and Stones )
问题描述: You're given strings J representing the types of stones that are jewels, and S representing th ...
- letcode刷题记录-day03-罗马转整数
题目 罗马转整数 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 ...
- letcode刷题记录-day02-回文数
回文数 题目描述 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答 ...
随机推荐
- ssh整合oracle数据源报错????
在SSH整合的时候,引入oracle的pom节点,但是报错,因为没有权限,要手动将Oracle.jar注册到本地仓库 保证你电脑有maven环境,测试 方式为 cmd---->mvn -vers ...
- shell 的多进程
例子 #!/bin/bash temp_fifo_file=$$.info #以当前进程号,为临时管道取名 mkfifo $temp_fifo_file #创建临时管道 exec <>$t ...
- 如何只安装Postgresql client(以9.4 为例)
Install the repository RPM: yum install https://download.postgresql.org/pub/repos/yum/9.4/redhat/rhe ...
- linux命令 常用
1.linux手动连接主机ssh '主机地址' 2.编辑 vi 查看i 编辑esc 退出编辑:wq 保存退出:q 直接退出 3. linux 防火墙 centOS7 firewall-cmd --sa ...
- shell脚本中gsub的应用
(1)文件filename的内容 cat awk_file 1 2 3 $1,200.00 1 2 3 $2,300.00 1 2 3 $4,000.00 (2)去掉第四列的$和,并汇总第四列的和 a ...
- vue eventBus使用
类似于iframe之间的possMessage方式传参 1.eventBus.js文件 //用于兄弟组件通信 import Vue from 'vue'; export default new Vue ...
- Linux-KVM
一.安装 ①直接使用yum安装:yum -yq install qemu-kvm qemu-kvm-tools virt-install qemu-img bridge-utils libvirt v ...
- C++ for循环语句
#include "pch.h" #include<iostream> using namespace std; int main() { int i = 1, sum ...
- git reset 和 git revert 使用区别
git reset 用于回退代码,但是git pull后会和远程分支保持一致,所以无法修改远程代码 git revert可以撤销代码,撤销后直接git push ,可以修改远程分支的代码
- 常用算法和Demo(Java实现)(持续更新)
源码地址:https://github.com/zwxbest/Demo 1.顺序存储和单向链表,双向链表,循环链表的增删查改 https://github.com/zwxbest/Demo/tree ...