LeetCode Missing Number (简单题)
题意:
给一个含有n个整数的数组,数组中的元素应该是0~n。现在缺了其中某1个,找出缺少的那个整数?
思路:
0~n的总和是可以直接计算的,而缺少的那个就是sum减去数组的和。
int missingNumber(vector<int>& nums)
{
int sum=;
for(int i=; i<nums.size(); i++)
sum+=i+-nums[i];
return sum;
}
AC代码
LeetCode Missing Number (简单题)的更多相关文章
- HDU 5166 Missing number 简单数论
Missing number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) [ ...
- PAT 1144 The Missing Number[简单]
1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...
- [LeetCode] Missing Number 丢失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] Missing Number (A New Questions Added Today)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- (leetcode)Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- LeetCode——Missing Number
Description: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one t ...
- 力扣485. 最大连续1的个数-C语言实现-简单题
题目 [题目传送门] 给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3 ...
- PAT-1144(The Missing Number)set的使用,简单题
The Missing Number PAT-1144 #include<iostream> #include<cstring> #include<string> ...
- leetcode简单题6
今天的华师 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
随机推荐
- 初识Composer
关于vendor name和project name的区别? 包名(package name)包含了供应商名(vendor name)和项目名(project name)是为了避免命名冲突的 requ ...
- 2018ICPC徐州区域赛网络赛G(VECTOR+SET,模拟)
#include<bits/stdc++.h>using namespace std;int x,y;vector<int>v1,v2;long long solve(vect ...
- InfoQ —— 百度搜索速度优化
本篇源自InfoQ —— <百度搜索速度优化> 好多名词不了解... 相关文档下载链接 背景 许霞,毕业于浙江大学.08年加入百度运维部. 本篇讲座主要讲述百度关于速度的优化,包括PC端和 ...
- Java Web之文件的上传及下载
一.文件的上传 1. 简介 > 将一个客户端的本地的文件发送到服务器中保存. > 上传文件是通过流的形式将文件发送给服务器. 2.表单的设置 1.向服务器上传一个文件时,表单要使用post ...
- js默认事件汇总
默认事件 就是浏览器通过HTML标签或DOM元素提供的一些功能性的默认行为.比如在a标签href属性上的跳转,右键呼出的菜单等等.我们可以通过js取消或更改这些默认事件. 取消默认事件 默认事件都是定 ...
- 高斯消元法的C++简单实现
高斯消元法 首先,我们导入几个概念. 定义1: 一个矩阵称为阶梯形(行阶梯形),若它有以下三个性质: 1.每一非零行在每一零行之上: 2.某一行的先导元素所在的列位于前一行先导元素的后面: 3.某一行 ...
- Sqlyog问题
Sqlyog没有架构设计器的解决方法 更换注册码即可
- thinkphp5.1 使用第三方扩展类库
此案例介绍的不是通过composer加载的,是手工下载放入extend目录下的扩展类库,仍然以phpspider为例 将owner888目录放入extend目录下,也可以直接将phpspider目录放 ...
- ELK系列(4) - Elasticsearch cannot write xcontent for unknown value of type class java.math.BigDecimal
问题与分析 在使用Elasticsearch进行index数据时,发现报错如下: java.lang.IllegalArgumentException: cannot write xcontent f ...
- Apache服务器配置虚拟域名
我在别处发的帖子 http://www.52pojie.cn/thread-599829-1-1.html