当前位置: 首页 > news >正文

LeetCode -- Search in Rotated Sorted Array II

题目描述:


Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?


Would this affect the run-time complexity? How and why?


Write a function to determine if a given target is in the array.


思路:


这个题目与Search in Rotated Sorted Array基本是一样的,在旋转过的排序好的数组中进行二分查找,区别只是在于这个题目的输入可能包含重复元素。
本题解法基本与Search in Rotated Sorted Array I相同,区别只是需要跳过重复元素。
这里是Search in Rotated Sorted Array的解法:
<link>


而对于本题,需要在得到中间索引m后跳过左右重复元素,并重新计算m:
while(l < m && nums[l] == nums[m]){
    l++;
    }
    while(r > m && nums[r] == nums[m]){
    r--;
    }
m = (l + r) / 2;






实现代码:


public class Solution {
    public bool Search(int[] nums, int target) {
        if(nums.Length == 0)
    	{
    		return false;
    	}
    	
    	if(nums.Length == 1)
    	{
    		return nums[0] == target ;
    	}
    	
    	var l = 0;
    	var r = nums.Length - 1;
    	
    	while(l < r - 1){
    	var m = (l + r) / 2;
    	while(l < m && nums[l] == nums[m]){
    		l++;
    	}
    	while(r > m && nums[r] == nums[m]){
    		r--;
    	}
	    m = (l + r) / 2;
	    
    	if(nums[m] == target || nums[r] == target || nums[l] == target){
    		return true;
    	}
    	
    	if(nums[m] > nums[l] && nums[m] > nums[r]){
    		if(target < nums[r]){
    			l = m;
    		}
    		else {
    			if(target > nums[m]){
    				l = m;	
    			}
    			else{
    				r = m;
    			}
    		}
    	}
    	else if(nums[m] < nums[l] && nums[m] < nums[r]){
    		if(target > nums[l]){
    			r = m;
    		}
    		else{
    			if(target > nums[m]){
    				l = m;	
    			}
    			else{
    				r = m;
    			}
    		}
    		
    	}
    	else if(nums[m] > nums[l] && nums[m] < nums[r]){
    		if(target > nums[m]){
    			l = m;
    		}
    		else {
    			r = m;
    		}
    	}
    	}
    	
    	if(target == nums[l] || target == nums[r]){
    		return true;
    	}
    	
    	return false;
    }
}


相关文章:

  • 小议移动Widget
  • LeetCode -- Search in Rotated Sorted Array
  • LeetCode -- Binary Tree Postorder Traversal
  • 《3G移动增值业务的运营、定制与开发——BREW进阶与精通》开始连载
  • LeetCode -- Course Schedule
  • 七、基本I/O接口电路设计实验
  • LeetCode -- Intersection of Two Linked Lists
  • 红帽的top命令不正确
  • LeetCode -- Minimum Window Substring
  • 70后所面临的软件技术学习困境
  • LeetCode -- Remove Duplicates from Sorted List II
  • 金旭亮博客之“计算机学习、教育与专业指导”主页
  • LeetCode -- Number of Islands
  • LeetCode -- Repeated DNA Sequences
  • oracle的V$log一些小用处
  • Angular Elements 及其运作原理
  • CSS相对定位
  • Effective Java 笔记(一)
  • input实现文字超出省略号功能
  • JavaScript-Array类型
  • Javascript弹出层-初探
  • java取消线程实例
  • Linux编程学习笔记 | Linux IO学习[1] - 文件IO
  • Phpstorm怎样批量删除空行?
  • 爱情 北京女病人
  • 前端面试之CSS3新特性
  • 项目实战-Api的解决方案
  • 一些关于Rust在2019年的思考
  • 用quicker-worker.js轻松跑一个大数据遍历
  • 优秀架构师必须掌握的架构思维
  • 正则学习笔记
  • hi-nginx-1.3.4编译安装
  • 阿里云ACE认证学习知识点梳理
  • #图像处理
  • #中的引用型是什么意识_Java中四种引用有什么区别以及应用场景
  • (+4)2.2UML建模图
  • (1)(1.9) MSP (version 4.2)
  • (a /b)*c的值
  • (八)Spring源码解析:Spring MVC
  • (附源码)springboot电竞专题网站 毕业设计 641314
  • (附源码)springboot家庭财务分析系统 毕业设计641323
  • (转)linux 命令大全
  • (转)winform之ListView
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • ***详解账号泄露:全球约1亿用户已泄露
  • .Net - 类的介绍
  • .NET 4 并行(多核)“.NET研究”编程系列之二 从Task开始
  • .net core 源码_ASP.NET Core之Identity源码学习
  • .net开源工作流引擎ccflow表单数据返回值Pop分组模式和表格模式对比
  • .NET框架设计—常被忽视的C#设计技巧
  • [20160807][系统设计的三次迭代]
  • [autojs]autojs开关按钮的简单使用
  • [BZOJ1053][HAOI2007]反素数ant
  • [C++] new和delete
  • [C++]priority_queue的介绍及模拟实现