浏览代码

Another problem done with Rust

master
Lachlan Jacob 5 年前
父节点
当前提交
bb272ad824
共有 3 个文件被更改,包括 68 次插入0 次删除
  1. 27
    0
      1394/main.rs
  2. 36
    0
      1394/problem.txt
  3. 5
    0
      1394/run.sh

+ 27
- 0
1394/main.rs 查看文件

@@ -0,0 +1,27 @@
use std::collections::HashMap;

pub fn find_lucky(arr: Vec<i32>) -> i32 {
let mut hm = HashMap::<i32, i32>::new();
for n in arr {
let entry = hm.entry(n).or_insert(0);
*entry += 1;
}
let mut lucky = Vec::new();
for (k, v) in hm {
if k == v {
lucky.push(k);
}
}
let mut max = -1;
for n in lucky {
if n > max {
max = n;
}
}
return max;
}

pub fn main() {
println!("Expected: 4");
println!("Got: {}", find_lucky(vec![1, 4, 4, 4, 4, 2, 6]));
}

+ 36
- 0
1394/problem.txt 查看文件

@@ -0,0 +1,36 @@
Given an array of integers arr, a lucky integer is an integer which has a frequency in the array equal to its value.

Return a lucky integer in the array. If there are multiple lucky integers return the largest of them. If there is no lucky integer return -1.

Example 1:

Input: arr = [2,2,3,4]
Output: 2
Explanation: The only lucky number in the array is 2 because frequency[2] == 2.

Example 2:

Input: arr = [1,2,2,3,3,3]
Output: 3
Explanation: 1, 2 and 3 are all lucky numbers, return the largest of them.

Example 3:

Input: arr = [2,2,2,3,3]
Output: -1
Explanation: There are no lucky numbers in the array.

Example 4:

Input: arr = [5]
Output: -1

Example 5:

Input: arr = [7,7,7,7,7,7,7]
Output: 7

Constraints:

1 <= arr.length <= 500
1 <= arr[i] <= 500

+ 5
- 0
1394/run.sh 查看文件

@@ -0,0 +1,5 @@
#!/bin/bash

rustc main.rs
./main
rm main

正在加载...
取消
保存