当前位置:网站首页>leetcode268:丢失的数字
leetcode268:丢失的数字
2022-04-22 06:08:00 【浅浅望】
问题:丢失的数字
给定一个包含 [0, n] 中 n 个数的数组 nums ,找出 [0, n] 这个范围内没有出现在数组中的那个数。
示例 1:
输入:nums = [3,0,1]
输出:2
解释:n = 3,因为有 3 个数字,所以所有的数字都在范围 [0,3] 内。2是丢失的数字,因为它没有出现在 nums 中。
示例 2:
输入:nums = [0,1]
输出:2
解释:n = 2,因为有 2 个数字,所以所有的数字都在范围 [0,2] 内。2是丢失的数字,因为它没有出现在 nums 中。
示例 3:
输入:nums = [9,6,4,2,3,5,7,0,1]
输出:8
解释:n = 9,因为有 9个数字,所以所有的数字都在范围 [0,9] 内。8 是丢失的数字,因为它没有出现在 nums 中。
示例 4:
输入:nums = [0]
输出:1
解释:n = 1,因为有 1 个数字,所以所有的数字都在范围 [0,1] 内。1是丢失的数字,因为它没有出现在 nums 中。
思路一:排序
- 对数组元素进行排序
- 找出前后差值不为1的元素
- 该元素后一个即为丢失的数字
**特殊情况:**所有元素均连续,且从0开始时,丢失的元素应该为n。
代码:
class Solution {
public int missingNumber(int[] nums) {
Arrays.sort(nums);
int result = 0;
for(int i=1; i<nums.length; i++){
if(nums[i]-nums[i-1] != 1)
result = nums[i-1] + 1;
}
if((result == 0) && (nums[0]==0))
result = nums[nums.length-1]+1;
return result;
}
}
思路二:数学法
- 已知 1 + 2 + 3 + ⋯ + n = n ( n + 1 ) 2 1+2+3+\cdots+n=\frac{n(n+1)}{2} 1+2+3+⋯+n=2n(n+1),故 [ 0 , n ] [0,n] [0,n]范围内所有整数的和为 t o t a l = n ( n + 1 ) 2 total=\frac{n(n+1)}{2} total=2n(n+1)
- 对nums内的所有元素求和sum
- 故丢失的数字即为: t o t a l − s u m total-sum total−sum
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int total = n*(n+1)/2;
int sum = 0;
for(int i=0; i<n; i++){
sum += nums[i];
}
return total-sum;
}
}
版权声明
本文为[浅浅望]所创,转载请带上原文链接,感谢
https://blog.csdn.net/xpl_1620/article/details/121176484
边栏推荐
- 转行数字IC验证的学习路线是什么样的?现在开始转是否来得及?985本硕,电子学院研二
- PL2586设计电路|PL2586替代FE1.1S电路图|USB2.0HUB工业级集线器方案设计
- Sss1700 qfn36 single chip designs a USB typec headset | supports the automatic switching of USB headset scheme between wire control and European and American regulations
- It's nothing to be able to dismantle the host. Mr. expressway can also test it
- 如何成为IC验证工程师?
- Minipcie interface can card expands can channel for industrial computer
- STM32学习记录0002-STM32初探
- Fire fighting equipment optical fiber networking can to optical fiber converter
- 量化5个城市的PM2.5随时间的变化情况
- Quantify the relationship between 911 calls and years from 2015 to 2017
猜你喜欢

Lcfnet series can to optical fiber Ethernet equipment realizes ultra long-distance optical fiber communication between can networks

Is there any difference in the worst impedance processing you have encountered?

USBCAN卡在动力电池组EOL测试系统中CAN总线的应用

STM32 learning record 0002-stm32

HDMI2.0二进一出切换器方案ASW3642和TS3DV642设计电路对比

CS5268开发TypeC转HDMI+VGA+PD+U3+3.5音频五合一扩展音视频转换方案参考设计

Dcoker安装

阿里云部署RSSHub踩坑笔记

SSS1700

集成电路模拟版图入门-版图基础学习笔记(五)
随机推荐
Remote program upgrade scheme of transparent cloud gateway in construction machinery under epidemic environment
Decomposition of matrix -- LU decomposition
数字IC设计自学入门难吗?如何快速入门呢?
Application of mcicecan in industrial computer
Alternative ag9311 design circuit | cs5266 application circuit diagram | design and development of three in one scheme of typec docking station
Quantify the relationship between 911 calls and years from 2015 to 2017
Application of can optical fiber optical transceiver in photoelectric slip ring
超定方程的求解
CAN光端机在消防水炮中的应用
环网冗余CAN光端机在保得威尔消防火灾报警系统中的应用
集成电路模拟版图入门-版图基础学习笔记(三)
阿里云部署Tiny Tiny RSS踩坑笔记
CAN总线记录诊断助手
Can transparent recording cloud gateway enables construction machinery
fastmock使用说明
GridView of QML realizes sliding grid layout
Is there any mistake? The optical module shell affects the signal quality
替代 FE1.1s HUB读卡主控芯片-MA8601
I've gone too far, but I can't get out of the routine of ddrx debugging
HDMI2. Design circuit comparison between asw3642 and ts3dv642