LeetCode 268. Missing Number

深入了解 CPU 的型号、代际架构与微架构! 阅读详情

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

For example,
Given nums = [0, 1, 3] return 2.

Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

分析:本题说出的数组包含从 0 到 n 这 n+1 个数中的 n 个,这 n 个数不重复,乱序的在数组中存放,求出数组中不包含的那个数字。

解法一:

数学方法:我们知道 0 ~ n 这 n+1 个数之和,减去数组各元素即得到最后的Missing Number

代码:

public class Solution {
    public int missingNumber(int[] nums) {
        int n = nums.length;
        int result = n*(n + 1)/2;
        for(int i = 0; i < n; i++){
            result -= nums[i];
        }
        return result;
        
    }
}
解法二:

位运算:0 ~ n 这 n+1 个数异或所得的数 与 数组各元素异或所得的数 不同就在于 Missing Number,那么可以通过两个和的异或(XOR)获得

代码:

public class Solution {
    public int missingNumber(int[] nums) {
        
        int totalXOR = 1;
        int numsXOR = nums[0];
        for(int i = 1; i < nums.length; i++){
            numsXOR = nums[i] ^ numsXOR;
        }
        for(int i = 2; i <= nums.length; i++){
            totalXOR = i ^ totalXOR;
        }
        return numsXOR ^ totalXOR;
                
    }
}
解法三:

将数组中的元素排序,遍历数组,索引值与当前元素值相等则继续,索引值和当前元素值不同,则返回;如果全部相同,则返回索引的下一个值。

[0,1,2,3,5]-->返回4;[0,1,2,3,4]-->返回5.

代码:

public class Solution {
    public int missingNumber(int[] nums) {
        
        Arrays.sort(nums);
        int index;
        for(index = 0; index < nums.length; index++){
            if(index != nums[index]){
                return index;
            }
        }
        return index;
    }
}






OnePose++: Keypoint-Free One-Shot Object Pose Estimation without CAD Models 算法:首先采用LoFTR的细匹配模块,将粗匹配点周围的小窗口内,通过对比特征找到最相似的位置,细化当前特征点的位置。算法:PnP算法,利用3D点和2D投影点的对应关系,计算相机相对于物体的6D姿态(位置朝向)。算法:使用COLMAP,对2D粗匹配对(不依赖传统关键点匹配)进行特征轨迹计算并生成半稠密点云,每个3D点包含坐标和对应的2D投影点。算法:LoFTR粗匹配模块,将每一个像素点视为伪关键点,生成半稠密的2D匹配对。输入:优化后的3D、当前视图的2D特征点。输出:粗半稠密点云以及物体的特征轨迹。 阅读详情

相关推荐

要配置西门子G120XA变频器实现**端子启停**和**Modbus RTU(485)频率给定

如有问题,可通过STARTER软件在线诊断Modbus通讯状态。要配置西门子G120AX变频器实现。

微:13763355074 4849

LeetCode:268. Missing Number

LeetCoed:268. Missing NumberGiven an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.For example, Given nums = [0, 1, 3] return 2.Note: Your

zems 518

Gradle进阶:4:结合Sonarqube进行代码审查

作为代码质量检查的流行工具,比如Sonarqube能够检查代码的“七宗罪”,跟代码结合起来能够更好地提高代码的质量,这篇文章将会介绍如何结合gradle和sonarqube对代码质量与测试覆盖率进行分析。

知行合一 止于至善 1万+

【力扣算法 LeetCode】268 Missing Number 消失的数字

文章目录前言一、题目二、解答1.代码2.复盘总结 前言 题目出自力扣 算法练习题:Missing Number 消失的数字 一、题目 题目 给定一个包含 [0, n] 中 n 个数的数组 nums ,找出 [0, n] 这个范围内没有出现在数组中的那个数。 题目链接 测试用例 输入:nums = [3,0,1] 输出:2 解释:n = 3,因为有 3 个数字,所以所有的数字都在范围 [0,3] 内。2 是丢失的数字,因为它没有出现在 nums 中。 示例 2: 输入:nums = [0,1]

