Leetcode--Java--268. Missing Number

Claude Code Skills 入门:内置技能与自定义技能开发 │ Skill 的特点 │1. 封装性:把复杂操作封装成简单命令2. 可复用:一次定义,随处使用3. 可定制:支持参数传入4. 可发现:通过 /skill-name 方式调用打个比方,如果说 Claude Code 的基础工具是「原材料」,那 Skills 就是「预制菜」——只需要简单加热(调用),就能得到成品(结果)。你可以创建自己的 Skill 来封装常用操作。Skill 支持参数,通过。 阅读详情

题目描述

Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?

样例描述

Example 1:

Input: nums = [3,0,1]
Output: 2
Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
Example 2:

Input: nums = [0,1]
Output: 2
Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
Example 3:

Input: nums = [9,6,4,2,3,5,7,0,1]
Output: 8
Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
Example 4:

Input: nums = [0]
Output: 1
Explanation: n = 1 since there is 1 number, so all numbers are in the range [0,1]. 1 is the missing number in the range since it does not appear in nums.
 

Constraints:

n == nums.length
1 <= n <= 104
0 <= nums[i] <= n
All the numbers of nums are unique.

思路

  1. 思路一:数学技巧 ,利用n个连续的数总和是 n * ( n + 1) / 2,减去出现的数剩下的就是没有出现的
  2. 思路二:位运算,将0到n这n+1个数求异或运算,然后在逐个和出现的数异或,由于异或相同为0,而0与任何数异或等于任何数,则最后结果就是缺失的数

代码

思路一:

class Solution {
    public int missingNumber(int[] nums) {
      int n = nums.length;
      //注意其实是n+1个项,首项是0,末项是n+1
      int res = n * (n + 1) / 2;
      //减去所有出现过的,剩余就是消失的数
      for (int x: nums) res -= x;
      return res;
    }
}

思路二:

class Solution {
    public int missingNumber(int[] nums) {
      //由于循环里面 n对应的下标超过了数组的范围,先将n放到结果里面
     int missNum = nums.length;
     for (int i = 0; i < nums.length; i ++){
         missNum ^= i ^ nums[i];
     }
     return missNum;
    }
}
从零开始:C#上位机集成YOLO完成摄像头目标追踪(保姆级教程) 新手入门C#上位机+YOLO做摄像头目标追踪,最忌讳的是“只抄代码,不懂逻辑”。这个教程不仅给了完整代码,还把核心原理和踩坑点都讲清楚了,新手跟着走,不仅能跑通demo,还能理解为什么这么写。如果你们在运行时遇到具体问题,比如模型导出失败、代码报错,都可以在评论区留言,我会一一解答。后续也可以出一期工业相机+YOLO目标追踪的教程,帮助大家把demo落地到实际生产中。 阅读详情

相关推荐

IBIS模型中的Pullup和Pulldown曲线详解:如何避免信号完整性设计中的常见错误

本文深入解析IBIS模型中Pullup和Pulldown曲线的关键作用,揭示其在信号完整性设计中的黄金法则。通过详细分析曲线特性、常见误区及验证方法,帮助工程师避免设计错误,优化高速PCB设计。文章还展示了曲线数据在动态终端匹配、串扰预测和电源完整性分析中的高级应用,提升设计效率和可靠性。

weixin_30275415的博客 460

268. Missing Number

Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. Example 1: Input: nums = [3,0,1] Output: 2 Explanation: n = 3 since there are 3 numbers, so all numbers are in the r

weixin_53371096的博客 159

leetcode解题之268# Missing NumberJava版 (找出0~N中缺少的数字)

leetcode解题之268# Missing NumberJava版 (找出0~N中缺少的数字)

mine_song的博客 1637

STM32CUBEMX配置SDRAM

STM32CUBEMX配置SDRAM

Master_0_的博客 2996

从0开始部署基础的AlwaysOn

在这个快餐文化盛行的时代,应该没多少人真的会沉下心来看理论和具体步骤,加上我自己也需要一篇可以扩展的快速搭建环境的指引,所以特别写了这篇文章。 我们在这里要做的事情有以下几个:安装环境。包括虚拟机和网络配置等。安装Windows域、配置必要的账号密码及功能。搭建域环境并进行配置。安装配置Windows Server Failover Cluster(WSFC)。安装SQL Server。配置Alw

MVP黄钊吉(發糞塗牆) 7590

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

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. 利用等差数列求和公式即可,空间复杂度为1,算法复杂

u011918382的博客 1042

SQL注入绕过information_schema:实战技法与替代路径详解

