博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 137. Single Number I/II/III
阅读量:4326 次
发布时间:2019-06-06

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

Given an array of integers, every element appears twice except for one. Find that single one.

本题利用XOR的特性, X^0 = X, X^X = 0, 并且XOR满足交换律。

 

1 class Solution(object): 2     def singleNumber(self, nums): 3         """ 4         :type nums: List[int] 5         :rtype: int 6         """ 7         s = 0 8         for x in nums: 9             s= s^x10         11         return s

 

single number II/III可以用位操作。用Hash table也可以通过OJ

class Solution(object):    def singleNumber(self, nums):        """        :type nums: List[int]        :rtype: int        """        dict = {}        for i in range(len(nums)):            if nums[i] not in dict:                dict[nums[i]] = 1            else:                dict[nums[i]] += 1                for word in dict:            if dict[word] == 1:                return word

 

转载于:https://www.cnblogs.com/lettuan/p/6084174.html

你可能感兴趣的文章
PHP Curl发送数据
查看>>
HTTP协议
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Ubuntu菜鸟入门(五)—— 一些编程相关工具
查看>>
valgrind检测linux程序内存泄露
查看>>
Hadoop以及组件介绍
查看>>
1020 Tree Traversals (25)(25 point(s))
查看>>
第一次作业
查看>>
“==”运算符与equals()
查看>>
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>