我试图在代码战中解决这个问题。 https://www.codewars.com/kata/are-they-the-same/
我写了下面的代码。
fn comp(mut a: Vec<i64>, mut b: Vec<i64>) -> bool {
a.sort();
b.sort();
a = a.iter().map(|x| x * x).collect();
a == b
}
判断系统说有一个输入,我的代码返回 false 但应该是 true。我不明白这是什么问题。
我看到了其他人通过所有测试的答案。这是其中之一。
fn comp(a: Vec<i64>, b: Vec<i64>) -> bool {
let mut a1 = a.iter().map(|&x| x * x).collect::<Vec<_>>();
let mut a2 = b;
a1.sort();
a2.sort();
a1 == a2
}
平方和排序的顺序重要吗?
最佳答案
很可能某些输入包含负数,因此顺序很重要。例如。 -2 < -1
,但是(-2)² > (-1)²
.
关于algorithm - 我无法理解 Rust 代码的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58799246/