【Leetcode】 two sum #1 for rust solution
给定一个整数数组 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的更多相关文章
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 【Leetcode】404. Sum of Left Leaves
404. Sum of Left Leaves [题目]中文版 英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【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 ...
- 【LeetCode】Path Sum(路径总和)
这道题是LeetCode里的第112道题.是我在学数据结构——二叉树的时候碰见的题.题目要求: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...
- 【LeetCode】Maximize Sum Of Array After K Negations(K 次取反后最大化的数组和)
这道题是LeetCode里的第1005道题. 题目描述: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次. ...
- 【LeetCode】404. Sum of Left Leaves 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
随机推荐
- 深入理解 python 虚拟机:pyc 文件结构
深入理解 python 虚拟机:pyc 文件结构 在本篇文章当中主要给大家介绍一下 .py 文件在被编译之后对应的 pyc 文件结构,pyc 文件当中的一个核心内容就是 python 字节码. pyc ...
- gulp基本操作
1.安装淘宝镜像 npm install cnpm -g --registry=https://registry.npm.taobao.org cnpm -v 2.生成项目描述文件 package.j ...
- LeetCode 双周赛 101,DP/中心位贪心/裴蜀定理/Dijkstra/最小环
本文已收录到 AndroidFamily,技术和职场问题,请关注公众号 [彭旭锐] 提问. 大家好,我是小彭. 这周比较忙,上周末的双周赛题解现在才更新,虽迟但到哈.上周末这场是 LeetCode 第 ...
- 【Java SE】IO流
1.File类 ①File类的一个对象代表一个文件或一个文件目录 ②File类声明在java.io下 1.1 FIle类的声明 路径分隔符 Windows和DOS系统默认使用'',UNIX和URL使用 ...
- Linx 阶段一
Linux Linux常用命令 具体演示 1). ls 2). pwd 3). touch 4). mkdir 5). rm 使用技巧 1. 连按 Tab健自动补齐文件名 2. ll 查看当前目录文件 ...
- TENGSHE-OS-渗透测试系统-win11版
下载ISO文件 创建新的虚拟机 VM17 已支持直接创建 win11 x64 稍后安装系统 选中win11 修改路径 win11需要设置8位加密密码 勾选安全引导 根据自身情况选择 默认即可 150G ...
- ntp导致其他线程卡顿原因总结
这个是在项目开发中遇到的一个比较严重的问题,第一影响到主界面的播放卡顿,第二影响到我这边线程同样卡顿,按道理来说两个没有数据交互的线程应该没有任何影响,改为detach模式也没用,最后定位到居然是单独 ...
- 在unity中制作live2d参数的AnimationClip[简单随笔]
假定:已经成功导入模型.相关文档参考:https://github.com/gtf35/live2d_unity_sdk_chinese_document(人力翻译版的Live2d SDK文档) 全部 ...
- 学习Golang时遇到的似懂非懂的概念
背景 这是我学习golang的第三天,大致已经掌握了golang的语法,但是着手开发的时候,却遇到了许多问题,例如golang导包机制.golang的项目管理规范.go mod生成project怎么管 ...
- Godot 4.0 加载为占位符(InstancePlaceholder)的用法和特点
加载为占位符的功能设计上是用来辅助选择性加载场景的.比如一个很庞大的3D世界,玩家一时之间只会处在世界一小部分区域内,同时让整个地图驻留于内存是不现实的,此时需要选择性地灵活加载地图,使用Godot的 ...