博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
136. Single Number; 137. Single Number II;260. Single Number III; 287. Find the Duplicate Number
阅读量:4571 次
发布时间:2019-06-08

本文共 2708 字,大约阅读时间需要 9 分钟。

#136. Single Number class Solution(object):     def singleNumber(self, nums):         """         :type nums: List[int]         :rtype: int         """         single=[]         for i in range(len(nums)):             if nums[i] not in single:                 single.append(nums[i] )             else:                 single.remove(nums[i])         return single[0]
class Solution(object):
def singleNumber(self, nums):         """         :type nums: List[int]         :rtype: int         """         hash_table={}         for i in nums:             if i not in hash_table:                 hash_table[i]=1             else:                 hash_table[i] += 1                 if hash_table[i] >=3:                     del hash_table[i]         return hash_table.keys()[0]
class Solution(object):     def singleNumber(self, nums):         """         :type nums: List[int]         :rtype: int         """         return (2*sum(set(nums)) - sum(nums))
#137. Single Number II class Solution(object):
def singleNumber(self, nums):         """         :type nums: List[int]         :rtype: int         """         hash_table={}         for i in nums:             if i not in hash_table:                 hash_table[i]=1             else:                 hash_table[i] += 1                 if hash_table[i] >=3:                     del hash_table[i]         return hash_table.keys()[0]
class Solution(object):     def singleNumber(self, nums):         """         :type nums: List[int]         :rtype: int         """         return (3*sum(set(nums)) - sum(nums))/2
 
260. Single Number III class Solution(object):     def singleNumber(self, nums):         """         :type nums: List[int]         :rtype: int         """         diff = 2*sum(set(nums)) - sum(nums)         for i in nums:             j=diff - i             if j in nums:                 return [j,i]
class Solution(object):     def singleNumber(self, nums):         """         :type nums: List[int]         :rtype: int         """         hash_table={}         for i in nums:             if i not in hash_table:                 hash_table[i]=1             else:                 hash_table[i] += 1                 if hash_table[i] ==2:                     del hash_table[i]         return hash_table.keys()
 
287. Find the Duplicate Number class Solution:     def findDuplicate(self, nums):         """         :type nums: List[int]         :rtype: int         """         hash_table ={}         for i in nums:             if i not in hash_table:                 hash_table[i]=1             else:                 hash_table[i]+=1                 if hash_table[i] >1:                     return i
 

转载于:https://www.cnblogs.com/ffeng0312/p/9236771.html

你可能感兴趣的文章
搜索1016
查看>>
配置算法(第4版)的Java编译环境
查看>>
PostgreSQL 9.6.0 手册
查看>>
编写带有点击特效的UIButton
查看>>
使用mac版思维导图软件MindNode
查看>>
理解面向对象编程(OOP Object Oriented Programming)
查看>>
[題解]luogu_P1144最短路計數
查看>>
每日一个linux命令5 -- rm
查看>>
外存===内存
查看>>
node.js中的fs.appendFile方法使用说明
查看>>
URAL 1297 求最长回文字符串
查看>>
HDU 1098 Ignatius's puzzle 费马小定理+扩展欧几里德算法
查看>>
【C/C++】指针
查看>>
Anconda3导入TensorFlow报错,错误:h5py\__init__.py:36: FutureWarning
查看>>
js中return;、return true、return false;区别
查看>>
容器技术|Docker三剑客之docker-machine
查看>>
SQL注入理解与防御
查看>>
yum本地源配置
查看>>
3.4 C与汇编程序的相互调用
查看>>
浅析 JavaScript 链式调用
查看>>