Rust : codewars的Sum of Pairs_rust

來源:互聯網
上載者:User

還是見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 code    let 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,    }}

通過了測試,問題是提交逾時,有待最佳化。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.