leetcode 136 Single Number bBt Option

Linked Url:https://leetcode.com/problems/single-number/

Given a non-empty 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?

Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4

solution:

The method is xor option, principles is  : 0 xor 0 = 0; 0 xor 1 = 1;any  num xor itself  =  0,so we pass the array and xor its elements all,so the result  which is we want.

Ac code follow:

1 class Solution {
2 public:
3     int singleNumber(vector<int>& nums) {
4         int res = nums[0];
5         for(int i = 1;i < nums.size();++i)
6             res = nums[i]^res;
7         return res;
8     }
9 };

原文地址:https://www.cnblogs.com/CS-WLJ/p/11181720.html

时间: 2024-11-09 20:29:03

leetcode 136 Single Number bBt Option的相关文章

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 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 可以用数学方法直接做,

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] 136.Single Number Java

题目:Given an array of integers, every element appears twice except for one. Find that single one. 题意及分析:一个数组中,有一个数只出现了一次,其他的出现了两次.要求给出只出现一次的数.这道题,最简单的方法是用一个hashtable来储存,然后得出结果,但是效率比较低.也可以用位运算求解.直接使用异或运算,因为对于异或运算有:n^0=n,n^n=0,所以相同的数异或为0,最后得出的结果就为出现一次的数

LeetCode: 136 Single Number(easy)

题目: 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? 代码: 1 class Solution { 2 public: 3 int s

leetcode 136. Single Number &amp;&amp; 137. Single Number II

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? 异或运算应用: 异或运算满足交换律和结合律, 通过交换元素(如插入排序等)可以使得相同元

Java for 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 implement it without using extra memory? 解题思路: 按位异或运算,出现两次的都归零了,剩下的就是出现一次的了,JAVA实现如下

LeetCode 136. Single Number C++

Given a non-empty 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? Example 1: Input: [2,2,1] Output: