[Swift]LeetCode268. 缺失数字 | Missing Number

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 中没有出现在序列中的那个数。

示例 1:

输入: [3,0,1]
输出: 2

示例 2:

输入: [9,6,4,2,3,5,7,0,1]
输出: 8

说明:
你的算法应具有线性时间复杂度。你能否仅使用额外常数空间来实现?


 1 class Solution {
 2     func missingNumber(_ nums: [Int]) -> Int {
 3         //利用异或运算,将数组全体内容与0~n进行异或,
 4         //根据异或运算的性质可知最后结果为缺少的那个数字。
 5         var result:Int = nums.count
 6         for i in 0..<nums.count
 7         {
 8             result ^= i ^ nums[i]
 9         }
10         return result
11     }
12 }


28ms

1 class Solution {
2     func missingNumber(_ nums: [Int]) -> Int {
3         var h = (nums.count+1)*nums.count/2
4         for i in 0..<nums.count {
5            h -= nums[i]
6         }
7         return h
8     }
9 }


24ms

 1 class Solution {
 2     func missingNumber(_ nums: [Int]) -> Int {
 3         var sum = 0
 4         var max = 0
 5         var i = 0
 6         while i < nums.count {
 7             max = max + i
 8             sum = sum + nums[i]
 9             i = i + 1
10         }
11         return max - sum + i
12     }
13 }


32ms:

求出从0~n的累加和,减去数组整体的和,那么由于数组内每个数字不相同,其差就是缺少的那个数字

 1 class Solution {
 2     func missingNumber(_ nums: [Int]) -> Int {
 3         let count = nums.count
 4         var sum = count + (count * (count - 1)) / 2
 5
 6         for i in 0..<count {
 7             sum -= nums[i]
 8         }
 9         return sum
10     }
11 }

原文地址:https://www.cnblogs.com/strengthen/p/9756490.html

时间: 2024-09-30 07:03:42

[Swift]LeetCode268. 缺失数字 | Missing Number的相关文章

[Swift]LeetCode163. 缺失区间 $ Missing Ranges

Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing ranges.For example, given [0, 1, 3, 50, 75], return [“2”, “4->49”, “51->74”, “76->99”] 给定一个排序的整数数数组,其中元素的范围包含[0,99],返回其缺少的范围. 例如,给定[0,1,3,50,75],返回

LeetCode:Missing Number - 缺失的数字

1.题目名称 Missing Number (缺失的数字) 2.题目地址 https://leetcode.com/problems/missing-number 3.题目内容 英文:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n  find the one that is missing from the array. 中文:给出一个包含了n个不同数字的数组,从0开始一直到n,找出缺失的数字.如果数

[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. 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 c

[LintCode] Find the Missing Number 寻找丢失的数字

Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example Given N = 3 and the array [0, 1, 3], return 2. Challenge Do it in-place with O(1) extra memory and O(n) time. 这道题是LeetCode上的原题,请参见我之前的博客Missing Number

[LeetCode] 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 u

LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (Easy) 分析:求n的阶乘中末位0的个数,也就是求n!中因数5的个数(2比5多),简单思路是遍历一遍,对于每个数,以此除以5求其因数5的个数,但会超时. 考虑到一个数n比他小

第一个缺失数字

Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 思路: 桶排的思想 从头到尾遍历数组,在位置 i,希望放置的元素是 i+1, 如果不是, 就把 A[ i

LeetCode 136. Single Number &amp; 268. Missing Number

136. Single Number 考察的是异或运算.相同的数异或结果为0,一个数与0异或还是原来的数,以及异或符合交换律.因此,把所有的数都异或起来,结果就是落单的那个数. class Solution { public: int singleNumber(vector<int>& nums) { int res=0; for (int num:nums){ res ^= num; } return res; } }; 268. Missing Number 可以用数学方法直接做,

[Swift中错误]missing argument label &#39;greeting:&#39; in call

Swift 中出现这个问题:从第二个参数起,自动加上lable func sayHello(name:String? ,greeting:String)->String { let result = greeting + "," + (name ?? "Guest") + "!" return result } var nickname:String? nickname = "yc" //“Goodmorning前面应该