目录题目要求思路:模拟Javac++Rust题目要求 思路:模拟 根据题意模拟即可:排序然后只取中间符合条件的数加和然后计算均值;根据给出的数组长度n为20的倍数,5%可直接取n
class Solution {
public double trimMean(int[] arr) {
Arrays.sort(arr);
int n = arr.length, tot = 0;
for (int i = n / 20; i < n - n / 20; i++)
tot += arr[i];
return tot / (n * 0.9);
}
}
class Solution {
public:
double trimMean(vector<int>& arr) {
sort(arr.begin(), arr.end());
int n = arr.size(), tot = 0;
for (int i = n / 20; i < n - n / 20; i++)
tot += arr[i];
return tot / (n * 0.9);
}
};
impl Solution {
pub fn trim_mean(arr: Vec<i32>) -> f64 {
let mut res = arr.clone();
let n = arr.len();
res.sort();
res[(n / 20)..(n - n / 20)].iter().sum::<i32>() as f64 / (n as f64 * 0.9)
}
}
以上就是Java C++ 题解LeetCode1619删除某些元素后数组均值的详细内容,更多关于Java C++ 删除元素后数组均值的资料请关注编程网其它相关文章!
--结束END--
本文标题: JavaC++题解leetcode1619删除某些元素后数组均值
本文链接: https://www.lsjlt.com/news/167501.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0