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

推荐订阅源

博客园 - 聂微东
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
A
About on SuperTechFans
M
MIT News - Artificial intelligence
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
Jina AI
Jina AI
P
Proofpoint News Feed
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
N
Netflix TechBlog - Medium
人人都是产品经理
人人都是产品经理

博客园 - 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;
}