题目大意:

    给你一个由小到大排好序的数组,里面只有一个数出现了一次,其他数都出现了两次,要求找出那个只出现一次的数,而且时间复杂度为O(logn)

题目思路:

    说实话一开始没想到,因为几乎每个数都出现了两次那么对于一个偶数i,一定有nums[i] == nums[i+1]否则说明在这之前出现过只出现一次的数字。这样就是可以用二分查找,其实当读到题目说logn的复杂度时想都不用想是二分了

 class Solution:
def singleNonDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
lo = 0
hi = (len(nums)-1)
while lo < hi:
mi = (lo+hi)//2
if mi%2 != 0:
if nums[mi] != nums[mi-1]:
hi = mi-1
else:
lo = mi+1
else:
if nums[mi] != nums[mi+1]:
hi = mi-1
else:
lo = mi+1
return nums[lo]

540. Single Element in a Sorted Array的更多相关文章

  1. 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 ...

  2. LeetCode——540. Single Element in a Sorted Array

    题目:Given a sorted array consisting of only integers where every element appears twice except for one ...

  3. 【LeetCode】540. Single Element in a Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:异或 方法二:判断相邻元素是否相等 方法三:二分查找 ...

  4. 【leetcode】540. Single Element in a Sorted Array

    题目如下: 解题思路:题目要求时间复杂度是O(logN),可以尝试使用二分查找法.首先数组是有序的,而且仅有一个元素出现一次,其余均为两次.我们可以先找到数组最中间的元素,记为mid.如果mid和mi ...

  5. 540 Single Element in a Sorted Array 有序数组中的单一元素

    给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数.示例 1:输入: [1,1,2,3,3,4,4,8,8]输出: 2 示例 2:输入: [3,3,7,7,10,1 ...

  6. LeetCode 540. 有序数组中的单一元素(Single Element in a Sorted Array) 42

    540. 有序数组中的单一元素 540. Single Element in a Sorted Array 题目描述 每日一算法2019/6/14Day 42LeetCode540. Single E ...

  7. [LeetCode] Single Element in a Sorted Array 有序数组中的单独元素

    Given a sorted array consisting of only integers where every element appears twice except for one el ...

  8. [Swift]LeetCode540. 有序数组中的单一元素 | Single Element in a Sorted Array

    Given a sorted array consisting of only integers where every element appears twice except for one el ...

  9. LeetCode——Single Element in a Sorted Array

    Question Given a sorted array consisting of only integers where every element appears twice except f ...

随机推荐

  1. tomcat8.0部署启动

    http://tomcat.apache.org/download-80.cgi 打开命令行提示符窗口, 进入Tomcat安装目录, 进入bin目录下, 输入:service.bat install  ...

  2. vue的定位

    高德定位 https://blog.csdn.net/YY110621/article/details/87921605(copy) 话不多说,直接写方法步骤,需要的直接拿去放在自己项目中即可使用先看 ...

  3. C# Note10: AutoComplete TextBox in WPF

    参考: 1.https://stackoverflow.com/questions/950770/autocomplete-textbox-in-wpf 2.AutoCompleteBox的使用(实现 ...

  4. 阿里巴巴 Java开发手册1.4.0

    <阿里巴巴Java开发手册1.4.0>下载地址: 下载地址:https://102.alibaba.com/downloadFile.do?file=1528269849853/Java_ ...

  5. 转《vue引入第三方js库》

    一.绝对路径直接引入,全局可用 二.绝对路径直接引入,配置后,import 引入后再使用 三.webpack中配置 alias,import 引入后再使用 四.webpack 中配置 plugins, ...

  6. Linux 的相关操作

    切换权限   在linux环境下,用户之前的切换使用 “su - name,若要切换到root下面,则使用sudo su 命令即可. 在linux下安装软件,经常就是装完后不知道装到哪里去了 (201 ...

  7. java学习之—队列

    /** * 队列 * Create by Administrator * 2018/6/11 0011 * 下午 3:27 **/ public class Queue { private int m ...

  8. James 3.1服务器的安装与搭建

    参考:1. ububtu下基于docker安装配置Apache James 3.1.0: https://blog.csdn.net/bonwei/article/details/83061372 2 ...

  9. Docker 部署应用、jar 工程 docker 方式部署

    https://blog.csdn.net/jiangyu1013/article/details/81988342

  10. Nginx proxy_protocol协议

    L:113