丢失的数字

题目描述:给定一个包含 [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-丢失的数字的更多相关文章

  1. GridView导出成Excel字符"0"丢失/数字丢失的处理方式 收藏

    GridView导出成Excel字符"0"丢失/数字丢失的处理方式 收藏 GridView 导出成Excel文件,这个代码在网上比较多.但是发现存在一个问题,导出的数据中如果有&q ...

  2. AOJ.559 丢失的数字

    丢失的数字 Time Limit: 1000 ms Memory Limit: 64 MB Total Submission: 1552 Submission Accepted: 273 Descri ...

  3. [LeetCode] 268. Missing Number ☆(丢失的数字)

    转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...

  4. [LeetCode] Missing Number 丢失的数字

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

  5. [LeetCode] 268. Missing Number 缺失的数字

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

  6. LeetCode 268. Missing Number (缺失的数字)

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

  7. Java实现 LeetCode 268 缺失数字

    268. 缺失数字 给定一个包含 0, 1, 2, -, n 中 n 个数的序列,找出 0 - n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [ ...

  8. Leetcode 268.缺失数字 By Python

    给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [9,6,4,2 ...

  9. [LeetCode] 35. Search Insert Position ☆(丢失的数字)

    转载:https://leetcode.windliang.cc/leetCode-35-Search-Insert-Position.html    思路 Given a sorted array ...

  10. 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 ...

随机推荐

  1. Pycharm 使用备忘

    1.打开方法定义 快捷方式:[ctrl+左键]或者[Ctrl+B] 如果点击之后,打开不是[.py]文件,而是[.pyi]文件,可以把下面红框的参数删掉. 2.设置文件开头默认注释 # *_* cod ...

  2. Vue.js项目的兼容性与部署配置

    一.处理兼容性问题的相关插件: 1> 解决移动端某些版本的浏览器,点击事件有3s延时触发的问题 · 安装 fastclick 依赖包:npm install fastclick --save-d ...

  3. Codeforces Round #740 Div. 2

    题目跳转链接 A. Simply Strange Sort 题意 定义一个函数\(f_{i}\) : 如果\(a_i \ge a_{i+1}\) swap(\(a_i\) \(a_{i+1}\)) 定 ...

  4. Nginx网络压缩 CSS压缩 图片压缩 JSON压缩

    一.序言 使用Nginx作为web应用服务时,会代理如下常见文件:js.css.JSON.图片等,本文提供基于Nginx内置的压缩技术,提供网络请求响应速度的解决方案. 1.网络压缩原理 网络压缩的原 ...

  5. Shell编程中的用户输入处理

    Linux read命令用于从标准输入读取数值. read 内部命令被用来从标准输入读取单行数据.这个命令可以用来读取键盘输入,当使用重定向的时候,可以读取文件中的一行数据. 语法 read [-er ...

  6. CABasicAnimation基础核心动画

    核心动画之作用在层上面.     动画的本质是改图层的某一个属性.     CABasicAnimation *anim = [CABasicAnimation animation];     图层有 ...

  7. Linux开机显示模式切换

    修改vim /etc/inittab 默认为5-图形界面模式,改为3-多用户模式即可 # Default runlevel. The runlevels used are: # 0 - halt (D ...

  8. Idea快捷键---根据自己使用情况持续更新

    查看接口的实现类 -->ctrl+alt+b 查看继承关系 -->ctrl+h 快速查看上次查看代码的位置: -->ctrl+alt+方向键(注意与intel显卡快捷键的冲突,如有冲 ...

  9. HashCode补充

    public class Demo4 { public static void main(String[] args) { String str1 = "hello"; Strin ...

  10. Docker consul的容器服务更新与发现

    Docker consul的容器服务更新与发现 目录 Docker consul的容器服务更新与发现 一.Consul简介 1. 服务注册与发现 2. consul概述 3. consul的两种模式 ...