4.Single Number && Single Number (II)
Single Number:
1. 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(int A[], int n) {
int result = 0;
for(int i = 0; i < n; ++i){
result ^= A[i];
}
return result;
}
};
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:(分别计算各位1的个数 mod 3)
class Solution {
public:
int singleNumber(int A[], int n) {
int count[32] = {0};
int result = 0;
int bit = sizeof(int) * 8;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < bit; ++j){
if((A[i] >> j) & 0x1) count[j] = (count[j] + 1) % 3;
}
}
for(int i = 0; i < bit; ++i){
result |= (count[i] << i);
}
return result;
}
};
方法2:(三个变量,分别记录出现一次,出现两次,出现三次的值)
class Solution {
public:
int singleNumber(int A[], int n) {
int one, two, three;
one = two = three = 0;
for(int i = 0; i < n; ++i){
two |= (one & A[i]);
one ^= A[i];
three = ~(one & two);
one &= three;
two &= three;
}
return one;
}
};
4.Single Number && Single Number (II)的更多相关文章
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 《Mastering Opencv ...读书笔记系列》车牌识别(II)
http://blog.csdn.net/jinshengtao/article/details/17954427 <Mastering Opencv ...读书笔记系列>车牌识别(I ...
- 人人都是 DBA(II)SQL Server 元数据
SQL Server 中维护了一组表用于存储 SQL Server 中所有的对象.数据类型.约束条件.配置选项.可用资源等信息,这些信息称为元数据信息(Metadata),而这些表称为系统基础表(Sy ...
- 【leetcode】Number of Islands(middle)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 函数式Android编程(II):Kotlin语言的集合操作
原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...
- 用Kotlin开发Android应用(II):创建新项目
这是关于Kotlin的第二篇.各位高手发现问题,请继续“拍砖”. 原文标题:Kotlin for Android(II): Create a new project 原文链接:http://anton ...
- 逻辑回归 vs 决策树 vs 支持向量机(II)
原文地址: Logistic Regression vs Decision Trees vs SVM: Part II 在这篇文章,我们将讨论如何在逻辑回归.决策树和SVM之间做出最佳选择.其实 第一 ...
- 详解 ML2 Core Plugin(II) - 每天5分钟玩转 OpenStack(72)
上一节我们讨论了 ML2 Plugin 解决的问题,本节将继续研究 ML2 的架构. ML2 对二层网络进行抽象和建模,引入了 type driver 和 mechansim driver. 这两类 ...
- 百度地图学习(II)-Android端的定位
哎,经历了小编的最近时间的研究,我的百度定位终于成功啦,刹那间觉得自己萌萌哒啦(- ̄▽ ̄)- 话不多说,直接进入正题: 首先,我们来看一下效果: [分析定位原理] [编码分析] 1)处理程序的清单文件 ...
随机推荐
- bzoj 3124: [Sdoi2013]直径
#include<cstdio> #include<iostream> #define M 400009 #define ll long long using namespac ...
- IT公司100题-tencent-打印所有高度为2的路径
问题描述: 打印所有到叶子节点长度为2的路径 10 / \ 6 16 / \ / \ 4 8 14 18 / \ / \ \ 2 5 12 15 20 / 11 ...
- java 内存机制简介
java内存回收机制 不论哪种语言的内存分配方式,都需要返回所分配内存的真实地址,也就是返回一个指针到内存块的首地址.java中对象是采用new或者反射的方法创 建的,这些对象的创建都是在堆中分配,所 ...
- Android 每次访问网络时,都需要判断是否有网络
/** * 在执行网络操作之前判断网络是否链接可用 * * @return true 可用 false 不可用 */ private boolean isOnline() { Connectivity ...
- 使用Ueditor的心得。
现在有一个项目,需要富文本插件,以前用的都是国外的CKEditor,后来百度推出了自己的富文本编辑插件Ueditor,试用了一下,感觉不错. 遂决定在新项目中使用该插件. 在使用Ueditor上传图片 ...
- HTML编码规则、CSS属性声明顺序--简介
From AmazeUI:http://amazeui.org/getting-started/html-css-guide HTML 属性顺序 HTML 属性应当按照以下给出的顺序依次排列,确保代码 ...
- HM NIS Edit 2.0.3 Win32 Error. Code:740.请求的操作需要提升
使用NSIS安装向导,生成脚本后,按F9后,居然提示:HM NIS Edit 2.0.3 Win32 Error. Code:740.请求的操作需要提升 一开始就出错了,还真不顺. 在网上搜索了一下, ...
- HDU 1508 DP
题意:规定一个数列 = {这个数的质因子只能包括2,3,5,7},求第n个数字是多少: 思路:暴力打表,然后只粘数据,虽然过了,但是正解其实是DP,每一个数字都是由某一个该数列里的某一个数字乘以2,3 ...
- AppStore 内购验证的方法
AppStore增加了验证内购(In App Purchasement)的方法, 就是苹果提供一个url地址, 开发测试用: https://sandbox.itunes.apple.com/veri ...
- Ubuntu下安装配置JDK 7
第一步:下载jdk-7-linux-i586.tar.gz wget -c http://download.oracle.com/otn-pub/java/jdk/7/jdk-7-linux-i586 ...