zoukankan      html  css  js  c++  java
  • 旋转数组的最小数字(python/c++)

    题目描述

    把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
    # -*- coding:utf-8 -*-
    class Solution:
        def minNumberInRotateArray(self, rotateArray):
            #由于数组(Array)可以分为两个部分有序的子数组,在排序的数组中可以考虑利用二分查找法实现。
            #用两个指针分别指向数组的第一个元素(low)和最后一个元素(high),
            #接着可以找到数组中间的元素(mid = (low+high)/2),
            #如果中间元素位于前面的递增子数组中,则arr[mid] >= arr[low],
            #此时数组中最小的元素应该位于该中间元素的后面。我们可以用指针low指向该中间元素low = mid,缩小搜索范围。
            #移动之后的第二个指针high仍然位于后面的递增子数组中。
            #如果中间元素位于后面的递增子数组,那么arr[mid] <= arr[high],
            #此时输出中最小的元素应该位于该中间元素的前面,我们可以用指针high指向该中间元素high = mid。
             #low总是指向前面递增子数组的元素,而high总是指向后面递增子数组的元素。最终low指向前面子数组的最后一个元素,
             #而high指向后面子数组的第一个元素,即它们会指向两个相邻的元素,而此时high指向的刚好是最小的元素,
            # write code here
            n = len(rotateArray)
            if n == 0: #空数组,直接返回0
                return 0
            low = 0 #初始化元素长度
            high = n - 1
            #如果把第0个元素移到最后,即数组有序,返回第一个元素即可
            if rotateArray[low] < rotateArray[high]: 
                return rotateArray[low]
            else:
                #两个指针最终会指向两个相邻的元素,第二个指针指向最小的元素
                while (high - low) > 1: 
                    mid = (low + high)/2
                    #如果中间元素位于前面的递增子数组中
                    if rotateArray[low] <= rotateArray[mid]: 
                        low = mid 
                    #如果中间元素位于后面的递增子数组中
                    elif rotateArray[mid] <= rotateArray[high]: 
                        high = mid
                    
                    #当首元素等于尾元素等于中间值时,只能在这个区域顺序查找,如[1,1,1,0,0,0,1,1]。
                    elif rotateArray[low] == rotateArray[high] and rotateArray[low] == rotateArray[mid]:
                        for i in range(n):
                            if rotateArray[i] < rotateArray[0]:
                                result = rotateArray[i]
                                high = i
                result = rotateArray[high]                
                return result
    

     

    #include<iostream>
    #include<vector>
    using namespace std;
    class Solution 
    {
    public:
    	int minNumberInRotateArray(vector<int> rotateArray)
    	{
    		int size = rotateArray.size();
    		int i;
    		if (size == 0)
    			return 0;
    		if (size == 1)
    			return rotateArray[0];
    		for (i = 1; i<size; i++)
    		{
    			if (rotateArray[i]<rotateArray[i - 1])
    				return rotateArray[i];
    		}
    		return rotateArray[0];
    	}
    };
    int main()
    {
    	vector<int>vec;
    	int a[1000];
    	int i = 0;
    	int x;
    	while (cin >> a[i])
    	{
    		x = cin.get();
    		if (x == '
    ')
    			break;
    		vec.push_back(a[i]);
    		++i;
    	}
        vector<int>result(Solution().minNumberInRotateArray(vec));
    	for (int j = 0; j < result.size(); j++){
    		cout << result[j]<<" ";
    		cout << endl;
    	}
    	system("pause");
    	return 0;
    }
    

      

     

  • 相关阅读:
    电脑内存九大常见问题解决方案
    学会快速装系统 图解硬盘分区软件Norton Ghost使用
    XP和Win 7双系统安装说明和注意事项
    Linux gzip、gunzip
    kylin的状态栏(启动器)改成ubuntu之前的样子
    chrome安装HTTP测试扩展
    程序员重要的事情
    PD003-NET通用后台系统
    通用后台管理系统(ExtJS 4.2 + Spring MVC 3.2 + Hibernate)
    UNICODE与ANSI的区别
  • 原文地址:https://www.cnblogs.com/277223178dudu/p/10432237.html
Copyright © 2011-2022 走看看