给定一个整数数组 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. LeetCode 周赛 338,贪心 / 埃氏筛 / 欧氏线性筛 / 前缀和 / 二分查找 / 拓扑排序

    本文已收录到 AndroidFamily,技术和职场问题,请关注公众号 [彭旭锐] 提问. 大家好,我是小彭. 上周末是 LeetCode 第 338 场周赛,你参加了吗?这场周赛覆盖的知识点很多,第 ...

  2. 垃圾回收之CMS、G1、ZGC对比

    ZGC(The Z Garbage Collector)是JDK 11中推出的一款低延迟垃圾回收器,它的设计目标包括: 停顿时间不超过10ms: 停顿时间不会随着堆的大小,或者活跃对象的大小而增加: ...

  3. 在EF Core中为数据表按列加密存储

    假设有User表 public class User : Entity<int> { public int Id { get; set; } public string UserName ...

  4. LeeCode 二叉树问题(四)

    二叉搜索树的应用问题 二叉搜索树的定义 若左子树不空,则左子树上所有节点的值均小于根节点的值 若右子树不空,则右子树上所有节点的值均大于根节点的值 它的左右子树也均为二叉搜索树 中序遍历结果为一个升序 ...

  5. 通过 iframe 调用 天气预报&jsonp

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 使用vue-cli创建第一个vue项目

    命令提示符切换至需要创建项目的目录: 直接在路径输入cmd在按键盘的enter键打开的终端就直接切换到该目录下 (1)输入以下命令: vue create 项目名称 (2)我这里选手动选择,键盘上下按 ...

  7. ROS机器人摄像头寻线

    ROS机器人摄像头寻线 连接小车 注意:必须在同一区域网 ssh clbrobort@clbrobort 激活树莓派主板 roslaunch clbrobot bringup.launch 开启摄像头 ...

  8. Linux下ftp常见问题总结

    Linux下ftp常见问题总结 似乎拖欠了几篇文章了@_@,来公司半年了,成长了不少!从大学毕业,直到看到http://blog.csdn.net/leixiaohua1020  雷霄骅(然而天妒英才 ...

  9. [OpenCV-Python] 20 图像金字塔

    文章目录 OpenCV-Python:IV OpenCV中的图像处理 20 图像金字塔 20.1 原理 20.2 使用金字塔进行图像融合 OpenCV-Python:IV OpenCV中的图像处理 2 ...

  10. mysql-高级功能(触发器、存储过程、视图、事务)

    目录 触发器-created trigger 1.触发器分为六种情况 2.语法结构 3.创建触发器 4.查询/删除触发器 存储过程-created procedure 1.创建存储过程 2.使用存储过 ...