Single Number,Single Number 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?
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res = ;
int nums_size = nums.size();
for(int i=;i<nums_size;i++){
res ^= nums[i];
}
return res;
}
};
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?
统计各个二进制位中1的个数,该方法适用于这种类型的题目:数组中的所有数都出现了K次,只有一个数只出现了一次
const int BITS = sizeof(int) * ; class Solution {
public:
int singleNumber(vector<int>& nums) {
int times[BITS]={};
cout<<BITS<<endl;
int nums_size = nums.size();
for(int i=;i<nums_size;i++){
int x = nums[i];
for(int j=;j<BITS;j++){
if((x>>j) & ){
times[j]++;
}
}
}
int res = ;
for(int i=;i<BITS;i++){
if(times[i]%){
res += <<i;
}
}
return res;
}
};
Single Number III
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 once.
For example:
Given nums = [1, 2, 1, 3, 2, 5]
, return [3, 5]
.
Note:
- The order of the result is not important. So in the above example,
[5, 3]
is also correct. - Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity
所有的结果异或之后剩下一个非零值,这个值就是只出现一次的两个数的异或结果,找到这个数中第一个bit=1的数位,用1左移这么多个数位做为数组的分割器。
const int BITS = sizeof(int)*;
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int nums_size = nums.size();
int n = ;
for(int i=;i<nums_size;i++){
n ^= nums[i];
}
int seprator = ;
for(int i=;i<BITS;i++){
if((n>>i) & ){
seprator = <<i;
break;
}
}
vector<int> res;
int res1=,res2=;
for(int i=;i<nums_size;i++){
if( (seprator & nums[i])){
res1 ^= nums[i];
}else{
res2 ^= nums[i];
}
}
res.push_back(res1);
res.push_back(res2);
return res;
}
};
Single Number,Single Number II的更多相关文章
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
- 4.Single Number && Single Number (II)
Single Number: 1. Given an array of integers, every element appears twice except for one. Find that ...
- Single Number i and ii
Single Number Given an array of integers, every element appears twice except for one. Find that sing ...
- [ActionScript 3.0] 用TextField的方法getCharIndexAtPoint(x:Number, y:Number):int实现文字在固定范围内显示
有时候我们遇到一行文字过多时必须固定文字的显示范围,但由于中英文所占字节数不一样,所以不能很好的用截取字符的方式去统一显示范围的大小,用TextField的getCharIndexAtPoint(x: ...
- leetcode@ [136/137] Single Number & Single Number II
https://leetcode.com/problems/single-number/ Given an array of integers, every element appears twice ...
- [Leetcode]Single Number && Single Number II
Given an array of integers, every element appears twice except for one. Find that single one. 非常简单的一 ...
- [LeetCode#136, 137]Single Number, Single Number 2
The question: Single Number Given an array of integers, every element appears twice except for one. ...
- leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number
一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
随机推荐
- spring cuowu
spring常见错误总结 在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结,希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with nam ...
- Python学习笔记4(函数与模块)
1.Python程序的结构 Python的程序由包(package).模块(module)和函数组成. 模块是处理一类问题的集合,由函数和类组成. 包是由一系列模块组成的集合.包是一个完成特定任务的工 ...
- UnSupported Encoding错误
学习struts2的时候碰到tomcat报错:org.apache.jasper.JasperException: Unsupported encoding: UTF-8,原因是jsp文件中的pag ...
- inPolygonTest学习和C++实现
大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 此篇博客实现了判定平面一点是否在给定多边形内部的功能.精确,性能优良,因为只包含加法和乘法运算,效 ...
- typecho路由机制详解
本文介绍的是typecho的路由机制,引自 不烦恼路由机制是typecho的核心,有很多功能都是基于路由功能设计的,理解并熟悉TE的路由机制将非常有助于插件的开发. 完整的路由表如下: array ( ...
- Eclipse Juno 配置反编译插件
1 下载最新的jadClipse插件,地址:http://sourceforge.net/projects/jadclipse/files/jadclipse3.3/ 2 将net.sf.jadcli ...
- Extjs Tooltip属性的使用
要想让 tooltip生效必须:Ext.QuickTips.init();
- c++容器使用总结(转载)
目录 ==================================================== 第一章 容器 第二章 Vector和string 第三章 关联容器 第四章 迭代器 第五 ...
- 剑指offer-面试题20.顺时针打印矩阵
题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字.例如: 输入一个矩阵如下: 则依次打印出数字:1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10 这道题的 ...
- HDU5137 How Many Maos Does the Guanxi Worth(枚举+dijkstra)
How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/5 ...