7-4 查找最接近的元素 分数 20 作者 zhu weihua 单位 新余学院 在一个非降序列中,
时间: 2023-06-23 08:08:16 浏览: 87
给定一个整数x,请设计一个高效算法,查找序列中最接近x的数。 如果有多个最接近x的数,则返回序号最小的数的下标(从0开始)。 如果序列中没有元素,则返回-1。
函数头部: int find(int* a, int n, int x);
输入参数:
a:非降序列,数组a中存放n个整数(0<=n<=10^6,-10^9<=a[i]<=10^9)。
n:非降序列中元素的个数。
x:需要查找的整数(-10^9<=x<=10^9)。
输出参数:
函数返回值:序列中最接近x的数的下标。
输入样例:
a: 1 2 3 4 5 6
n: 6
x: 3
输出样例:
2
算法1
(二分查找) $O(logn)$
由于是非降序列,可以使用二分查找法。
首先特判序列为空的情况,如果序列为空,则返回-1。
如果序列不为空,则在序列中进行二分查找,找到最接近x的数。具体的方法是设两个指针left和right,分别指向序列的第一个元素和最后一个元素。然后不断将区间缩小,直到left和right相邻为止。最后判断left和right哪一个更接近x即可。
相关问题
给我推荐20个比较流行的图神经网络模型源码
在这里推荐几个比较流行的图神经网络模型的源码:
1. Graph Convolutional Network (GCN):https://github.com/tkipf/pygcn
2. Graph Attention Network (GAT):https://github.com/PetarV-/GAT
3. DeepWalk:https://github.com/phanein/deepwalk
4. Node2Vec:https://github.com/aditya-grover/node2vec
5. PageRank:https://github.com/dennybritz/pagerank-py
6. Spectral Clustering:https://github.com/lmcinnes/spectral_clustering
7. Structural Role Similarity:https://github.com/dorian3d/StructuralRoleSimilarity
8. GraphSAGE:https://github.com/williamleif/GraphSAGE
9. Random Walk with Restart (RWR):https://github.com/vahidk/RWR
10. Graph Isomorphism Network (GIN):https://github.com/weihua916/powerful-gnns
11. Graph Autoencoder:https://github.com/suanrong/GraphAutoEncoder
12. Large-scale Graph Convolutional Networks (GraphSAINT):https://github.com/PetarV-/GraphSAINT
13. Graph Neural Network (GNN) Explainer:https://github.com/jkuczm/gnn-explainer
14. Graph Convolutional Policy Network (GCPN):https://github.com/shenweichen/GCPN
15. Relational Graph Convolutional Network (RGCN):https://github.com/DeepGraphLearning/RecommenderSystems
16. Graph Neural Network (GNN) Benchmark:https://github.com/jwchen307/GNN-Benchmark
17. Graph Neural Tangent Kernel (GNTK):https://github.com/google/graph_neural_tangent_kernel
18. GraphWave:https://github.com/leoribeiro/graphwave
19. Graph Mixer:https://github.com/google-research/graph_mixer
20. Message Passing Neural Network (MPNN):https://github.com/deepmind/mpnn
希望这些源码能帮到你!
js截取"D:\\Users\\80320974\\Desktop\\fdkjgkgdf;;.txt"最后一个点和最后一个\\之间的字符替换其他字符不改变前后字符信息
可以使用正则表达式和字符串的replace方法来截取最后一个点和最后一个\\之间的字符,然后将其替换为其他字符。下面是一个示例代码:
```javascript
let path = "D:\\Users\\80320974\\Desktop\\fdkjgkgdf;;.txt";
let replacedPath = path.replace(/(.*)\\(.*\..*)/, "$1\\replacement$2");
console.log(replacedPath); // 输出:D:\Users\80320974\Desktop\replacement.txt
```
在上面的代码中,使用了正则表达式`/(.*)\\(.*\..*)/`来匹配整个路径,并使用括号分组来捕获最后一个点之前和之后的部分。然后在replace方法中,使用了`$1`和`$2`来引用这两个捕获的部分,并将它们替换为"replacement"。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)