LeetCode题解之Missing Number
1、题目描述

2、题目分析
将 [ 0 , n ]之间的整数放到 n 个元素的数组中去,必然缺失一个元素。在一次遍历中,将元素n[i] 放到 n[ n[i] ] ,位置。最后检查元素值和下标不相等的情况。
3、代码
int missingNumber(vector<int>& nums) {
if( nums.size() == ) return ;
for( int i= ; i < nums.size() ; i++ )
{
if( nums[i] != i && nums[i] < nums.size() ){
swap( nums[i] , nums[ nums[i] ] );
i--;
}
}
for( int i = ; i < nums.size() ; i++ )
{
if( nums[i] != i)
return i;
}
return nums.size() ;
}
LeetCode题解之Missing Number的更多相关文章
- [LeetCode 题解]:Palindrome Number
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Determine ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- LeetCode算法题-Missing Number(Java实现-四种解法)
这是悦乐书的第200次更新,第209篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第65题(顺位题号是268).给定一个包含n个不同数字的数组,取自0,1,2,...,n ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
- 【LeetCode】268. Missing Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...
- LeetCode题解-----First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- leetcode题解 9. Palindrome Number
9. Palindrome Number 题目: Determine whether an integer is a palindrome. Do this without extra space. ...
- Leetcode 题解 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode题解之Happy Number
1.题目描述 2.题目分析 根据 happy number 的 性质,如果循环7次还没有到达 1,则这个数不是happy number . 3.代码 bool isHappy(int n) { ) r ...
随机推荐
- 2018春招-今日头条笔试题-第二题(python)
题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) 解题思路: 利用深度优先搜索 #-*- coding:utf-8 -*- class DFS: ''' num:用于存储最后执行次 ...
- 第6章—渲染web视图—使用Thymeleaf
使用Thymeleaf 长期以来,jsp在视图领域有非常重要的地位,随着时间的变迁,出现了一位新的挑战者:Thymeleaf,Thymeleaf是原生的,不依赖于标签库.它能够在接受原始HTML的地方 ...
- HUE配置文件hue.ini 的hive和beeswax模块详解(图文详解)(分HA集群和非HA集群)
不多说,直接上干货! 我的集群机器情况是 bigdatamaster(192.168.80.10).bigdataslave1(192.168.80.11)和bigdataslave2(192.168 ...
- js二维数组
1.判断是否为二维数组 function isMultiArr(arr){ return arr.every(function(element){ return element instanceof ...
- MySQL Optimization 优化原理
MySQL Optimization 优化原理 MySQL逻辑架构 如果能在头脑中构建一幅MySQL各组件之间如何协同工作的架构图,有助于深入理解MySQL服务器.下图展示了MySQL的逻辑架构图. ...
- springboot-mongodb的多数据源配置
pom.xml中引入mongodb的依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...
- C#的Lambda表达式嵌套例子
/* *curStatsResult是List<string>类型, *x.GetAllOsVersion()结果是string[]类型, *这里是先使用SelectMany()返回一个结 ...
- Ceph 存储集群 - 存储池
目录 一.存储池介绍 二.存储池命令 1. 列出存储池 2. 创建存储池 3. 设置存储池配额 4. 删除存储池 5. 重命名存储池 6. 查看存储池统计信息 7. 生成存储池快照 8. 删除存储池快 ...
- elasticsearch的join查询
1.概述 官方文档 https://www.elastic.co/guide/en/elasticsearch/reference/current/joining-queries.html 两种类型的 ...
- [PY3]——threading.Event
Class Event { __init__(self) clear(self) is_set(self) set(self) wait(self,timeout=None) } is_set(sel ...