题目

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. [ZJOI2008]无序运动Movement

    Description D博士对物理有着深入的研究,经典物理.天体物理.量子物理都有着以他的名字命名的定理.最近D博士着迷于研究粒子运动的无规则性.对圣经深信不疑的他相信,上帝创造的任何事物必然是有序 ...

  2. [JSOI2009]密码

    Description Input Output Sample Input 10 2 hello world Sample Output 2 helloworld worldhello HINT 一看 ...

  3. nodejs学习(3) express+socket.io

    //node var express=require('express'); var app = express(); var server = require('http').createServe ...

  4. Parenthesis UVALive - 4882 删除不必要的括号序列,模拟题 + 数据

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. Linux--NiaoGe-Service-05

    1.设置网络参数的命令 命令名称 作用 ifconfig 查询.设置网卡与IP网络等相关参数 ifup.ifdown 启动.关闭网络接口 route 查看配置路由表(route table) ip 整 ...

  6. zoj3765Lights(splay)

    链接 splay的增删改操作. 刚开始对于某段区间首先有了lazy标记时,把其左右孩子给交换了,导致在pushup时又交换了一次而debug了n久. #include <iostream> ...

  7. 安卓,IOS真机调试

    移动端前端开发真机调试攻略 有线调试: 一.IOS 移动端 (Safari开发者工具) 手机端:设置 → Safari → 高级 → Web 检查器 → 开. mac端:Safari → 偏好设置 → ...

  8. LINQ to Entities不支持Convert.ToDateTime方法解決一例

    錯誤提示: LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' met ...

  9. matlab载入图像,旋转,裁剪 保存

    clc;clear all;close all src=imread('C:\Users\think\Desktop\12.jpg'); subplot(,,) imshow(src); I = ma ...

  10. LR中webservice服务测试的脚本

    Action(){ /* 测试QQ是否在线的功能接口 输入参数:QQ号码 String,默认QQ号码:8698053. 返回数据:String,Y = 在线:N = 离线:E = QQ号码错误:A = ...