当前位置:网站首页>2022-06-29:x = { a, b, c, d }, y = { e, f, g, h }, x、y两个小数组长度都是4。 如果有: a + e = b + f = c + g = d + h
2022-06-29:x = { a, b, c, d }, y = { e, f, g, h }, x、y两个小数组长度都是4。 如果有: a + e = b + f = c + g = d + h
2022-06-30 00:43:00 【福大大架构师每日一题】
2022-06-29:x = { a, b, c, d },
y = { e, f, g, h },
x、y两个小数组长度都是4。
如果有: a + e = b + f = c + g = d + h,
那么说x和y是一个完美对。
题目给定N个小数组,每个小数组长度都是K。
返回这N个小数组中,有多少完美对。
来自阿里。
答案2022-06-29:
找特征,存map,求数组相邻数字的差值,组装成key,value是数量。
代码用rust编写。代码如下:
use std::collections::HashMap;
fn main() {
let sc: Vec<i32> = vec![5, 3, 2, 11, 21, 19, 10, 1, 20, 11, 1, 6, 15, 24, 18, 27, 36];
let mut ii: i32 = 0;
while ii < sc.len() as i32 {
let n = sc[ii as usize];
ii += 1;
let m: i32 = sc[ii as usize];
ii += 1;
let mut matrix: Vec<Vec<isize>> = vec![];
for i in 0..n {
matrix.push(vec![]);
for _ in 0..m {
matrix[i as usize].push(sc[ii as usize] as isize);
ii += 1;
}
}
println!("matrix = {:?}", matrix);
let ans = perfect_pairs(&mut matrix);
println!("ans = {}", ans);
}
}
fn perfect_pairs(matrix: &mut Vec<Vec<isize>>) -> isize {
let mut ans: isize = 0;
// key : 字符串 特征,差值特征 : "_5_-2_6_9"
let mut counts: HashMap<String, isize> = HashMap::new();
for arr in matrix.iter() {
let mut self0: String = String::new();
let mut minus: String = String::new();
for i in 1..arr.len() as isize {
self0.push_str(
&(String::from("_") + &format!("{}", arr[i as usize] - arr[(i - 1) as usize])),
);
minus.push_str(
&(String::from("_") + &format!("{}", arr[(i - 1) as usize] - arr[i as usize])),
);
}
ans += match counts.get(&minus) {
Some(v) => *v,
None => 0,
};
counts.insert(
self0.clone(),
match counts.get(&self0) {
Some(v) => (*v) + 1,
None => 1,
},
);
}
return ans;
}
执行结果如下:

边栏推荐
- 【服装软件】服装出产办理体系选型的准则有哪些?
- About the case of SQL statements
- How did the data center change from "Britney Spears" to "Mrs. bull"?
- modbus-tcp-rtu协议图表
- 间歇采样转发干扰
- Intermittent sampling and forwarding interference
- MySQL deadlock
- The third bullet of wechat payment app application for refund
- 优秀的测试/开发程序员与普通的程序员对比......
- 干外包3年,真废了...
猜你喜欢

外包干了三年,废的一踏糊涂...

Outsourcing for 3 years is a waste

网工常见面试题分享:Telnet、TTL、路由器与交换机

TwinCAT 3 EL7211模塊控制倍福伺服

How did the data center change from "Britney Spears" to "Mrs. bull"?

IDEA工具快捷键的使用

在线SQL转CSV工具
![[mrctf2020]ezpop-1 | PHP serialization](/img/f8/6164b4123e0d1f3b90980ebb7b4097.png)
[mrctf2020]ezpop-1 | PHP serialization
![[MRCTF2020]Ezpop-1|php序列化](/img/f8/6164b4123e0d1f3b90980ebb7b4097.png)
[MRCTF2020]Ezpop-1|php序列化

Nested call and chained access of functions in handwritten C language
随机推荐
Initial i/o and its basic operations
股票网上开户及开户流程怎样?还有,在线开户安全么?
炒股开户选择哪家券商公司比较好哪家平台更安全
降低打新的预期
【three.js】WEB3D初次体验
中小企业签署ERP合同时,需要注意这几点
练习副“产品”:自制七彩提示字符串展示工具(for循环、if条件判断)
Lower expectations
The third bullet of wechat payment app application for refund
MySQL basics 1
Video to image -cv2 Videocapture() usage
MySQL foundation 3
How about stock online account opening and account opening process? Also, is it safe to open an account online?
Which department should the company's fixed assets be managed? How should the company's fixed assets be managed
【PHP】php压测,报错:通常每个套接字地址(协议/网络地址/端口)只允许使用
出门在外保护好自己
【服装软件】服装出产办理体系选型的准则有哪些?
MySQL deadlock
Experience of C language course design: open source sharing of "push box" course design works
Relevance - canonical correlation analysis