当前位置:网站首页>[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
边栏推荐
- C language hashtable/hashset library summary
- PHP generates PDF tcpdf
- Pytoch lightweight visualization tool wandb (local)
- Introduction to mongodb
- Using jasmine to monitor constructors - spying on a constructor using Jasmine
- Ffmpeg one / more pictures synthetic video
- node,npm以及yarn下载安装
- 记一次 .NET 差旅管理后台 CPU 爆高分析
- Intercept string fixed length to array
- Compare float with 0
猜你喜欢
随机推荐
Shardingsphere dynamic data source
How to download pytorch? Where can I download pytorch?
[mathematical logic] propositional logic (propositional and connective review | propositional formula | connective priority | truth table satisfiable contradiction tautology)
pytorch怎么下载?pytorch在哪里下载?
What is pytorch? Is pytorch a software?
Download and install captura and configure ffmpeg in captura
Mongodb installation & Deployment
动态规划:最长公共子串和最长公共子序列
Ansible introduction [unfinished (semi-finished products)]
Mysql Mac版下载安装教程
Tidal characteristics of the Bohai Sea and the Yellow Sea
Debug: CD cannot be used in kaggle
CEPH Shangwen network xUP Nange that releases the power of data
MongoDB簡介
Table structure of Navicat export database
【学习笔记】seckill-秒杀项目--(11)项目总结
简易版 微信小程序开发之for指令、上传图片及展示效果优化
Applet (continuous update)
Positioning (relative positioning, absolute positioning, fixed positioning, Z-index) 2022-2-11
Dynamic programming: Longest palindrome substring and subsequence