Single Number I

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?

分析:两个相同的数进行异或,结果为0。对所有数据进行异或,则可以得到所有数字中只出现一次的那个。

运行时间:18ms

 1 class Solution {
 2 public:
 3     int singleNumber(vector<int>& nums) {
 4         if(nums.size() == 0) return 0;
 5
 6         int result = 0;
 7         for(int i = 0; i < nums.size(); i++) result ^= nums[i];
 8
 9         return result;
10     }
11 };
时间: 2024-10-06 00:40:23

Single Number I的相关文章

Configuring Single Number Reach on Cisco CUCM 7.1.3

Cisco CUCM allows you to configure single number reach (also known as mobility) so that a call destined for your desk phone's extension can ring on a number of other devices such as your cellphone, blackberry, home phone, etc.  This feature also allo

260. Single Number III

260. Single Number III DescriptionHintsSubmissionsDiscussSolution DiscussPick One Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only o

Single Number II

题目描写叙述 链接地址 解法 算法解释 题目描写叙述 Given 3*n + 1 numbers, every numbers occurs triple times except one, find it. Example Given [1,1,2,3,3,3,2,2,4,1] return 4 Challenge One-pass, constant extra space. 链接地址 http://www.lintcode.com/en/problem/single-number-ii/

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? 思路: 一开始自己的思路非常简单,首先设置一个status的变量,然后通过2层for循环

leetcode Single Number III

题目连接 https://leetcode.com/problems/single-number-iii/ Single Number III Description Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only

LeetCode 136、137:Single Number I &amp; II

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? Single Number II Given an arr

LeetCode 笔记26 Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 没辙,智商碾压题.楼主没遇到之前就只会这种做法. public int si

137.Single Number II(法1排序法2STL容器map哈希法3位运算法4改进的位运算)

Given an array of integers, every element appears three timesexcept for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement itwithout using extra memory? HideTags Bit Manipulation #pragma once

Single Number II (17)

Given an array of integers, every element appears threetimes except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 这一次数字重复出现的次数是3了,不像是 single number中的2,不能

【LeetCode】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? 解答: 常规解法:先对数组进行排序,然后通过按顺序判断每相邻两个数是否相同即可