惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
Hugging Face - Blog
Hugging Face - Blog
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
B
Blog
小众软件
小众软件
T
Tailwind CSS Blog
MyScale Blog
MyScale Blog
I
InfoQ
Engineering at Meta
Engineering at Meta
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
H
Help Net Security
雷峰网
雷峰网
S
SegmentFault 最新的问题
V
Visual Studio Blog
爱范儿
爱范儿

博客园 - zhaofeng555

欧氏距离 vs 余弦相似度 相似度2-欧式距离 相似度1-余弦相似度 Oh-My-OpenCode介绍 OpenCode 里的 Atlas / Sisyphus / Prometheus区别 安装opencode langchain第二个小例子 langchain的第一个小例子 springai第二个例子使用配置类配置chatclient springai访问本地alloma第一个例子 springai访问本地alloma的qwen3报错 C/c++趣味程序百例 mac 安装stale disffusion笔记 mac 安装TA-Lib包 centos7 install docker CentOS7 内核升级从3.10升级到4.4(以kernel-lt 为例) 找数组中重复的数字 mysql server 端命令 mac安装python3 pandas tushare
一个数组中有一个重复的数字,O(n) 找出来
zhaofeng555 · 2019-12-05 · via 博客园 - zhaofeng555

一个数组中有一个重复的数字,时间复杂度O(n),空间复杂度O(1) 找出来

#include <iostream>
#include<stdlib.h>
using namespace std;
int main()
{
        int n = 1001;
        int *a = new int[n];
        for (int i = 0; i < n; i++)
        {
                a[i] = i+1;
        }
        a[n-1] = rand()%n;
        for (int i = 0; i < n; i++)
        {
                cout<<a[i]<<" ";
        }
        cout << endl;
        int x = 0;
        for (int i = 1; i <= n-1; i++)
        {
                x = (x ^ i);
        }
        for (int i = 0; i < n; i++)
        {
                x = x ^ a[i];
        }
        cout <<"重复的元素为:"<< x << endl;
        return 0;
}