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 ...
随机推荐
- ASP.NET CORE系列【四】基于Claim登录授权
介绍 关于什么是Claim? 可以看看其他大神的文章: http://www.cnblogs.com/jesse2013/p/aspnet-identity-claims-based-authenti ...
- Codeforces Round #523 (Div. 2)C(DP,数学)
#include<bits/stdc++.h>using namespace std;long long a[100007];long long dp[1000007];const int ...
- bzoj2724: [Violet 6]蒲公英(分块)
传送门 md调了一个晚上最后发现竟然是空间开小了……明明算出来够的…… 讲真其实我以前不太瞧得起分块,觉得这种基于暴力的数据结构一点美感都没有.然而今天做了这道分块的题才发现分块的暴力之美(如果我空间 ...
- NodeJS什么都能做,为什么还要JAVA?
这张图看起来简单而且很好理解,但没尝试过,会有很多疑问. SPA模式中,后端已供了所需的数据接口,view前端已经可以控制,为什么要多加NodeJS这一层? 多加一层,性能怎么样? 多加一层,前端的工 ...
- Python Day22
Django之Form组件 Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 1.创建Form类 ...
- 洛谷 P1536 村村通(并查集)
嗯... 题目链接:https://www.luogu.org/problemnew/show/P1536 思路: 这道题可以看出是并查集的思想,然后用一个while嵌套一下,输入一条路的两个端点,就 ...
- MySQL不同数据库之间表的简单同步
MySQL不同数据库之间表的简单同步,实用轻量级数据如下案列展示:例如我现在主库上面有users .tenants两张表需要同步到备库上面主库1.确认主库数据条数 select count(*) fr ...
- not null 非空约束
例子:create table tb1( id int, name varchar(20) not null); 注意 空字符不等于null #手动,添加非空约束 (必须这个字段,没 ...
- unique key 唯一约束
#添加唯一约束mysql> alter table tb2 -> add unique key(name) ->;#删除唯一约束mysql> alter table ...
- 自动检测GD库支持的图像类型
以下代码通过自动检测GD库支持的图像类型 来写出移直性更好的PHP代码 <?php if(function_exists("imagegif")){ header(" ...