当前位置:网站首页>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;
}
执行结果如下:

边栏推荐
- UDP servers and clients in go
- [daily question 1] traversal of binary tree
- Top performance version 2 reading notes (IV) -- memory monitoring
- 测试用例设计方法之等价类划分方法
- 降低打新的预期
- Developers, why does the maturity of container technology herald the arrival of cloud native era?
- The third bullet of wechat payment app application for refund
- Crmeb SMS for program configuration of knowledge payment system
- Outsourcing for 3 years is a waste
- 【mysql篇-基础篇】通用语法2
猜你喜欢

How to create a module in the idea and how to delete a module in the idea?

Interviewer: how to solve the problem of massive requests for data that does not exist in redis, which affects the database?

Antd - tree structure: default deployment node attribute failure - Basic promotion

Common settings in idea

练习副“产品”:自制七彩提示字符串展示工具(for循环、if条件判断)

间歇采样转发干扰

Outsourcing for 3 years is a waste

Bytek suffered a disastrous defeat in the interview: he was hanged on one side, but fortunately Huawei pushed him in, and he got an offer on three sides

Use of shortcut keys for idea tools

Exercise "product": self made colorful Prompt string display tool (for loop and if condition judgment)
随机推荐
Top performance version 2 reading notes (IV) -- memory monitoring
阿于的彩虹桥
DL: deep learning model optimization model training skills summary timely automatic adjustment of learning rate implementation code
How did the data center change from "Britney Spears" to "Mrs. bull"?
Modbus TCP RTU protocol chart
Is there any discount for securities account opening? Is it safe to open an account online?
MySQL advanced 1
开发者,为什么说容器技术的成熟预示着云原生时代的到来?
Command line Basics
MySQL deadlock
如何在IDEA中自定義模板、快速生成完整的代碼?
MySQL basics 1
Time flies that year
Crmeb SMS for program configuration of knowledge payment system
Ml: introduction to confidence interval (the difference and relationship between precision / accuracy / accuracy), use method, and detailed introduction to case application
岁月匆匆那年
Distributed task scheduling elasticjob demo
如何在IDEA中创建Module、以及怎样在IDEA中删除Module?
出门在外保护好自己
xshell中怎么切换到root用户