南迁的博客 166

基于STM32F407的摄像头(不带FIFO的OV7670)图像采集及LCD显示实验-笔记整理

硬件说明 STM32F4开发板 stm32f4系列的芯片处理的能力可以说非常强大了的,而且内部还自带有数字摄像头接口(DCMI),可以说stm32f407的核心板和不带FIFO的摄像头模块OV7670简直就是绝配! 我手上的这块张这个样子 (图片来源:某宝): F4系列的DCMI接口内部结构框图如下图3所示,具体的配置使用方式可以参考以下这篇博文:《stm32f4的数字摄像头接口(DCMI)使用》 这位博主大大已经很详细地介绍了DCMI接口的具体使用方法和原理,更重要的是里面还有接口的配置函数,虽然不完

LiangWF22的博客 2万+

CarSim转向控制对比指南:开环扭矩控制vs转角控制(附Simulink连接示例)

本文深入对比了CarSim中车轮转角控制与开环扭矩控制的核心原理与适用场景。开环扭矩控制能激活完整的转向系统动力学,更适用于EPS控制策略开发与驾驶员在环仿真等对真实性要求高的场景。文章提供了详细的Simulink接口配置步骤与连接示例,为车辆动力学仿真工程师提供了清晰的决策与实操指南。

weixin_29199873的博客 478

力扣Leetcode 268 | 缺失的数字(Missing Number)

官方解法 视频: 力扣Leetcode 268 | 缺失的数字 Missing Number

maoersong的专栏 363

Howland电流源电路:从理论到实践的深度解析与优化策略

本文深度解析了Howland电流源电路的工作原理,从经典四电阻架构的“虚短”“虚断”分析,到其实现宽带恒流输出的理论优势。重点剖析了实践中因电阻精度、运放参数及PCB布局导致的性能瓶颈,并提供了引入第五电阻、优化运放选型等关键优化策略,旨在将输出阻抗从kΩ级提升至MΩ级,以满足精密测量与传感器驱动等应用需求。

weixin_29243063的博客 443

【leetcode】268. Missing Number

