LeetCode——Single Element in a Sorted Array
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的更多相关文章
- [LeetCode] 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) 42
540. 有序数组中的单一元素 540. Single Element in a Sorted Array 题目描述 每日一算法2019/6/14Day 42LeetCode540. Single E ...
- 【LeetCode】540. Single Element in a Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:异或 方法二:判断相邻元素是否相等 方法三:二分查找 ...
- 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 ...
- [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 ...
- 【leetcode】540. Single Element in a Sorted Array
题目如下: 解题思路:题目要求时间复杂度是O(logN),可以尝试使用二分查找法.首先数组是有序的,而且仅有一个元素出现一次,其余均为两次.我们可以先找到数组最中间的元素,记为mid.如果mid和mi ...
- 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 ...
- 540. Single Element in a Sorted Array
题目大意: 给你一个由小到大排好序的数组,里面只有一个数出现了一次,其他数都出现了两次,要求找出那个只出现一次的数,而且时间复杂度为O(logn) 题目思路: 说实话一开始没想到,因为几乎每个数都出现 ...
随机推荐
- 虚拟网卡 TUN/TAP 驱动程序设计原理
简介 虚拟网卡Tun/tap驱动是一个开源项目,支持很多的类UNIX平台,OpenVPN和Vtun都是基于它实现隧道包封装.本文将介绍tun/tap驱动的使用并分析虚拟网卡tun/tap驱动程序在li ...
- VMwareWorkstations中安装ubuntu,apt install报E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
ubuntu中apt安装软件python时报: E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily un ...
- 网络爬虫之scrapy框架设置代理
前戏 os.environ()简介 os.environ()可以获取到当前进程的环境变量,注意,是当前进程. 如果我们在一个程序中设置了环境变量,另一个程序是无法获取设置的那个变量的. 环境变量是以一 ...
- 10张Gif动图让你弄懂递归等概念
图像(包括动图)是传递信息的一种高效方式,往往能增强表象.记忆与思维等方面的反应强度.所谓一图胜千言,说的就是这个道理. 今天为大家整理了十张动图GIFS,有助于认识循环.递归.二分检索等概念的具体运 ...
- git学习------> Gitlab如何进行备份恢复与迁移?
前段时间,在某台CenterOS服务器上搭建了Gitlab环境,并且大家陆陆续续的都把代码从svn迁移到了gitlab,但是之前的CenterOS服务器并不是搭建在公司的机房环境,而是搭建在办公室的某 ...
- SpringBean 定义继承
Bean定义继承 bean定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等.子bean的定义继承副定义的配置数据.子定义可以根据需要重写一些值 ...
- Python高级教程-多重继承
多重继承 继承是面向对象编程的一个重要的方式,因为通过继承,子类可以扩展父类的功能. Animal类的层次设计,假设要实现以下4中动物: Dog - 狗狗: Bat - 蝙蝠: Parrot - 鹦鹉 ...
- sql server中使用xp_cmdshell
关键词:sql server开启高级配置,使用Bat,cmdshell 1.sql server中使用xp_cmdshell --允许配置高级选项 GO RECONFIGURE GO . --开启xp ...
- Zookeeper概述和基本概念
一.Zookeeper背景 随着互联网技术的发展,企业对计算机系统的计算,存储能力要求越来越高,各大IT企业都在追求高并发,海量存储的极致,在这样的背景下,单纯依靠少量高性能单机来完成计算机,云计算的 ...
- Linux系统——sed命令
sed命令精讲 cat工作原理 cat命令只想文件,把文件打开后,将文件中所有内容一次性读到内存中,从内存里一次性输出到屏幕上,此时可能存在内存装不下的情况,因此cat命令只能查看小文件内容,不能读取 ...