Question

Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once.

Example 1:

Input: [1,1,2,3,3,4,4,8,8]

Output: 2

Example 2:

Input: [3,3,7,7,10,11,11]

Output: 10

Note: Your solution should run in O(log n) time and O(1) space.

Solution

这个题可以直接对所有元素取一遍异或可以得到答案,但是不满足时间复杂度的要求,log n的要求,明显只能计算一半;因为有一个单出来的,那么这个值所在的那一半肯定有奇数个数字。

Code

class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
if (nums.size() == 1)
return nums[0];
int middle = nums.size() / 2;
int left = middle - 1;
int right = middle + 1;
if (nums[middle] != nums[left] && nums[middle] != nums[right])
return nums[middle];
if (nums[middle] == nums[left] ) {
if ((middle + 1) & 1 != 0) {
int res = 0;
for (int i = 0; i <= middle; i++)
res = res ^ nums[i];
return res;
} else {
int res = 0;
for (int i = middle + 1; i < nums.size(); i++)
res = res ^ nums[i];
return res;
} }
if (nums[middle] == nums[right]) {
if ((middle + 1) & 1 != 0) {
int res = 0;
for (int i = middle; i < nums.size(); i++)
res = res ^ nums[i];
return res;
} else {
int res = 0;
for (int i = 0; i < middle; i++)
res = res ^ nums[i];
return res;
} }
}
};

LeetCode——Single Element in a Sorted Array的更多相关文章

  1. [LeetCode] 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) 42

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

  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

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

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

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

  6. [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 ...

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

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

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

  9. 540. Single Element in a Sorted Array

    题目大意: 给你一个由小到大排好序的数组,里面只有一个数出现了一次,其他数都出现了两次,要求找出那个只出现一次的数,而且时间复杂度为O(logn) 题目思路: 说实话一开始没想到,因为几乎每个数都出现 ...

随机推荐

  1. POJ3660 传递闭包———floyd算法

    POJ3660 Cow Contest 题目链接:http://poj.org/problem?id=3660 题意:农名约翰有些奶牛,约翰通过让他们决斗来决定他们的排名,约翰让这些奶牛一对一打完一定 ...

  2. php composer,update-ca-trust

    安装 ComposerComposer 需要 PHP 5.3.2+ 才能运行. $ curl -sS https://getcomposer.org/installer | php这个命令会将 com ...

  3. spring data jpa 遇到的问题

    org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.O ...

  4. Ckeditor事件绑定

    最近有个需求是要在点击CKeditor的时候触发某个判断的事件.试了一些方法都不可行,自己写的onclick时间都会被编辑器屏蔽.可以对对象加载完成绑定事件代码如下. CKEDITOR.instanc ...

  5. 基于flask的代码上传

    from flask import Flask,Blueprint,request,render_template from flask import current_app as app from ...

  6. SSH secure shell 原理与运用

    转: http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html 作者: 阮一峰 日期: 2011年12月21日 SSH是每一台Linux ...

  7. Jquery源码分析(一)

    版本: jQuery JavaScript Library v3.2.1 分析架构: 打开jquery.js,哇塞,一万多行,噩梦啊!很多人就say bye-bye了.其实,将代码结构拆分后,再分析源 ...

  8. [golang note] 流程控制

    流程控制 • 流程控制语句作用 ▪ 选择:根据条件跳转到不同的执行序列. ▪ 循环:根据条件反复执行某个序列. ▪ 跳转:据条件返回到某执行序列. • 流程控制语句类型 ▪ 条件语句:关键字为if.e ...

  9. 04 linux用户群组和权限

    作业一: 1)新建用户natasha,uid为1000,gid为555,备注信息为“master” 2)修改natasha用户的家目录为/Natasha 3)查看用户信息配置文件的最后一行 4)为na ...

  10. Linux系统——Rsync数据同步工具

    Rsync的优点及缺点 优点:类似cp命令.scp命令,但rsync为增量复制工具 缺点:针对大文件,效率非常高(打包再比对),针对小文件,效率非常低. Rsync作用 (1)可使本地和远程两台主机之 ...