LeetCode 136. Single Number & 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

可以用数学方法直接做,求和减去数组的和就是所求答案。

用位操作来做的话,同样用异或。把这一道题向上一道题转化,即在数组元素全部异或以后,再把1~n的数字异或上,就和上一道题一模一样了。

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int res=0;
        int tmp=1;
        for (int i=0;i<nums.size();++i){
            res ^= nums[i]^(tmp++);
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/hankunyan/p/9127631.html

时间: 2024-08-24 23:26:49

LeetCode 136. Single Number & 268. Missing Number的相关文章

&amp;lt;LeetCode OJ&amp;gt; 268. Missing Number

268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium 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.

LeetCode 136 Single Number(只出现一次的数字)

翻译 给定一个整型数组,除了某个元素外其余元素均出现两次.找出这个只出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度.你可以不用额外空间来实现它吗? 原文 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you i

LeetCode 136 Single Number(仅仅出现一次的数字)

翻译 给定一个整型数组,除了某个元素外其余元素均出现两次. 找出这个仅仅出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度. 你能够不用额外空间来实现它吗? 原文 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could yo

【LeetCode】268. Missing Number

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 i

268. Missing Number (binary)

0到n的sum减去已经存在的就是missing number 1 //Old 2 class Solution { 3 public int missingNumber(int[] nums) { 4 int max = nums.length; 5 List<Integer> A = new ArrayList<Integer>(); 6 for(int i = 0; i <= max; i++) { 7 A.add(i); 8 } 9 for(int j = 0; j &

LeetCode - 136. Single Number - ( c++ ) - 解题报告

1.题目大意 Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 给定一个数组的整数,数组中的每个元素都出现了两次.例外地,有一个元素只出现

Leetcode 268 Missing Number 位运算

题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组,这样就和singe number 一样. 1 class Solution { 2 public: 3 int missingNumber(std::vector<int>& nums) { 4 int ans = 0; 5 for (std::vector<int>::siz

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 relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. Not

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. For example,Given nums = [0, 1, 3] return 2. Note:Your algorithm should run in linear runtime complexity. Could you implement it usi