【leetcode】540. Single Element in a Sorted Array
题目如下:

解题思路:题目要求时间复杂度是O(logN),可以尝试使用二分查找法。首先数组是有序的,而且仅有一个元素出现一次,其余均为两次。我们可以先找到数组最中间的元素,记为mid。如果mid和mid-1以及mid+1都不相同,那么mid就是single number。如果mid和mid-1相同,就要分两种情况,a.mid是奇数,single number会出现在mid的右半边;b.mid是偶数,出现在左半边。同理,如果mid和mid+1相同,那么是相反的:a.mid是奇数,出现在左半边;b.mid是偶数,出现在右半边。得到这个规律后,就可以放心的使用二分查找了。
代码如下:
class Solution(object):
def singleNonDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
low = 0
high = len(nums)-1
res = 0
while low <= high:
mid = (low + high) /2
#print mid,nums[mid]
if mid == 0 or mid == len(nums)-1:
res = nums[mid]
break
if nums[mid] == nums[mid +1]:
if mid % 2 == 1:
high = mid - 1
else:
low = mid + 1
elif nums[mid] == nums[mid-1]:
if mid % 2 == 1:
low = mid + 1
else:
high = mid - 1
else:
res = nums[mid]
break
return res
【leetcode】540. Single Element in a Sorted Array的更多相关文章
- 【LeetCode】540. Single Element in a Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:异或 方法二:判断相邻元素是否相等 方法三:二分查找 ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
[LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- LeetCode - 540. Single Element in a Sorted Array
Given a sorted array consisting of only integers where every element appears twice except for one el ...
- LeetCode——540. Single Element in a Sorted Array
题目:Given a sorted array consisting of only integers where every element appears twice except for one ...
- 【LeetCode】961. N-Repeated Element in Size 2N Array 解题报告(Python & C+++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)
Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array (3 solutions)
Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you ...
- 【leetcode】961. N-Repeated Element in Size 2N Array
题目如下: In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is r ...
随机推荐
- Octavia 的实现与分析(OpenStack Rocky)
目录 文章目录 目录 Octavia 基本对象概念 基本使用流程 软件架构 服务进程清单 代码结构 loadbalancer 创建流程分析 network_tasks.AllocateVIP netw ...
- docker nginx ssl
docker run -d -p 80:80 -p 443:443 -v $(pwd)/conf/conf.d:/etc/nginx/conf.d -v $(pwd)/cert:/etc/nginx/ ...
- [ScreenOS] How to manually generate a new system self-signed certificate to replace the expired system self-signed certificate without resetting the firewall
SUMMARY: This article provides information on how to manually generate a new system self-signed cert ...
- scala 使用case 关键字定义类不需要创建对象直接调用
1.必须是使用case 定义object类 package config import org.apache.spark.sql.SparkSession import org.apache.spar ...
- Win10.输入法(控制面板)
1.之前 Win7 都是每个进程都是自己的输入法. 但是到了Win10 默认情况下 输入法是全局的,输入法切换成中文 所有进程都变成 中文输入,又是很不方便 也不习惯... 2.感觉 WIn10 真不 ...
- C语言Ⅰ博客作业01
1.你对计算机科学与技术专业了解是怎样? 本专业培养具有良好的科学素养,系统地.较好地掌握计算机科学与技术包括计算机硬件.软件与应用的基本理论.基本知识和基本技能与方法,能在科研部门.教育单位.企业. ...
- 【C语言--数据结构】线性顺序表
线性表的本质: 1.线性表(List)是零个或者多个数据元素的集合: 2.线性表中的数据元素之间是有顺序的: 3.线性表中的数据元素个数是有限的: 4.线性表中的数据元素的类型必须相同: 定义: 线性 ...
- 【监控笔记】【1.5】事件通知(event Notification)
关键词:DDL监控 [监控笔记][1.5]事件通知(event Notification) 注意,只能通过删除新建来修改事件. [1]概念 事件通知是特殊类型的数据库对象,用于将有关服务器和数据库实践 ...
- Hibernate 日期映射 条件查询
1. hql: ...and accopt_time > ?" 2. query.setDate Query query = session.createQuery(hql); int ...
- ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;
参考来源:https://blog.csdn.net/yuxinha11/article/details/80090197 ENGINE=InnoDB不是默认就是这个引擎吗?——是的,如果不写也是ok ...