problem 268.Missing Number solution1:等差数列求和公式 根据题意,(0, 1, 2,...,n),共有(n+1)个数,丢失了一个数之后,剩余的数组成的数组,求解丢失的数据。 等差数列求和减去数组累加和得到的结果即为所求数据。 class Solution { public: int missingNumber(vector<in...

weixin_30390075的博客 91

【N32L40X】学习笔记12-规则组+注入组+外部触发启动

使用定时器触发规则通道开始采集数据,采集完成后使用dma传输数据,并启动注入通道转换数据,注入通道转换完成后产生中断,中断中读物注入通道数据。

Car12 423

预测股票价格趋势:使用MATLAB进行股票价格预测

通过收集股票数据、数据预处理、构建预测模型以及可视化结果,我们可以利用MATLAB强大的计算和分析功能来预测股票价格的走势。然而,需要注意的是,股票市场具有高度复杂性和不确定性,预测结果仅供参考,并不构成投资建议。然而,借助计算机编程和数据分析工具,我们可以利用历史股价数据来建立模型,并预测未来的股票价格走势。首先,我们需要获取股票的历史价格数据。完成模型的拟合后,我们可以使用该模型来进行未来股票价格的预测。最后,我们可以使用MATLAB的绘图功能将预测的股票价格与实际股票价格进行对比,并进行可视化展示。

2301_79326588的博客 1018

LeetCode刷题:268. Missing Number

LeetCode刷题:268. Missing Number 原题链接:https://leetcode.com/problems/missing-number/ Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 274

华为OD机试双机位C卷:挑选宝石 (C/C++/Python/JAVA/JS/GO)

华为OD机试双机位C卷 挑选宝石,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解。华为OD机试-挑选宝石,华为OD机考-挑选宝石。

qq_45776114的博客 1500

leetcode 268: Missing Number

Missing Number Total Accepted: 10033 Total Submissions: 31720 Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For e

西施豆腐渣 2957

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

2018.03.15 leetcode 打卡 #268. Missing Number

!!!leetcode第一次Accepted!!! 虽然感觉这句话暴露自己的智商,但是没办法,就是比较笨了~~~ 虽然超级简单!几个简单的if语句搞定,还是阻挡不住我看到Accepted时的开心啊~ 有第一次绿色 就有不断的绿灯 哈哈哈哈 #268.Missing Number 题目 :Given an array containingndistinct numbers taken...

Amy_mm的博客 274

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

leetcode 268. Missing Number

目录 一、问题分析 二、代码实现 1、数学运算 2、位运算 https://leetcode.com/problems/missing-number/ 给定一个数组包含n个独一无二的元素,元素大小在[0, n]之间,求出数组中所缺失的那个整数,要求时间O(n),空间O(1) 一、问题分析 测试用例: Example 1: Input: [3,0,1] Output:...

小景的博客 312

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

LeetCode 268: Missing Number

LeetCode 268: Missing Number 题目描述 给定一个含有n个元素的数组,数组中所有的元素在 0-n 之间,但是有一个数是缺失的,要求找到这个缺失的数。 解题思路 可采用位异或的思路,将数组中所有的元素取位异或,然后与所有的下标取位异或,最后再位异或n即可得最终的结果。 AC代码 class Solution { public: int missingN...

HakunaMatata的博客 163

【Leetcode】268. 缺失数字(Missing Number)

Leetcode - 268 Missing Number (Easy) 题目描述:长度为 n 的数组由 0 - n 的数字组成,找到数组中丢失的数字。 Input: [3,0,1] Output: 2 解题思路:前 n 项和。 public int missingNumber(int[] nums) { int sum = nums.length * (nums.length + 1)...

厉兵秣码 228

LeetCode 268 Missing Number

题目描述Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.For example, Given nums = [0, 1, 3] return 2.Note: Your algorithm should run in

Yano_nankai的博客 3702

【Leetcode】268 Missing Number

题目描述 题目含义是,假设有一个长度为n+1的数组(其取值为[0,1,2,...,n]),随机拿掉一个数,使之变为长为n的array,打乱顺序问拿掉的数是几? time:O(nlogn);space:O(1)time:O(nlogn);space:O(1)time:O(nlogn);space:O(1) 想法是如果先排序,那么遍历的时候会很容易判断前后两个数是否差值为1。这个好像能补上上...

Ray 303

leetcode【268 Missing Number】【Python】

给出从0到n的n+1个数字中的n个数,找到缺失的那个数字。第一想法就遍历就可以了,但是注意不要被它给的例子所迷惑,数组不一定是排好序的,所以需要先排序。class Solution(object): def missingNumber(self, nums): """ :type nums: List[int] :rtype: int

u014251967的博客 2818

LeetCode 268.Missing Number

LeetCode 268.Missing NumberDescription: Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array. Example 1: Input: [3,0,1] Output: 2 Ex

My Blog 179

Leetcode 268. Missing Number (Medium) (cpp)

Leetcode 268. Missing Number (Medium) (cpp)

还是需要多学习!闷声大发财,这是坠吼的! 517

LeetCode - 268. Missing Number

链接 268. Missing Number 题意 消失的数字 给定一个数组,包含了n个不同的数(从0到n),注意其中缺少了一个数。找出这个数。 思路 思路一: 首先对该数组排序,直接遍历该数组,缺少哪个数字直接返回 思路二: 来自讨论区,由于a^b^b = a。因此可以遍历数组,假设数组没有缺失的数字,那么xor = xor ^ i ^ nums[i]的值是一直不变的。但因为缺失了一...

163

Leetcode 268. Missing Number

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example, Given nums = [0, 1, 3] return 2. Note: Your algorithm shoul

无名山丘,崛起成峰 1157

[LEETCODE] 268. Missing Number

[LEETCODE]268. Missing NumberGiven an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.

TonyYang1995的博客 416
上一篇: LeetCode 319. Bulb Switcher
下一篇: LeetCode 144. Binary Tree Preorder Traversal
Appletable
博客等级 码龄14年 0粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值