LeetCode-268-丢失的数字
丢失的数字
题目描述:给定一个包含 [0, n] 中 n 个数的数组 nums ,找出 [0, n] 这个范围内没有出现在数组中的那个数。
进阶:
- 你能否实现线性时间复杂度、仅使用额外常数空间的算法解决此问题?
示例说明请见LeetCode官网。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/missing-number/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法一:数组遍历
首先,获取数字的长度为n,根据根据公式
n*(n+1)/2得到从0到n的数字相加之和为sum,由于nums数组中只缺少一个数,所以遍历数组,将sum减去数组中所有的元素,然后剩下的数字就是要返回的数。
public class LeetCode_268 {
public static int missingNumber(int[] nums) {
int n = nums.length;
int sum = n * (n + 1) / 2;
for (int num : nums) {
sum -= num;
}
return sum;
}
public static void main(String[] args) {
int[] nums = new int[]{9, 6, 4, 2, 3, 5, 7, 0, 1};
System.out.println(missingNumber(nums));
}
}
【每日寄语】 最清晰的脚印,总是印在最泥泞的路上。
LeetCode-268-丢失的数字的更多相关文章
- GridView导出成Excel字符"0"丢失/数字丢失的处理方式 收藏
GridView导出成Excel字符"0"丢失/数字丢失的处理方式 收藏 GridView 导出成Excel文件,这个代码在网上比较多.但是发现存在一个问题,导出的数据中如果有&q ...
- AOJ.559 丢失的数字
丢失的数字 Time Limit: 1000 ms Memory Limit: 64 MB Total Submission: 1552 Submission Accepted: 273 Descri ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- [LeetCode] Missing Number 丢失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] 268. Missing Number 缺失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- LeetCode 268. Missing Number (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- Java实现 LeetCode 268 缺失数字
268. 缺失数字 给定一个包含 0, 1, 2, -, n 中 n 个数的序列,找出 0 - n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [ ...
- Leetcode 268.缺失数字 By Python
给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [9,6,4,2 ...
- [LeetCode] 35. Search Insert Position ☆(丢失的数字)
转载:https://leetcode.windliang.cc/leetCode-35-Search-Insert-Position.html 思路 Given a sorted array ...
- LeetCode 268. Missing Number缺失数字 (C++/Java)
题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...
随机推荐
- 深入理解F1-score
本博客的截图均来自zeya的post:Essential Things You Need to Know About F1-Score | by Zeya | Towards Data Science ...
- 论文解读(GAN)《Generative Adversarial Networks》
Paper Information Title:<Generative Adversarial Networks>Authors:Ian J. Goodfellow, Jean Pouge ...
- .NET Core分析程序集最优美的方法,不用Assembly.LoadFile(),超越ReflectionOnlyLoad
在编写.NET程序的时候,如果需要对一个程序集文件进行分析,我们可以使用Assembly.LoadFile()来加载这个程序集,然后对LoadFile()方法返回的Assembly对象进行进一步的分析 ...
- elasticsearch之mappings parameters
目录 ignore_above 返回elasticsearch目录 ignore_above#top 长度超过ignore_above设置的字符串将不会被索引或存储(个人认为会存储,但不会为该字段建立 ...
- 根据指定手绘图纸照片行政区划自定义绘制对应区域边界生成geoJOSN的解决方案
项目需求 指定某区域(非省市区县乡镇标准行政区划),做功能边界分区,实现Echarts地理坐标数据可视化展示. 提供信息 项目分析 要在Echarts上实现地理坐标可视化,必须使用geoJSON格式文 ...
- Swift 类的构造函数
构造函数的介绍 构造函数类似于OC中的初始化方法:init方法 默认情况下载创建一个类时,必然会调用一个构造函数 即便是没有编写任何构造函数,编译器也会提供一个默认的构造函数. 如果是继承自NSObj ...
- Maven系列--"maven-compiler-plugin"的使用
万分感谢大佬:Poorzerg 原文链接:https://my.oschina.net/poorzerg/blog/206856 maven是个项目管理工具,如果我们不告诉它我们的代码要使用什么样的j ...
- linux内核中的eventfd
转载请注明来源:https://www.cnblogs.com/hookjc/ eventfd 在内核版本,2.6.22以后有效.查看内核版本可以用命令 uname -r . [cpp] view p ...
- springboot实现分布式锁(spring integration,redis)
Springboot实现分布式锁(Spring Integration+Redis) 一.在项目的pom.xml中添加相关依赖 1)Spring Integration依赖 <dependenc ...
- python基础语法_9-0函数概念
http://www.runoob.com/python3/python3-function.html 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代 ...