268. 缺失数字

地址:https://leetcode-cn.com/problems/missing-number/

<?php

/**
给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。

示例 1:

输入: [3,0,1]
输出: 2
示例 2:
 */
class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function missingNumber1($nums) {
        sort($nums);
        $start = 0;
        for ($i = 1;$i < count($nums);$i++){
            if ($nums[$i] - $nums[$start] != 1){
                return $nums[$i]-1;
            }
            $start = $i;
       }
    }

    //异或运算
    function missingNumber($nums) {
        $ans = count($nums);
        for($i=0;$i<count($nums);$i++){
            $ans ^= $nums[$i];
            $ans ^= $i;
        }
        return $ans;
    }
}

$solution = new Solution();
$nums = [3,0,1];
var_dump($solution->missingNumber($nums));

原文地址:https://www.cnblogs.com/8013-cmf/p/12690208.html

时间: 2024-08-30 15:17:30

268. 缺失数字的相关文章

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

力扣-268.缺失数字 题解

题目 给定一个包含 0, 1, 2, ..., n?中?n?个数的序列,找出 0 .. n?中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例?2: 输入: [9,6,4,2,3,5,7,0,1] 输出: 8 说明: 你的算法应具有线性时间复杂度.你能否仅使用额外常数空间来实现? 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/missing-number 著作权归领扣网络所有.商业转载请联系官方授权,非商业

第一个缺失数字

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 第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 说明: 你的算法应具有线性时间复杂度.你能否仅使用额外常数空间来实现? 解法1:思路:采用数列求和的方式 class Solution {    public int missingNumber(int[] nums) {        Arrays.sort(n

[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

【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

leet

# 题名1 两数之和    2 两数相加    3 无重复字符的最长子串    4 寻找两个有序数组的中位数    5 最长回文子串    6 Z 字形变换    7 整数反转    8 字符串转换整数 (atoi)    9 回文数    10 正则表达式匹配    11 盛最多水的容器    12 整数转罗马数字    13 罗马数字转整数    14 最长公共前缀    15 三数之和    16 最接近的三数之和    17 电话号码的字母组合    18 四数之和    19 删除链表