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 linear runtime complexity. Could you implement it using only constant extra space complexity?
题目标签:Array
题目给了我们一个nums array, array 里是从0 到n 的所有数字, 但是会缺失一个,让我们找出来,数字的排列不是有序的。
既然我们知道n,而且数字是从0 到 n 的,可以利用等差数列求和公式 (首项+尾项) * 个数/2 求出总和, 再遍历一次nums 算出实际的sum, 然后差值就是缺失的那个数字了。
Java Solution:
Runtime beats 49.88%
完成日期:04/27/2017
关键词:Array
关键点:等差数列求和公式
class Solution
{
public int missingNumber(int[] nums)
{
int cSum = (0 + nums.length) * (nums.length + 1) / 2;
int sum = 0; for(int i=0; i<nums.length; i++)
{
sum += nums[i];
} return cSum - sum;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 268. Missing Number (缺失的数字)的更多相关文章
- [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 ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- 268 Missing Number 缺失的数字
给出一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数.案例 1输入: [3,0,1]输出: 2案例 2输入: [9,6,4,2,3,5,7, ...
- 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 ...
- Java [Leetcode 268]Missing Number
题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告
1.题目大意 Given an array nums, write a function to move all 0's to the end of it while maintaining the ...
- 33. 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 ...
- Leetcode 268 Missing Number 位运算
题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组, ...
随机推荐
- java 数组内的最大组合数
给定一个任意长度的java数组,求数组内的数能组合出来的最大整数比如说{9,98,123,32} 最大就是 99832123 import java.util.Arrays; import java. ...
- 【SQL】- 基础知识梳理(三) - SQL连接查询
一.引言 有时为了得到一张报表的完整数据,需要从两个或更多的表中获取结果,这时就用到了"连接查询". 二.连接查询 连接查询的定义: 数据库中的表通过键将彼此联系起来,从而获取这些 ...
- java中Set类接口的用法
在Java中使用Set,可以方便地将需要的类型,以集合类型保存在一个变量中.主要应用在显示列表. Set是一个不包含重复元素的collection.更确切地讲,set 不包含满足 e1.equals( ...
- 一张图告诉你移动Web前端所有技术(工程化、预编译、自动化)
你要的移动web前端都在这里! 大前端方向:移动Web前端.Native客户端.Node.js. 大前端框架:React.Vue.js.Koa 跨终端技术:HTML5.CSS 3.JavaScript ...
- TCP/IP(二)物理层详解
前言 在前面说了一下,计算机网络的大概内容,没有去深刻的去了解它,这篇文章给大家分享一下物理层! 我们知道ISO模型是七层,TCP/IP模型是五层,而tcp/ip协议只将七层概括为4层,我们将学习其中 ...
- 增大hadoop client内存
export HADOOP_CLIENT_OPTS="-Xmx512m $HADOOP_CLIENT_OPTS" 问题场景:sqoop import时报OOM
- unset与unlink
unset() -- 释放给定的变量 详见->http://www.kuqin.com/php5_doc/function.unset.html unlink() --删除文件 常用于用户 ...
- 移动WEB 响应式设计 @media总结
第一种: 在引用样式的时候添加 <link rel="stylesheet" media="mediatype and|not|only (media featur ...
- php版本的选择
简单来说non-thread-safe 非 线程安全 与IIS 搭配环境,thread-safe 线程安全 与apache 搭配的 环境这个大家一定要注意,否则用错了版本,apache是无法启动的,另 ...
- Django 1.10中文文档-聚合
Django 数据库抽象API 描述了使用Django 查询来增删查改单个对象的方法. 然而,有时候你要获取的值需要根据一组对象聚合后才能得到. 这个主题指南描述了如何使用Django的查询来生成和返 ...