SQL注入是Web安全领域的核心漏洞类型,其原理是通过构造恶意SQL语句,干扰应用程序与数据库的正常交互逻辑,从而窃取或篡改数据。在渗透测试与CTF竞赛中,绕过常见防护措施是提升技术能力的关键。当标准的信息收集方式受阻时,例如无法访问information_schema数据库,攻击者需要掌握替代的元数据获取方法。这些技术价值在于模拟真实环境中日益严密的防御体系,锻炼在受限条件下的系统性信息收集思维。典型的应用场景包括存在WAF过滤、权限限制或编码干扰的实战环境。本文将围绕SQL注入这一核心,深入探讨如何利用

weixin_33980459的博客 436

(java)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

martinmateng的博客 405

从理论到代码:蒙特卡洛法在光学散射模拟中的应用全解析(Matlab 2014a版)

本文详细解析了蒙特卡洛法在光学散射模拟中的应用,从数学原理到Matlab 2014a实现。通过模拟光子随机游走过程,精确描述光在浑浊介质中的传输特性,提供完整的源码实现和性能优化技巧,适用于生物医学光学、大气遥感等领域。

rum55的博客 635

115网页版VR模式终于能用了!电脑直接播放vr视频沉浸感满满 115vr播放 无需vr头盔播放vr视频 在线vr播放器 115网盘360度视频播放方法 115全景视频播放教程

油猴 + 脚本让你网页端直接开启 VR 模式支持 360 视频拖动视角,非常沉浸适合电脑设备直接用无需vr头盔也能轻松畅看vr视频,希望也能帮到你。如果你也被 115 的限制搞烦了,不妨试试这招。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 5791

【LeetCode】268 Missing Number (java实现)

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

添经地翼的博客 1797

Python爬虫selenium验证-中文识别点选+图片验证码案例

环境对模型进行训练,参考:https://github.com/sml2h3/dddd_trainer。能识别,但是发现默认识别率有点低,想要提升识别率,可以搭建。截图每个字符,并基于ddddocr识别。根据坐标,在验证码上进行点击。

dme.的博客 2172

LeetCode-268. Missing Number [C++][Java]

Given an arraynumscontainingndistinct numbers in the range[0, n], returnthe only number in the range that is missing from the array.

贫道绝缘子的博客 726

OV9734 DATASHEET,完整规格书

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

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

LeetCode 268. Missing Number(java)

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 Example 2: Input: [9,6,4,2,3,5,7,0,1] Output: 8 ...

katrina95的博客 254

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

LeetCode 268. Missing Number【Java】

题目描述 268. 缺失数字 AC代码 先算出来1~nnn的和是多少,然后减去nums数组中出现的数,遍历完数组剩下的就是答案。 class Solution { public int missingNumber(int[] nums) { int n=nums.length; int sum=n*(n+1)/2; for(int x:nums) sum-=x; return sum; } } ...

NayelyA的博客 207

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【LeetCode】268. Missing Number 丢失的数字(Easy)(JAVA)

【LeetCode】268. Missing Number 丢失的数字(Easy)(JAVA) 题目地址: https://leetcode.com/problems/missing-number/ 题目描述: Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. Follow up:

qq_16927853的博客 259

Java [Leetcode 268]Missing Number

题目描述: Given an array containingndistinct numbers taken from0, 1, 2, ..., n, find the one that is missing from the array. For example,Givennums=[0, 1, 3]return2. Note:Your algorithm sho...

dkk37351的博客 127

Missing Number Leetcode 268 Java

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 line

Json随笔 461

leetcode:268. Missing Number(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址 题目地址 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

z_zhaojun的博客 2949

LeetCode 268. Missing Number (Java)

题目: 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 Example 2: Input: [9,6,4,2,3,5,7,0,1] Out...

qq_41878358的博客 254

LeetCode Top Interview Questions 268. Missing Number (Java版; Easy)

welcome to my blog LeetCode Top Interview Questions 268. Missing Number (Java版; Easy) 题目描述 Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from th...

littlehaes的博客 177

LeetCode 268 -Missing Number ( JAVA )

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 sho

UckyK的专栏 611

LeetCode-268. Missing Number (Java)

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 sho

NOadu的专栏 427

Leetcode 268. Missing Number (Java)

Given an arraynumscontainingndistinct numbers in the range[0, n], returnthe only number in the range that is missing from the array. Example 1: Input: nums = [3,0,1] Output: 2 Explanation: n = 3 since there are 3 numbers, so all numbers are in t...

weixin_58080442的博客 571

leetcode-268-缺失数字(missing number)-java

题目及测试 package pid268; /* 缺失数字 给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [9,6,4,2,3,5,7,0,1] 输出: 8 说明: 你的算法应具有线性时间复杂度。你能否仅使用额外常数空间来实现? */ pu...

今天吃了吗的博客 366
上一篇: Leetcode--Java--228. Summary Ranges
下一篇: Flask-WTF 简化处理表单数据流程
写点code吧
博客等级 码龄9年 14粉丝 463原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值