当前位置:网站首页>[daily question] dichotomy - find a single dog (Bushi)
[daily question] dichotomy - find a single dog (Bushi)
2022-07-03 03:48:00 【Code Fox】
️ Winter vacation xinkeng —— Code Fox's daily notes
The winter vacation is about to expire
540. A single element in an ordered array -Mid( Binary search deformation )
Give you an ordered array of integers only , Each of these elements will appear twice , Only one number will appear once .
Please find and return the number that only appears once .
The solution you design must meet O(log n) Time complexity and O(1) Spatial complexity .
class Solution {
public int singleNonDuplicate(int[] nums) {
int l=0;
int r=nums.length-1;
while(l<r){
int mid=l+(r-l)/2;
// If mid The front is orderly , There must be an even subscript equal to the value of its next odd subscript , Take this as a condition for narrowing the scope
if((mid%2==0&&nums[mid]==nums[mid+1])||(mid%2==1&&nums[mid]==nums[mid-1])){
l=mid+1;
}
else if((mid==0||nums[mid]!=nums[mid-1])&&(mid==nums.length-1||nums[mid]!=nums[mid+1])){
return nums[mid];
}
else{
r=mid-1;
}
}
return nums[l];
}
}
35. Search insert location
Given a sort array and a target value , Find the target value in the array , And return its index . If the target value does not exist in the array , Return to where it will be inserted in sequence .
Please use a time complexity of O(log n) The algorithm of .
class Solution {
public int searchInsert(int[] nums, int target) {
int l=0;
int r=nums.length-1;
while(l<r){
int mid=l+(r-l)/2;
if(target<nums[mid]){
r=mid-1;
}
else if(target==nums[mid]){
return mid;
}
else{
l=mid+1;
}
}
if(nums[l]>=target)
return l;
else
return l+1;
}
}
ending
Title source : Power button (LeetCode) link :https://leetcode-cn.com/problems
️ Focus on the author , Take you to brush the questions , Learn the most commonly used algorithm skills from simple algorithm problems ( Winter vacation every day )
️ Pay attention to the author's questions —— Simple to advanced , Let you unknowingly become a ruthless problem brushing machine , If you have any questions, please send a private letter
边栏推荐
- FileZilla Client下载安装
- Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
- Introduction to mongodb
- 小程序获取用户头像和昵称
- Recursive use and multi-dimensional array object to one-dimensional array object
- Hutool dynamically adds scheduled tasks
- MySQL MAC download and installation tutorial
- Téléchargement et installation du client Filezilla
- Docker install and start MySQL service
- 2022年已过半,得抓紧
猜你喜欢

FileZilla Client下载安装

navicat 导出数据库的表结构

Positioning (relative positioning, absolute positioning, fixed positioning, Z-index) 2022-2-11

MongoDB簡介

How does the pytorch project run?

Ansible简介【暂未完成(半成品)】

Error in compiled file: error: unmapped character encoding GBK

Is pytorch difficult to learn? How to learn pytorch well?

Pytorch multi card distributed training distributeddataparallel usage

IPv6 transition technology-6to4 manual tunnel configuration experiment -- Kuige of Shangwen network
随机推荐
【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
Pytoch lightweight visualization tool wandb (local)
Ansible简介【暂未完成(半成品)】
动态规划:最长公共子串和最长公共子序列
How to move towards IPv6: IPv6 Transition Technology - Shangwen network quigo
TCP, the heavyweight guest in tcp/ip model -- Kuige of Shangwen network
Message queue addition failure
Debug: CD cannot be used in kaggle
numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences
ffmpeg录制屏幕和截屏
Numpy warning visibledeprecationwarning: creating an ndarray from ragged needed sequences
Makefile demo
MongoDB简介
Table structure of Navicat export database
Introduction à mongodb
Don't use the new Dede collection without the updated Dede plug-in
403 error displayed when vs cloning
[mathematical logic] propositional logic (propositional logic reasoning | formal structure of reasoning | inference law | additional law | simplification law | hypothetical reasoning | refusal | disju
FileZilla Client下载安装
Open Visual Studio 2010 hangs when opening a SQL file sql file