LeetCode 268. Missing Number缺失数字 (C++/Java)
题目:
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n
, find the one that is missing from the array.
Example 1:
- Input: [3,0,1]
- Output: 2
Example 2:
- Input: [9,6,4,2,3,5,7,0,1]
- Output: 8
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
分析:
给定一个包含 0, 1, 2, ..., n
中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。
最先想到将数组进行排序,再依次判定数和索引是否对应,首次不对应的索引便是缺失的数。不过由于需要对数组进行排序,这样时间复杂度就不会是线性的了。
也可以创建一个集合,将数组中元素添加进集合中,再从0到n对集合进行查询,不在集合内的便是缺失的数。不过这样空间复杂度就不是常数级的了。
我们知道所给的数的序列是从0到n,且只缺失一个数,根据高斯求和公式,可以快速求得0到n这n+1个数的和,再减去序列中的n个数,剩下的便是缺失的数。
还可以利用异或运算(XOR),我们知道对一个数进行两次完全相同的异或运算会得到原来的数,即
a ^ b ^ b = a
a ^ a = 0
根据题目我们知道,未缺失的数,在[0-n]和数组中各出现了一次,而缺失的数只在[0-n]中出现了一次,根据这个条件和异或的特性可以通过一次循环求得缺失数。
假设所给的序列为0,1,3,4,我们可以求得
missing = 4 ^ 0 ^ 0 ^ 1 ^ 1 ^ 2 ^ 3 ^ 3 ^ 4
= (4 ^ 4) ^ (0 ^ 0) ^ (1 ^ 1) ^ 2 ^ (3 ^ 3)
= 0 ^ 0 ^ 0 ^ 0 ^ 2
= 2
程序:
C++
- //use math
- class Solution {
- public:
- int missingNumber(vector<int>& nums) {
- int n = nums.size();
- int res = (+n)*n/;
- for(int q:nums)
- res -= q;
- return res;
- }
- };
- //use xor
- class Solution {
- public:
- int missingNumber(vector<int>& nums) {
- int res = nums.size();
- for(int i = ; i < nums.size(); ++i){
- res = res ^ i ^ nums[i];
- }
- return res;
- }
- };
Java
- class Solution {
- public int missingNumber(int[] nums) {
- int n = nums.length;
- int res = (1+n)*n/2;
- for(int q:nums)
- res -= q;
- return res;
- }
- }
- class Solution {
- public int missingNumber(int[] nums) {
- int res = nums.length;
- for(int i = 0; i < nums.length; ++i){
- res = res ^ i ^ nums[i];
- }
- return res;
- }
- }
LeetCode 268. Missing Number缺失数字 (C++/Java)的更多相关文章
- [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 ☆(丢失的数字)
转载: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 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...
- 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 ...
随机推荐
- acwing 7 混合背包
习题地址 https://www.acwing.com/problem/content/description/7/ 题目描述有 N 种物品和一个容量是 V 的背包. 物品一共有三类: 第一类物品只 ...
- 2019-2020-1 20199305《Linux内核原理与分析》第六周作业
系统调用的三层机制(下) (一)给MenuOS增加命令 (1)打开虚拟机,首先用rm -rf menu指令删除当前的menu目录,然后用git clone重新克隆一个新版本的menu,进入menu,运 ...
- 剑指Offer-31.整数中1出现的次数(从1到n整数中1出现的次数)(C++/Java)
题目: 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.A ...
- 【LOJ6397】「THUPC2018」蛋糕 / Cake(搜索)
点此看题面 大致题意: 把一个\(a\times b\times c\times d\)的\(4\)维图形划分成\(a\times b\times c\times d\)个小块,求有\(0\sim8\ ...
- 【STM32H7教程】第31章 STM32H7的USART应用之RS485
完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第31章 STM32H7的USART应用之RS48 ...
- Docker学习——Dockerfile
上一篇我们讲了docker的基本使用,掌握了前一篇,docker使用基本不成问题,但是要是你学习了Dockerfile,你会发现它使用起来有多方便了.项目最终部署时,我们希望docker容器打开时项目 ...
- WPF 使用SetParent嵌套窗口
有点类似与Winform的MDI窗口. 使用函数为SetParent和MoveWindow(经常配合). [DllImport("user32.dll", SetLastError ...
- python爬取小说
运行结果: 代码: import requests from bs4 import BeautifulSoup from selenium import webdriver import os cla ...
- TreeViewItem节点添加图标后再加header
1.需要实现的效果如图 2.解决方案 1).给TreeViewItem中添加children,children为包含一个stackpanel,在stackpanel中包含Image和TextBlock ...
- php中搭建Web服务器和服务器配置
1.搭建Web服务器 1.1目录结构 1.2访问服务器 访问规则:http://服务器ip地址/php页面 比如: http://localhost/demo.php http://127.0 ...