当前位置:网站首页>】 【 LeetCode daily one problem - 540. The order of a single element of the array
】 【 LeetCode daily one problem - 540. The order of a single element of the array
2022-08-04 17:02:00 【IronmanJay】
一【题目类别】
- 二分查找
二【题目难度】
- 中等
三【题目编号】
- 540.有序数组中的单一元素
四【题目描述】
- 给你一个仅由整数组成的有序数组,其中每个元素都会出现两次,唯有一个数只会出现一次.
- 请你找出并返回只出现一次的那个数.
- 你设计的解决方案必须满足 O(log n) 时间复杂度和 O(1) 空间复杂度.
五【题目示例】
示例 1:
输入: nums = [1,1,2,3,3,4,4,8,8]
输出: 2示例 2:
输入: nums = [3,3,7,7,10,11,11]
输出: 10
六【题目提示】
- 1 < = n u m s . l e n g t h < = 1 0 5 1 <= nums.length <= 10^5 1<=nums.length<=105
- 0 < = n u m s [ i ] < = 1 0 5 0 <= nums[i] <= 10^5 0<=nums[i]<=105
七【解题思路】
- 主要还是二分查找的思路
- Only the judgment conditions have changed,注意读题目:If a section does not have a single element before it,Then the length of this part must be even,index is odd,Otherwise, the length is an odd number,The index is also an even number
- So we still get the index of the middle element,如果索引为奇数,Then it means that the previous ones appear in pairs,There is no single element,But it is also necessary to judge whether the latter and the former are equal,That is to judge whether it is a pile,如果是的话,Explain that there is no single element on the left,Then modify the left index subscript to the right
- If the index is even,Explain that there must be a single element in the left part,Modify the right index subscript to the left
- 最后还需要注意whileThe judgment condition is that the left index is less than the right index,不能是小于等于,否则会死循环
八【时间频度】
- 时间复杂度: O ( l o g 2 N ) O(log_{2}N) O(log2N),其中 N N N为数组元素个数
- 空间复杂度: O ( 1 ) O(1) O(1)
九【代码实现】
- Java语言版
package BinarySearch;
public class p540_SingleElementInSortedArray {
public static void main(String[] args) {
int[] nums = {
1, 1, 2, 3, 3, 4, 4, 8, 8};
int res = singleNonDuplicate(nums);
System.out.println("res = " + res);
}
public static int singleNonDuplicate(int[] nums) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (mid % 2 == 1) {
mid--;
}
if (nums[mid] == nums[mid + 1]) {
left = mid + 2;
} else {
right = mid;
}
}
return nums[left];
}
}
- C语言版
#include<stdio.h>
int singleNonDuplicate(int* nums, int numsSize)
{
int left = 0;
int right = numsSize - 1;
while (left < right)
{
int mid = left + (right - left) / 2;
if (mid % 2 == 1)
{
mid--;
}
if (nums[mid] == nums[mid + 1])
{
left = mid + 2;
}
else
{
right = mid;
}
}
return nums[left];
}
/*主函数省略*/
十【提交结果】
Java语言版
C语言版
边栏推荐
猜你喜欢
浙江移动咪咕MGV2000-K4_ZJ_S905l2_7661_线刷固件包
吃透Chisel语言.32.Chisel进阶之硬件生成器(一)——Chisel中的参数化
Minecraft HMCL 第三方启动器使用教程
Boost库学习笔记(一)安装与配置
葫芦娃解析
Minecraft 我的世界 .minecraft下的各个文件夹的用处
Cesium快速上手0-Cesium安装与基本介绍
Hubei Mobile HG680-LV_S905L3B_wire brush firmware package
跨链桥已成行业最大安全隐患 为什么和怎么办
leetcode 48. Rotate Image 旋转图像(Medium)
随机推荐
【LeetCode每日一题】——374.猜数字大小
【Gazebo入门教程】第二讲 模型库导入与可视化机器人建模(模型编辑器)
Mobile magic box CM201-1_CW_S905L2_MT7668_wire brush firmware package
Mobile BesTV_R3300-L_S905L_8189_wire brush firmware package
手把手教你搭建一个Minecraft 服务器
R语言使用cov函数计算矩阵或者dataframe数据变量之间的协方差、cor函数计算相关性、cor函数通过method参数指定相关性、相关性计算方法Pearson,Spearman, Kendall
不需要服务器,教你仅用30行代码搞定实时健康码识别
WEB 渗透之越权
不需要服务器,教你仅用30行代码搞定实时健康码识别
跨域传递数据(iframe)
开一个羽毛球馆大概需要多少钱?大约15万左右可以搞定!
WEB 渗透之XXE&XML
Mobile zte ZXV10 B860AV2. 1 - A_S905L2_MT7668_ wire brush the firmware package
化学制品制造业数智化供应链管理系统:打造智慧供应体系,赋能企业产效提升
jMeter Transaction Controller 学习笔记
电气成套设备行业如何借助ERP系统,解决企业管理难题?
机器学习(十七):网格搜索(Grid Search)和SVM
SAP ABAP SteammPunk 蒸汽朋克的最新进展 - 嵌入式蒸汽朋克
shell脚本详解-------循环语句wuile循环和until循环
HCIP笔记(7)