题目

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?

分析

给定一个序列 找出只出现一次的元素,其它元素均出现2次;

要求线性时间,不需要额外空间。

最先采用的方法是先对序列排序,然后遍历一次即可找到,此时复杂度为O(nlogn),不过OJ竟然过了。

还是要思考下线性时间的实现方法的,

/*利用异或运算实现,复杂度为O(n) a^b = b^a ; a^a = 0 ; 0^a = a^0 = a*/

由此,对整个序列每个元素采取异或运算,0^a1^a2^…^an最终得到的结果即是那个只出现一次的元素。

这个方法实在是太赞了!可惜不是我想出来的… ^||^

AC代码

class Solution {
public:
/*利用算法库的排序实现,复杂度为O(nlogn)*/
//int singleNumber(vector<int>& nums) {
// if (nums.empty())
// return -1;
// sort(nums.begin(), nums.end());
//
// int size = nums.size(); // for (int i = 0; i < size - 1; i += 2)
// {
// if (nums[i] != nums[i + 1])
// {
// return nums[i];
// }//if
// }//for
// return nums[size - 1];
//} /*利用异或运算实现,复杂度为O(n) a^b = b^a ; a^a = 0 ; 0^a = a^0 = a*/
int singleNumber(vector<int>& nums) {
if (nums.empty())
return -1;
int ret = 0 , size = nums.size();
for (int i = 0; i < size; ++i)
ret = ret ^ nums[i];
return ret;
} };

GitHub测试程序源码

LeetCode(136) Single Number的更多相关文章

  1. LeetCode(137) Single Number II

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  2. LeetCode(260) Single Number III

    题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...

  3. LeetCode(202) Happy Number

    题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...

  4. 【LeetCode OJ 136】Single Number

    题目链接:https://leetcode.com/problems/single-number/ 题目:Given an array of integers, every element appea ...

  5. LeetCode(306) Additive Number

    题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...

  6. LeetCode(65) Valid Number

    题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...

  7. LeetCode(268) Missing Number

    题目 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is missi ...

  8. LeetCode(9)Palindrome Number

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  9. Leetcode(4)寻找两个有序数组的中位数

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...

随机推荐

  1. urllib库的基本使用

    urllib库的使用 官方文档地址:https://docs.python.org/3/library/urllib.html 什么是urllib Urllib是python内置的HTTP请求库包括以 ...

  2. Zip-line Codeforces - 650D || 风筝

    https://codeforces.com/contest/650/problem/D 原题? http://210.33.19.103/contest/1024/problem/2 4s 520M ...

  3. Unity小知识记录

    //判断运行的平台 Application.platform == RuntimePlatform.Android EditorPrefs 编辑器储存数据 [AddComponentMenu(&quo ...

  4. Linux 增加虚拟内存

    1. 用 df -h 命令找一个比较大的磁盘空间 2.建立swap文件 大小为2G count= 3.启用虚拟内存 1. 将swap文件设置为swap分区文件 mkswap swapfile #(由于 ...

  5. leetcode134 Gas Station

    思路: https://leetcode.com/problems/gas-station/discuss/269604/Java-Greedy-thought-process 关键是要想清楚如果从加 ...

  6. node.js0-5初级者

    伴着<妈是心中的茉莉花> 这里,我用的sublime记事本,所以用的运行方法是终端.(后来发现git 可以省去cd切换目录). 安装node.js  官网说的很清楚. 这里我们可以在js文 ...

  7. OAuth2.0基本原理及应用

    OAuth2.0基本原理及应用 一.OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 在详细讲解OAuth 2.0之前,需要了解几个专 ...

  8. MFC程序添加快捷键

    [问题提出]     有的程序需要自定义组合键完成一定功能,如何实现? [解决方法]     RegisterHotKey函数原型及说明:     BOOL RegisterHotKey(     H ...

  9. CSS布局之-强大的负边距

    css中的负边距(negative margin)是布局中的一个常用技巧,只要运用得合理常常会有意想不到的效果.很多特殊的css布局方法都依赖于负边距,所以掌握它的用法对于前端的同学来说,那是必须的. ...

  10. IOS命名

    NS开头的名称不要出现. NS系统名称开头. 命名缩写只用于通用专业术语,如URL,不可自创命名缩写,如Ctr.Msg.命名宁可长一些,也不要难于理解. 是否在看别人代码时各种缩写而不知其所以然?简短 ...