【LeetCode】137. Single Number II 解题报告(Python)
【LeetCode】137. Single Number II 解题报告(Python)
标签: LeetCode
题目地址:https://leetcode.com/problems/single-number-ii/description/
题目描述:
Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
题目大意
有一个数组,每个数字都出现了3次,除了其中的某一个只出现了1次。找出这个只出现了1次的数字。
解题方法
这个题本身不是很难,注意,不能使用异或运算搞定了。这个题的做法是把32位的二进制数进行遍历,统计每个数字的每一位出现的和。因为每个数字出现了3次或者1次,所以如果某一位出现的次数不是3次,那么这个位置一定是因为那个只出现1次的数字导致的。用来保存结果的res是0,因此使用或操作,就能把这个位置的数字变成1.
需要注意的是:python的整型方便是方便了,但是由于其没有最大值,所以,当输入是一堆负数的时候,会导致认为结果是个整数!因为32位有符号的被认为成了无符号的,所以这就是Python的一个坑。。
注意一下结论,以后出现位运算的时候,需要对结果进行判断一下最好。如果不在这个范围内,说明了结果被认为是无符号的数了,需要减去2 ^ 32。
16位整数中-32768到32767
32位整数中-2147483648到2147 483 647
最高位为符号位 ,请您计算2的15次方以及2的31次方,就可以得到以上结果
16位整数-2^15~2^15-1
32位整数-2^31~2^31-1
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
res = 0
for i in range(32):
cnt = 0
mask = 1 << i
for num in nums:
if num & mask:
cnt += 1
if cnt % 3 == 1:
res |= mask
if res >= 2 ** 31:
res -= 2 ** 32
return res
日期
2018 年 3 月 14 日 –霍金去世日
【LeetCode】137. Single Number II 解题报告(Python)的更多相关文章
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- Leetcode 137 Single Number II 仅出现一次的数字
原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...
- [LeetCode] 137. Single Number II 单独数 II
Given a non-empty array of integers, every element appears three times except for one, which appears ...
- LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)
翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of inte ...
- [LeetCode] 137. Single Number II 单独的数字之二
Given a non-empty array of integers, every element appears three times except for one, which appears ...
- 详解LeetCode 137. Single Number II
Given an array of integers, every element appears three times except for one, which appears exactly ...
- Java [Leetcode 137]Single Number II
题目描述: Given an array of integers, every element appears three times except for one. Find that single ...
- LeetCode - 136. Single Number - ( C++ ) - 解题报告 - 位运算思路 xor
1.题目大意 Given an array of integers, every element appears twice except for one. Find that single one. ...
- Java for LeetCode 137 Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
随机推荐
- 37-Invert Binary Tree
Invert Binary Tree My Submissions QuestionEditorial Solution Total Accepted: 87818 Total Submissions ...
- Identity Server 4 从入门到落地(六)—— 简单的单页面客户端
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- A Child's History of England.2
They made boats of basket-work, covered with the skins of animals, but seldom, if ever, ventured far ...
- 外网无法访问hdfs文件系统
由于本地测试和服务器不在一个局域网,安装的hadoop配置文件是以内网ip作为机器间通信的ip. 在这种情况下,我们能够访问到namenode机器, namenode会给我们数据所在机器的ip地址供我 ...
- 零基础学习java------day8------javabean编写规范,继承,static关键字,代码块,单例设计模式
0. 今日内容提要 1. javabean书写规范 javabean:一个普通的类,用来描述事物的类,里面不包含任何的业务逻辑,只是用来存储数据. 比如:Teacher,Student,Mobile. ...
- Gradle—Android配置详解
参考[1]彻底弄明白Gradle相关配置 [2]Android Studio gradle配置详解
- 转 Android Studio中Junit调试
转:https://blog.csdn.net/xanthus_li/article/details/54314189 在程序开发完成后,需要交给专业的调试人员进行相关的专业调试(白盒测试,黑盒测试, ...
- 4.3 rust func closure
fn add_one_v1 (x: u32) -> u32 { x + 1 } let add_one_v2 = |x: u32| -> u32 { x + 1 }; let add_on ...
- 【编程思想】【设计模式】【行为模式Behavioral】观察者模式Observer
Python转载版 https://github.com/faif/python-patterns/blob/master/behavioral/observer.py #!/usr/bin/env ...
- 捷码:重塑DevOps,打造更流畅紧密的开发与服务交付业务链
捷码Gemcoder 1周前如果有机会安排一场行业吐槽大会,熟悉软件开发.交付.服务业务各环节的业内人士,对开发中的各种扯皮.交付反反复复.运维服务中的提心吊胆,往往会有很多深刻的体验和刻骨铭心的案例 ...