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?
分析
给定一个序列 找出只出现一次的元素,其它元素均出现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;
}
};
LeetCode(136) Single Number的更多相关文章
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(260) Single Number III
题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...
- LeetCode(202) Happy Number
题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...
- 【LeetCode OJ 136】Single Number
题目链接:https://leetcode.com/problems/single-number/ 题目:Given an array of integers, every element appea ...
- LeetCode(306) Additive Number
题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...
- LeetCode(65) Valid Number
题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- LeetCode(268) Missing Number
题目 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is missi ...
- LeetCode(9)Palindrome Number
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- Leetcode(4)寻找两个有序数组的中位数
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...
随机推荐
- C# 可空类型(Nullable)
C# 提供了一个特殊的数据类型,nullable 类型(可空类型),可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值. 例如,Nullable< Int32 >,读作& ...
- CentOS7.2 安装iptables
1 先检查是否安装了iptables: service iptables status iptables -L ls /etc/sysconfig/ 综上:命令报错,且 iptables不存在,那 ...
- ubuntu下lnmp添加虚拟目录没有权限
lnmp.org下载的lnmp集成环境,通过lnmp vhost tsp创建了虚拟主机目录,将此目录导入到phpstorm中时提示错误,应该时权限的问题,想通过chmod -R 777 tsp来改变t ...
- php调用c# webservice方法
第一次用,通过,还没深入了解. 首先在php.ini中启用extension=php_soap.dll,重启apache. $Client=new SoapClient("url?wsdl& ...
- HTML中实现Table表头点击升序/降序排序
题目:如下图,请实现表格信息的排序功能,当点击表头的属性区域,将表格信息进行排序切换功能,即第一次点击为降序排序,再一次点击进行升序排序. 姓名 力量 敏捷 智力 德鲁伊王 17 24 13 月之骑士 ...
- CF1060D Social Circles
思路: 贪心.既然每个人的左边是其他人的右边,每个人的右边是其他人的左边,那么使重叠的部分最多即可. 实现: #include <bits/stdc++.h> using namespac ...
- https握手失败案例(一)
OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(15, TimeUnit.SECONDS) .read ...
- DataGridView使用技巧(七、设定列宽和行高自动调整)----.NET
DataGridView使用技巧(七.设定列宽和行高自动调整)----.NET 1) 设定行高和列宽自动调整 [VB.NET]' 设定包括Header和所有单元格的列宽自动调整DataGridView ...
- Python学习日志9月15日
一周就要过去了,而我跟一周以前没什么区别.回想一下,我这周做了什么事情呢.恍然若失.这周的精力都浪费在很多不必要的事情上了.学过一片古文,讲后羿学射箭,他有一个同学跟他一样聪明,在一起学习.后羿呢,专 ...
- ssh复制remote
rsync rsync localdirectory username@10.211.55.4:/home/username/Downloads/localdirectory -r