给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。

 

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

Rust Solution:

 1     pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
2 let mut hashmap = HashMap::new();
3 let mut vec: Vec<i32> = Vec::new();
4 for (value1, &item ) in nums.iter().enumerate() {
5 let temp = target - item;
6 if let Some(&value2) = hashmap.get(&temp) {
7 vec.push(value2 as i32);
8 vec.push(value1 as i32);
9 }
10 hashmap.insert(item, value1);
11 }
12 vec
13 }

C++ Solution:

 1     vector<int> twoSum(vector<int>& nums, int target) {
2 unordered_map<int, int> _map;
3 for(int i = 0; i < nums.size(); i++)
4 {
5 int temp = target - nums[i];
6 if(_map.find(temp) != _map.end()) {
7 return { _map[temp], i};
8 }
9 _map[nums[i]] = i;
10 }
11 return {};
12 }

Python Solution:

1 def twoSum(self, nums: List[int], target: int) -> List[int]:
2 dict = {};
3 for i in range(len(nums)):
4 if (target - nums[i]) in dict :
5 return [dict[target - nums[i]], i]
6 dict[nums[i]] = i

Github link : https://github.com/DaviRain-Su/leetcode_solution/tree/master/two_sum

【Leetcode】 two sum #1 for rust solution的更多相关文章

  1. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  2. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  3. 【LeetCode】633. Sum of Square Numbers

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...

  4. 【Leetcode】404. Sum of Left Leaves

    404. Sum of Left Leaves [题目]中文版  英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...

  5. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  6. 【leetcode】Path Sum IV

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  7. 【LeetCode】Path Sum(路径总和)

    这道题是LeetCode里的第112道题.是我在学数据结构——二叉树的时候碰见的题.题目要求: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...

  8. 【LeetCode】Maximize Sum Of Array After K Negations(K 次取反后最大化的数组和)

    这道题是LeetCode里的第1005道题. 题目描述: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次. ...

  9. 【LeetCode】404. Sum of Left Leaves 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...

  10. 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

随机推荐

  1. 通过Navicat导入SQLServer的MDF文件和LDF文件

    新建查询运行: EXEC  sp_attach_db  @dbname  =  '你的数据库名',      @filename1  =  'mdf文件路径(包缀名)',      @filename ...

  2. STM32F407 学习 (0) 各种外设功能 (上)

      本文对正点原子STM32F4探索者的基本功能及外设作最基本的介绍,随笔者本人的学习进程(基本按照正点原子)而不定时更新,起到总结的作用. 一.HAL库编写程序的运行逻辑   HAL库函数(如stm ...

  3. Ubuntu+uWSGI部署基于Django的API【鸿篇巨制,事无巨细】

    背景 任务: 视频翻译项目需要在两个服务器上进行通信(国内&海外的阿里服务器). 因为python是主语言,选用了Django 来快速部署API. 注:Django中文文档:https://d ...

  4. [Linux]Linux中安装软件的方式?

    近日处理安全漏洞时,出现了这样一个问题: 判断某软件组件是通过何种方式安装的. 知道是何种方式安装,才方便做进一步的解决(升级/配置/卸载等操作) 1 解压即用 例如: sublime_text.py ...

  5. [大数据]Hadoop简述

    1 Hadoop:发展沿革 摘要:1个人(Doug Cutting).2个公司(Google.Cloudera) 1.1 渊源 Hadoop项目 最初开发者/创始者: Doug Cutting(道格· ...

  6. 洛谷:P5716日份天数

    题目描述 输入年份和月份,输出这一年的这一月有多少天.需要考虑闰年. 输入格式 输入两个正整数,分别表示年份 \(y\) 和月数 \(m\),以空格隔开. 输出格式 输出一行一个正整数,表示这个月有多 ...

  7. javasec(六)RMI

    这篇文章介绍java-RMI远程方法调用机制. RMI全称是Remote Method Invocation,远程⽅法调⽤.是让某个Java虚拟机上的对象调⽤另⼀个Java虚拟机中对象上的⽅法,只不过 ...

  8. OpenFec介绍

    官网: http://openfec.org/accueil.html   1.提供的编解码器 Reed-Solomon stable codec over GF(28)                ...

  9. 笔记:C++学习之旅 ---string 类、vector和迭代器

    string 类 #include <iostream> #include <string> using namespace std; int main() {         ...

  10. ES6必会重点汇总

    当下的前端开发已经成为一项非常流行的技能.在这个领域中,ES6是一个重要的主题.ES6是ECMAScript 2015的缩写,是JavaScript语言的下一个版本,引入了很多新的语言特性和API,让 ...