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 ...
随机推荐
- Dapper simplecrud的使用
为了方便Dapper操作可以使用Dapper的相关扩展dapper simplecrud. 1.首先点击管理NuGet
- 使用Win32 API 查找文件
头文件:#include <windows.h> //FindFirstFile() 获得指定目录的第一个文件 HANDLE FindFirstFile( LPCTSTR lpFileNa ...
- QSS 样式表 (一)
在开发应用程序时,往往对界面的美观有一定的要求.Qt 引入了 QSS 机制,使得界面的美化工作变的轻轻松松.嗯,QSS听着有点耳熟.是的,QSS的语法和CSS类似.在此做些总结. 先来看一个简单的例子 ...
- 退出ssh,程序继续运行的解决办法
对Unix.Linux类服务器维护经常是通过ssh完成的,而有些操作执行时间较长,如:更新程序.文件备份.软件编译安装等.此时如果断开ssh连接的话,更新程序就会随之被中断.如何保证断开ssh后仍旧能 ...
- activiti笔记四 关于部署信息表act_re_deployment
一.简要描述 部署流程定义时需要被持久化保存下来的信息.二.表结构说明 字段名称 字段描述 数据类型 主键 为空 取值说明 ID_ ID_ nvarchar(64) √ 主键ID NAME_ 部署名称 ...
- Roads in the North(POJ 2631 DFS)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- Sticks(Central Europe 1995) (DFS)
Sticks(Central Europe 1995) Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d &am ...
- keil TEA
http://bbs.mydigit.cn/read.php?tid=545086 #include "reg52.h" void send_char(unsigned char ...
- Qt学习(四)—实例涂鸦画板mspaint
一.Qt图形绘制 自己在Qt开源社区在自学yafeilinux前辈的<Qt快速入门系列教程>中的图形篇,结合所学的知识,可以做一个涂鸦板实例 二.实现涂鸦板 1.新建工程mspaint, ...
- [ASP.NET] 图形验证码破解-以简单图形为例
原文 http://www.dotblogs.com.tw/joysdw12/archive/2013/06/08/captcha-cracked.aspx 前言 这次来讲个比较有趣的主题,就是该如何 ...