给定一个整数数组 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. cookie时效无限延长方案

    作者:京东科技 刘清洁 1.痛点(*) 自动化测试有2种形式,接口自动化和UI自动化.而UI自动化经常会被登录节点堵塞,例如验证码.图形.滑块等,尽管有些方式可以识别图形和定位滑块位置,但成功率都不高 ...

  2. vue之列表渲染v-for

    目录 简介 用法 v-for可循环的几种变量的展示 数组的循环展示 对象的循环展示 字符串的循环展示 数字的循环展示 v-for搭档key值的说明 js循环几种方式 基于索引的循环 数组的循环 数组基 ...

  3. Zookeeper的深入分析

    运⾏时状态分析 在ZAB协议的设计中,每个进程都有可能处于如下三种状态之⼀ · LOOKING:Leader选举阶段. · FOLLOWING:Follower服务器和Leader服务器保持同步状态. ...

  4. ThreadLocal、进程VS线程、分布式进程

    1.ThreadLocal变量是一个全局变量,每个线程只能读取自己的独立副本,ThreadLocal解决了一个线程中各个函数之间的传递问题 import threading local_school ...

  5. mysql导出csv

    1.正常查询 SELECT a.emp_no '员工号',b.seq '文章序号' from vote_records a INNER JOIN vote_content b ON a.vote_co ...

  6. 创建SpringBoot项目,在yml中配置数据库, driver-class-name: com.mysql.cj.jdbc.Driver标红报错解决方式

    一.报错原因 com.mysql.cj.jdbc.Driver一直标红报错,原因在于pom.xml中mysql包没有下载下来,或者在创建项目的时候有问题 二.解决方案 在pom.xml添加 <d ...

  7. springboot项目 宿舍管理系统 (源码+数据库文件+1w字论文+ppt)

    来了就点个赞再走呗,即将毕业的兄弟有福了文章底部获取源码springboot项目 宿舍管理系统 (源码+数据库文件+1w字论文+ppt)技术框架:java+springboot+vue+mysql后端 ...

  8. Gradio入门到进阶全网最详细教程[一]:快速搭建AI算法可视化部署演示(侧重项目搭建和案例分享)

    Gradio入门到进阶全网最详细教程[一]:快速搭建AI算法可视化部署演示(侧重项目搭建和案例分享) 常用的两款AI可视化交互应用比较: Gradio Gradio的优势在于易用性,代码结构相比Str ...

  9. LeetCode刷题之652寻找重复的子树

    继续每日分享一道算法题,监督自己学习,不落下算法,有需要一起打卡的uu,可以一起加油呀! 好了,现在开始看题了哈: 给定一棵二叉树 root,返回所有重复的子树. 对于同一类的重复子树,你只需要返回其 ...

  10. Prism Sample 13-IActiveAwareCommands

    本例和12的唯一区别,仅仅是在ViewModel中增加了一个IActiveAware,这决定了只有在Acitve状态的视图中才会执行自己ViewModel中的命令.