给定一个整数数组 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. sqlmap的基本使用

    sqlmap的基本使用 一.注入 1.查询是否存在注入点 sqlmap -u "http://www.xx.com?id=x" --dbs 检测有哪些数据库 --current-d ...

  2. [ACM]STL-dfs

    #include<iostream> using namespace std; int book[101],sum,n,e[101][101]; void dfs(int cur) { c ...

  3. 如何将 CentOS 8 转换为 CentOS Stream

    CentOS 未来是不会更新数字版本了.CentOS 项目组,未来会变更为Stream版本,也就是俗称的滚动版本,那么如何将数字版本升级为滚动版本呢? 若需要将其转换为滚动版本,那么即可参考本文进行升 ...

  4. [Windows]BAT脚本自定义函数

    1 helloworld @echo off call :helloworld helloworld goto :EOF :helloworld setlocal echo %1 endlocal&a ...

  5. 和 chatgpt 聊了一会儿分布式锁 redis/zookeeper distributed lock

    前言 最近的 chatGPT 很火爆,听说取代程序员指日可待. 于是和 TA 聊了一会儿分布式锁,我的感受是,超过大部分程序员的水平. Q1: 谈一谈 java 通过 redis 实现分布式 锁 ch ...

  6. 派生,super 多态与多态性 组合

    派生的方法与重用: 方法一:指名道姓的调用某一类函数 >>> class Teacher(People): ... def __init__(self,name,sex,age,ti ...

  7. Disruptor-源码解读

    前言 Disruptor的高性能,是多种技术结合以及本身架构的结果.本文主要讲源码,涉及到的相关知识点需要读者自行去了解,以下列出: 锁和CAS 伪共享和缓存行 volatile和内存屏障 原理 此节 ...

  8. 回车,换行,转义字符“\r”,“\n”是什么关系?

    1."回车"这个名词的来历. 关于"回车键"的来历,还得从机械英文打字机说起.在机械英文打字机上,有一个部件叫"字车",每打一个字符(原为单 ...

  9. 开心档之MySQL 复制表

    MySQL 复制表 如果我们需要完全的复制MySQL的数据表,包括表的结构,索引,默认值等. 如果仅仅使用CREATE TABLE ... SELECT命令,是无法实现的. 本章节将为大家介绍如何完整 ...

  10. LeetCode 周赛 342(2023/04/23)容斥原理、计数排序、滑动窗口、子数组 GCB

    本文已收录到 AndroidFamily,技术和职场问题,请关注公众号 [彭旭锐] 提问. 大家好,我是小彭. 前天刚举办 2023 年力扣杯个人 SOLO 赛,昨天周赛就出了一场 Easy - Ea ...