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. Netty 粘包/半包原理与拆包实战

    Java NIO 粘包 拆包 (实战) - 史上最全解读 - 疯狂创客圈 - 博客园 https://www.cnblogs.com/crazymakercircle/p/9941658.html 本 ...

  2. Chrome cookies folder

    w本地存储数据2种形式. http://superuser.com/questions/292952/chrome-cookies-folder-in-windows-7 chrome://setti ...

  3. Flume简介及使用

    一.Flume概述 1)官网地址 http://flume.apache.org/ 2)日志采集工具 Flume是一种分布式,可靠且可用的服务,用于有效地收集,聚合和移动大量日志数据.它具有基于流数据 ...

  4. centos7修改hostname和hosts

    1.修改/etc/hostname vi /etc/hostname 打开之后的内容是: localhost.localdomain 把它修改成想要的名字就可以,比如:master 保存退出 2.修改 ...

  5. Linux命令:nl

    全称:number lines of files 用途:显示的时候添加行号. 格式:nl [OPTION]... [FILE]... 类型:nl is /usr/bin/nl 说明: 该命令主要就是针 ...

  6. Mybatis框架学习总结-Mybatis框架搭建和使用

    Mybatis介绍 Mybatis是一个支持普通SQL查询,存储过程,和高级映射的优秀持久层框架.Mybatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.Mybatis可以使 ...

  7. Centos Docker1.12 远程Rest api访问的配置方法

    Docker默认是没有开启HTTP远程访问的,默认只支持通过unix socket通信操作docker daemon,需要使用HTTP restful接口需要修改配置. 1.修改配置文件,文件位置/l ...

  8. Hadoop2.7.3 HA高可靠性集群搭建

    1.背景介绍 Hadoop2.0.0之前,在一个HDFS集群中,NameNode存在单节点故障(SPOF):因为集群中只有一个NameNode,所以在使用过程中,如果该NameNode出现故障或数据丢 ...

  9. PAT 1130 Infix Expression[难][dfs]

    1130 Infix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspond ...

  10. yield的表达式形式的应用(待补充)

    1.yield的表达式形式应用的定义: 在一个生成器函数内,将yield赋值给一个变量,这就是yield的表达式形式.也叫生成器的表达式形式 2.send方法的定义: (1)定义: yield的表达式 ...