面试题8:旋转数组的最小数字

问题说明:

1.原数组是排序的,将数组开始几位搬到末尾,例如{3,4,5,1,2}是{1,2,3,4,5}的一个旋转;

2.遍历一遍时间复杂度为O(n);

3.充分利用排序信息,二分思想,时间复杂度为O(logn)

public static int getMinValueIndex(int[] rotate){
		int len = 0;
		if(rotate == null || (len = rotate.length) == 0){
			throw new IllegalArgumentException();
		}
		// only one element so return 0
		if(len <= 1) return 0;
		
		// len >= 2 && rotate[index1] < rotate[index2]
		// now isSorted indicates that (int[] rotate) does not rotate  any element
		int index1 = 0;// this always points first increased array
		int index2 = len - 1;// this always points second increased array  
		boolean isSorted = (rotate[index1] < rotate[index2]);
		if(isSorted) return index1;

		// len >= 2 && rotate[index1] >= rotate[index2]
		int midIndex = 0;
		while((index1 < index2) && ((index2 - index1) != 1)){
			midIndex = (index1 + index2) >> 1;
			boolean isNotJudged = ((rotate[index1] == rotate[index2]) && (rotate[index1] == rotate[midIndex]));
			
			if(isNotJudged) return MinValueOrderIndexOf(rotate, index1,index2);
			
			// rotate[index1] > rotate[index2]
			// if isInLeft is false ,so at least 
			// rotate[midIndex] >= rotate[index1] because midIndex >= index1
			// so isInRight does not need because is opposite to isInLeft 
			boolean isInLeft = (rotate[midIndex] <= rotate[index2]);
			boolean isInRight = (rotate[midIndex] >= rotate[index1]);
			if(isInLeft){
				index2 = midIndex; 
			}
			if(isInRight){
				index1 = midIndex;
			}
		}
		//assert index2 -index1 == 1;
		return index2;	
	}
	public static int getMinValueOrderIndex(int[] rotate,int fromIndex,int toIndex){
		
		int minIndex = fromIndex;
		for(int i = fromIndex; i <= toIndex; i++){
			if(rotate[minIndex] > rotate[i]){
				minIndex = i;
			}
		}
		return minIndex;
	}

内容概要:本文详细介绍了C#编程语言的学习资源,涵盖了从基础到高级的不同层次。首先,对于初学者,推荐了多本经典书籍如《C# 8.0 and .NET Core 3.0 - Modern Cross-Platform Development》、《C# 9.0 in a Nutshell》等,以及在线课程和视频教程,帮助理解C#的基本语法、面向对象编程等概念。接着,针对进阶开发者,提供了深入研究C#特性的书籍和课程,如《C# 9 and .NET 5 – Modern Cross-Platform Development》,并推荐了一些实战项目来巩固所学知识。最后,为高级开发者准备了深入探讨C#内部机制和技术细节的书籍,如《CLR via C#》,以及高级课程和开源项目。此外,还介绍了常用的开发工具(如Visual Studio、Visual Studio Code)、框架(如ASP.NET Core、Entity Framework Core),并列出了活跃的开发社区和论坛供交流学习。; 适合人群:所有对C#编程感兴趣的开发者,无论是初学者还是有经验的专业人士。; 使用场景及目标:①初学者可以通过基础资源快速入门C#编程,掌握基本语法和面向对象编程;②进阶开发者可以利用提供的资源深入理解C#特性,如并发编程、异步编程等;③高级开发者则可以通过高级资源和实战项目提升技术水平,解决复杂问题。; 其他说明:文中提到的资源不仅限于理论学习,还包括大量实战项目和开源项目,鼓励开发者在实践中不断积累经验。同时,活跃的社区和论坛也为开发者提供了良好的交流平台,有助于解决问题和获取最新资讯。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值