Do you consider a group of numbers from largest to smallest like 2 1 0 7 6 5 4 . It's my O(n) accepted answer in C++.


  • 0
    J
    int findMin(vector<int> &num) {
                vector<int>::iterator iter;
    		int first,min;
    		if(num.size()==1)
    			return *num.begin();
    		else if(*num.begin()>*--num.end())
    		{
    			first=*num.begin();
    			for(iter=++num.begin();iter!=num.end();++iter)
    			{
    				if(*iter<first)
    				{
    					min=*iter;
    					break;
    				}
    			}
    		}
    		else
    		{
    			first=*num.begin();
    			for(iter=++num.begin();iter!=num.end();++iter)
    			{
    				if(*iter>first)
    				{
    					min=*(--iter);
    					break;
    				}
    			}
    		}
    		return min;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.