Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the interview questions of finding and sorting in C++

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to solve the interview questions found and sorted in C++. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Title:

The minimum number of rotated array

Move the first elements of an array to the end of the array, enter a rotation of an incrementally sorted array, and output the smallest element of the rotation number.

For example, {2pyrrine 3pyrr4 0pr 1} is the rotation of {0pr 1pm 2je 3p4}, and the minimum value of this array is 0.

Program 1.0

The failed program, traversing from the beginning, has a time complexity of O (N). This idea does not make use of the characteristics of the input rotation array and is casually implemented but failed.

Int MinNum (int* array, int length) {int cur = array [0]; for (int I = 1; I

< length; i++) { if (array[i]0); int left = 0; int right = length - 1; int mid = left;//初始化为0,这样若旋转了0个元素则直接返回 while (array[left] >

= array [right]) / / the array is rotated {if (right-left = = 1) {mid = right; break;} mid = (left + right) / 2 If (array [mid] > = array [left]) {left = mid;} else if (array [mid] 0); int left = 0; int right = length-1; int mid = left While (array [left] > = array [right]) {if (right-left = = 1) {mid = right; break;} mid = (left + right) / 2 If (array [left] = = array [right] & & array [left] = = array [mid]) {return MinInOrder (array, left, right);} if (array [mid] > = array [left]) {left = mid } else if (array [mid]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report