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 ...
随机推荐
- NPOI通过DataTable导出和读取Excel
Excel导入及导出问题产生: 从接触.net到现在一直在维护一个DataTable导出到Excel的类,时不时还会维护一个导入类.以下是时不时就会出现的问题: 导出问题: 如果是asp.net,你得 ...
- (转) Special members
原地址:http://www.cplusplus.com/doc/tutorial/classes2/ Special members [NOTE: This chapter requires p ...
- 设计模式 之 Organizing the Catalog 组织目录
Design patterns vary in their granularity and level of abstraction. Because thereare many design pat ...
- HttpURLConnection详解
HttpURLConnection详解 07. 五 / J2EE / 没有评论 HttpURLConnection类的作用是通过HTTP协议向服务器发送请求,并可以获取服务器发回的数据. Http ...
- Qt的十六进制的控件
Qt没有这样的Widget,自己写一个吧.我曾经用MFC写过一个,代码不多,不到2000行,估计用Qt写不到1000行就够了. 可以参考这个qhexedit2 - QHexEdit is a Bina ...
- jQuery插件之jqzoom
jqzoom是一款基于jQuery的图片方法插件. 使用方法:1.引入jQuery与jqzoom,jqzoom.css 2.准备两张一大一小大小相同的图片,小图片放在<img>标签的&qu ...
- C# 动态Linq(结合反射)
这篇文章决定对最近一个单机版Web程序用到的东西总结一下. 一.反射Linq之OrderBy 动态Linq结合反射对某字段排序: namespace 动态Linq { class Program ...
- FileAttributes枚举
FileAttributes枚举是一个专门用于标记硬盘上的文件属性的枚举,枚举的说明在这里:http://www.cnblogs.com/kissdodog/archive/2013/01/16/28 ...
- CircleImageView自定义圆形控件的使用
1.自定义圆形控件github地址: https://github.com/hdodenhof/CircleImageView 主要的类: package de.hdodenhof.circleima ...
- Java中的native方法
博客引用地址:Java中的native方法 今天花了两个小时把一份关于什么是Native Method的英文文章好好了读了一遍,以下是我依据原文的理解. 一. 什么是Native Method 简单地 ...