leetcode-Single NumberII
https://leetcode.com/problems/single-number-ii/
很无耻的又一次使用了黑暗料理...
class Solution:
# @param {integer[]} nums
# @return {integer}
def singleNumber(self, nums):
# 又是一道黑暗料理..
return (sum(set(nums))*3 - sum(nums))/2
速度还OK,不过这不是重点。
这回我要老老实实讨论一下这类题目的解答方法。一开始拿到这个题目就知道这肯定是一个关系到比特处理的题目。至于怎么处理,就需要我们细细思考了。之前有一道类似的题目,不过它的数组里面是重复的数字都出现了2遍,而这次是出现了3遍。
之前出现两遍的怎么解决呢?我们可以对每一个数组元素进行异或运算,对于重复出现的数字异或之后的结果肯定就是0了,而对于只出现了一次的数字,我们就可以得到它的反,对它再取一次反就可以得到他的结果辣。
但是现在我们却发现情况变成3次了,异或不好使了。。咋办。
这就像是我们要做一个机器,机器的输入是0或1,输出也是0或1,如果我么连续按三次一样的按钮,那么输出0,否则输出1。
所以顺着这个思路我们是不是可以这么写一个小机器:
#我是机器
def robot(input):
n = 0
for i in input:
if i == 1:
n += 1
if n%3==0:
return 0
else:
return 1
说到这里,其实我们的目的已经达到了。
我们可以对数组的每一个元素放到机器中,每一个比特进行处理记录,最后得到的结果就是我们需要的数。
代码具体如下(来自互联网):
class Solution {
public:
int singleNumber(int A[], int n) {
int count[]={};
int result=;
for(int i=;i<;i++){
for(int j=;j<n;j++){
count[i]+=((A[j]>>i)&); //首先把输入数字的第i位加起来。
count[i]=count[i]%; //然后求它们除以3的余数。
}
result|=(count[i]<<i);//把二进制表示的结果转化为十进制表示的结果
}
return result;
}
};
leetcode-Single NumberII的更多相关文章
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- LeetCode:Single Number II
题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...
- LeetCode Single Number III
原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...
- [LeetCode] Single Element in a Sorted Array 有序数组中的单独元素
Given a sorted array consisting of only integers where every element appears twice except for one el ...
- [leetcode]Single Number II @ Python
原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...
- [Leetcode] single number 找单个数
Given an array of integers, every element appears twice except for one. Find that single one. Note: ...
- [LeetCode] Single Number II 位运算
Given an array of integers, every element appears three times except for one. Find that single one. ...
随机推荐
- eclipse debug 快捷键
简单的说下调试的快捷键: 1 F5:下一步,可以进入下一个函数栈 2 F6:当前函数的下一步,不会进入其他的函数. 3 F8:下一个断点. 4 也可以通过选中一个变量或者表达式,按ctrl+shift ...
- 【GOF23设计模式】解释器模式 & 访问者模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_解释器模式.访问者模式.数学表达式动态解析库式 1.解释器模式Interpreter 2.访问者模式Visitor
- angular 指令——时钟范例
<html> <head> <meta charset='utf-8'> <title>模块化</title> <script typ ...
- How to Build Office Developer Tools Projects with TFS Team Build 2012
Introduction Microsoft Visual Studio 2012 provides a new set of tools for developing apps for Office ...
- iOS多线程-01-pthread与NSTread
简介 恰当的使用多线程编程可以提供任务的执行效率和系统资源的利用率 多线程是为了提高资源利用率,和应用程序的响应速度,多个线程共享应用资源 每个应用程序都有一个主线程,通常用来做UI界面刷新等 比较耗 ...
- android activity 管理器AMS----概述
AMS & WMS,应该是app端打交道最多的2个framwork层的service. ActivityManagerService 是android提供给用于管理Activity运行状态的系 ...
- git 一般的开发流程中的代码管理
一般的开发流程中的代码管理 1. 从版本库中下载代码 git clone ssh://wenbin@192.168.1.3:29418/mustang-web 2. 针对某个feature(比如ins ...
- Effective Java 75 Consider using a custom serialized form
Principle Do not accept the default serialized form without first considering whether it is appropri ...
- CentOS6.5(Python-2.7.12)安装Pip
1.安装setuptools(下载链接可从https://pypi.python.org/pypi/setuptools#code-of-conduct寻找) #Download setuptools ...
- 烂泥:KVM安装centos6.5系统
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 在前一篇文章中,我介绍了有关KVM的安装与配置.接下来,就来介绍如何在KVM中安装Centos系统. 注意该centos镜像文件,使用的是:CentOS ...