Rust : codewars的Sum of Pairs
还是见codewars的Sum of Pairs:
一、相关的要求
Given a list of integers and a single sum value, return the first two values (parse from the left please) in order of appearance that add up to form the sum.sum_pairs([11, 3, 7, 5], 10)
# ^--^ 3 + 7 = 10
== [3, 7]sum_pairs([4, 3, 2, 3, 4], 6)
# ^-----^ 4 + 2 = 6, indices: 0, 2 *
# ^-----^ 3 + 3 = 6, indices: 1, 3
# ^-----^ 2 + 4 = 6, indices: 2, 4
# * entire pair is earlier, and therefore is the correct answer
== [4, 2]sum_pairs([0, 0, -2, 3], 2)
# there are no pairs of values that can be added to produce 2.
== None/nil/undefined (Based on the language)sum_pairs([10, 5, 2, 3, 7, 5], 10)
# ^-----------^ 5 + 5 = 10, indices: 1, 5
# ^--^ 3 + 7 = 10, indices: 3, 4 *
# * entire pair is earlier, and therefore is the correct answer
== [3, 7]
Negative numbers and duplicate numbers can and will appear.NOTE: There will also be lists tested of lengths upwards of 10,000,000 elements. Be sure your code doesn't time out.
二、我的解法
use std::collections::HashMap;
fn sum_pairs(ints: &[i8], s: i8) -> Option<(i8, i8)> {// your codelet mut pair: HashMap<i64, Option<(i8, i8)>> = HashMap::new();let mut c = 0_i64;(&ints).into_iter().filter(|&x| {c += 1_i64;let mut indice = 0_64;(&ints[c as usize..]).into_iter().filter(|&y| {indice += 1_i64;match (*y) as i64 + (*x) as i64 == s as i64 {true => {pair.insert(indice + c, Some((*x, *y)));return true;}_ => return false,}}).collect::<Vec<_>>().len() > 0usize}).collect::<Vec<_>>();match pair.len() > 0 {true => {let mut indices: Vec<i64> = pair.keys().into_iter().map(|&x| x).collect();indices.sort();let min_indice = &indices.first().unwrap();//println!("pair:{:?}", pair);//println!("indices:{:?} min_indice :{:?}", indices, min_indice);return *(pair.get(&min_indice).unwrap());}_ => return None,}
}
通过了测试,问题是提交超时,有待优化。
Rust : codewars的Sum of Pairs相关推荐
- LeetCode 561. Array Partition I
题目: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...
- C#LeetCode刷题之#561-数组拆分 I(Array Partition I)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3718 访问. 给定长度为 2n 的数组, 你的任务是将这些数分成 ...
- Leetcode刷题记录[java]——561 Array Partition I
一.前言 二.题561 Array Partition I Given an array of 2n integers, your task is to group these integers in ...
- 程序员面试金典——17.12整数对查找
程序员面试金典--17.12整数对查找 Solution1:针对重复数字的情况题目未做明确说明,虽然此题仍能AC,但有的重复数字用了1次,有的用了超过1次,要求不清晰.重点是这种前后双指针的方法要会! ...
- LeetCode 简单算法题
使用Nodejs 抓取的LeetCode 简单算法题 一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...
- LeetCode 561 Array Partition I(数组划分)
翻译 原文 Given an array of 2n integers, your task is to group these integers into n pairs of integer, s ...
- [LeetCode]561. Array Partition I (数组分区 1)
561. Array Partition I Given an array of 2n integers, your task is to group these integers into n pa ...
- 561.Array Partition I--Python
刚开始学习Python编程,欢迎交流学习! 561.Array Partition I Given an array of 2n integers, your task is to group the ...
- 【刷leetcode,拿Offer-009】561. Array Partition I(贪心,C++)
题目链接 Given an array of 2n integers, your task is to group these integers into n pairs of integer, sa ...
- 561. Array Partition I
原题 Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...
最新文章
- linux 安装删除命令,Linux如何使用命令行卸载安装包
- Gradle 10分钟上手指南
- 基于TPS28225功率MOS半桥电路测试
- Node.js 连接 MySQL 插入 TEXT 类型报错问题
- 学习 launch-editor 源码整体架构,探究 vue-devtools「在编辑器中打开组件」功能实现原理...
- java commons lang 随机数_Apache Common-lang组件里随机数工具类RandomStringUtils的一个bug...
- php 禁用通知,推送消息能不能区分禁止通知和卸载两种类型?
- java - 抽象类、接口、内部类
- PYTHON语言之常用内置函数
- ES6——Map和WeakMap
- 戴尔计算机软件的安装,戴尔笔记本电脑安装软件没反应怎么办
- 用计算机绘制工作表,《计算机操作基础Excel练习题答案.doc
- yocto之相关class总结
- TweenMax逐帧动画
- 关于bitset中的 to_ulong()的解答
- 免费AI标注工具-音频查重工具
- 逻辑思维:5对夫妇握手
- java游戏三国神兽,三国神兽攻略游戏下载_三国神兽攻略手游安卓版下载-我的世界中文网...
- Flutter进阶—通用布局控件
- 【SPSS】因子分析详细操作教程(附案例实战)