leetcode问题:缺失数字

给定一个包含 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 {
    public int missingNumber(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length;
        int res = 0;
        int total =(0+ n ) * (n+1) / 2;
        int realSum = 0;
        for(int i = 0; i < n; i++)
        {
            realSum += nums[i];
        }
        return (total - realSum);
        
    }
}

解法2:思路:使用异或^运算符

**举个例子:**

  • 0 ^ 4 = 4
  • 4 ^ 4 = 0

那么,就可以不用求和,直接使用异或运算^进行**抵消**,剩下的数字就是缺失的了。

int missingNumber(vector<int>& nums) {
    int sum = nums.size();
    for (int i = 0; i < nums.size(); ++i){
        sum ^= nums[i];
        sum ^= i;
    }
    return sum;
}

原文地址:https://www.cnblogs.com/zhangchuan1001/p/10618723.html

时间: 2024-07-30 13:20:42

leetcode问题:缺失数字的相关文章

Leetcode 268.缺失数字 By Python

给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [9,6,4,2,3,5,7,0,1] 输出: 8 说明: 你的算法应具有线性时间复杂度.你能否仅使用额外常数空间来实现? 思路 因为给定的序列也是从0开始,所以可以进行排序,比较索引和索引对应的值,如果两个不等于说明就确实一个值了,还要注意一个情况是,没出现的数字是n 代码 class Solution(obje

第一个缺失数字

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:缺失的第一个正数【41】

LeetCode:缺失的第一个正数[41] 题目描述 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3示例 2: 输入: [3,4,-1,1] 输出: 2示例 3: 输入: [7,8,9,11,12] 输出: 1说明: 你的算法的时间复杂度应为O(n),并且只能使用常数级别的空间. 题目分析 参照链接:https://leetcode-cn.com/problems/first-missing-positive/solution/tong

LeetCode 第268题 缺失数字

给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1]输出: 2示例 2: 输入: [9,6,4,2,3,5,7,0,1]输出: 8 思路1: 0~n个数的总和 - 数组中所有数的和 = 缺失的数思路2: 异或 1 class Solution268 { 2 public int missingNumber(int[] nums) { 3 int n = nums.length + 1; 4 long

【leetcode 简单】 第七十四题 缺失数字

给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [9,6,4,2,3,5,7,0,1] 输出: 8 说明: 你的算法应具有线性时间复杂度.你能否仅使用额外常数空间来实现? class Solution: def missingNumber(self, nums): """ :type nums: List[int] :rtype: int 等

leetcode 缺失数字

给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [9,6,4,2,3,5,7,0,1] 输出: 8 方法1:排序去重查找 var missingNumber = function (nums) { var naturalNum = 0; var newArr = []; function sortNumber(a,b) { return a - b; } num

[leetcode] 41. 缺失的第一个正数

41. 缺失的第一个正数 注意这题要求时间复杂度应为O(n),并且只能使用常数级别的空间. 挺有意思的一个题. 思路: 通过交换元素的位置使得正确的位置仅存放正确的数字,例如给定一个数字3那么他应该在第三个位置,下标为2 .当数字>n 或 <1 时,不做操作.当出现重复数字时,置成-1 比如输入3,4,-1,1 扫描一遍后应该是 1 -1 3 4 class Solution { public int firstMissingPositive(int[] nums) { int n = num

LeetCode.2-两个数字相加(Add Two Numbers)

这是悦乐书的第340次更新,第364篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第1题(顺位题号是2).给定两个非空链表,表示两个非负整数. 数字以相反的顺序存储,每个节点包含一个数字.将两个数字相加并将其作为链表返回.你可以假设这两个数字不包含任何前导零,除了数字0本身.例如: 输入:(2 -> 4 -> 3)+(5 -> 6 -> 4) 输出:7 -> 0 -> 8 说明:342 + 465 = 807. 本次解题使用的开发工具是

【LeetCode】缺失的第一个正数【原地HashMap】

给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8,9,11,12] 输出: 1 说明: 你的算法的时间复杂度应为O(n),并且只能使用常数级别的空间. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/first-missing-positive 分析: 要求时间复杂度为O(N),并且只能使用常数级别的