当前位置:网站首页>LeetCode每日一题(1362. Closest Divisors)
LeetCode每日一题(1362. Closest Divisors)
2022-07-03 09:01:00 【wangjun861205】
Given an integer num, find the closest two integers in absolute difference whose product equals num + 1 or num + 2.
Return the two integers in any order.
Example 1:
Input: num = 8
Output: [3,3]
Explanation: For num + 1 = 9, the closest divisors are 3 & 3, for num + 2 = 10, the closest divisors are 2 & 5, hence 3 & 3 is chosen.
Example 2:
Input: num = 123
Output: [5,25]
Example 3:
Input: num = 999
Output: [40,25]
Constraints:
- 1 <= num <= 10^9
最优解当然是 n² = num + 1 或者 n² = num + 1, 因为 1 <= num <= 1000000000, 那我们的 n 自然不会大于 40000, 有了这个条件托底, 我们就算用最简单的方法从 40000 一路试到 1 也可以, 当然, 最好是把 n 先判断出来
impl Solution {
pub fn closest_divisors(num: i32) -> Vec<i32> {
if num == 1 {
return vec![1, 2];
}
let num = num as i64;
// 指数
let mut e = 1u32;
loop {
if 2i64.pow(e) >= num {
break;
}
e += 1;
}
// 计算出n的上限
let n = 2i64.pow(e / 2 + e % 2);
let mut a = i64::MAX;
let mut b = 0;
for v in (1..=n).rev() {
if (num + 1) % v == 0 {
if ((num + 1) / v - v).abs() < (a - b).abs() {
a = (num + 1) / v;
b = v;
}
}
if (num + 2) % v == 0 {
if ((num + 2) / v - v).abs() < (a - b).abs() {
a = (num + 2) / v;
b = v;
}
}
}
vec![a as i32, b as i32]
}
}
边栏推荐
- [point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
- Severity code description the project file line prohibits the display of status error c2440 "initialization": unable to convert from "const char [31]" to "char *"
- Instant messaging IM is the countercurrent of the progress of the times? See what jnpf says
- IDEA 中使用 Hudi
- Spark 集群安装与部署
- CSDN markdown editor help document
- Derivation of Fourier transform
- Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
- 【点云处理之论文狂读前沿版13】—— GAPNet: Graph Attention based Point Neural Network for Exploiting Local Feature
- Explanation of the answers to the three questions
猜你喜欢

LeetCode 75. Color classification

LeetCode每日一题(931. Minimum Falling Path Sum)

Flink学习笔记(十一)Table API 和 SQL

Utilisation de hudi dans idea

Solve POM in idea Comment top line problem in XML file

【点云处理之论文狂读前沿版8】—— Pointview-GCN: 3D Shape Classification With Multi-View Point Clouds
![[point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks](/img/ea/2c4336ee929c26c16627e5c0955704.png)
[point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks
![[point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points](/img/c1/045ca010b212376dc3e5532d25c654.png)
[point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points

Digital management medium + low code, jnpf opens a new engine for enterprise digital transformation

Introduction to the basic application and skills of QT
随机推荐
【Kotlin学习】类、对象和接口——定义类继承结构
WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
C language programming specification
[point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs
Construction of simple database learning environment
Hudi integrated spark data analysis example (including code flow and test results)
[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling
Spark cluster installation and deployment
Just graduate student reading thesis
【点云处理之论文狂读经典版12】—— FoldingNet: Point Cloud Auto-encoder via Deep Grid Deformation
Derivation of Fourier transform
【点云处理之论文狂读经典版7】—— Dynamic Edge-Conditioned Filters in Convolutional Neural Networks on Graphs
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
Go language - JSON processing
LeetCode每日一题(516. Longest Palindromic Subsequence)
CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers
Modify idea code
Filter comments to filter out uncommented and default values
We have a common name, XX Gong
[graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years