Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Java looks for the minimum value in the rotated sort array

Shulou Source: shulou.com Published: 2022-06-01 01:39:58 10月16日 Update

Editor to share with you java looking for the minimum value in the rotation sort array, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Suppose that an array sorted in ascending order is rotated at a pre-unknown point.

(for example, an array [0meme1reparent2magor455pyrr7] may become [4pyrrine 5rec 6pr 7jor0rec 1reie2]).

Please find the smallest element.

You can assume that there are no repeating elements in the array.

Example 1:

Input: [3, 4, 5, 5, 1, 2]

Output: 1

Example 2:

Input: [4, 5, 6, 7, 0, 0, 1, 2]

Output: 0

Answer:

1public int findMin (int [] nums) {

2 int start = 0, end = nums.length-1

3 while (start

< end) { 4 if (nums[start] < nums[end]) 5 return nums[start]; 6 int mid = (start + end) / 2; 7 if (nums[mid] >

= nums [start]) {

8 start = mid + 1

9} else {

10 end = mid

11}

12}

13 return nums [start]

14}

Parsing:

Dichotomy search, it is easy to understand, but the array has been rotated, so it can not be fully applied, we can intercept, find in the subarray

What are the advantages of Java. Simple, as long as understand the basic concepts, you can write applications suitable for a variety of situations; 2. Object oriented; 3. Distributed, Java is a network-oriented language; 4. Robust, java provides automatic garbage collection for memory management to prevent programmers from making mistakes when managing memory. ; 5. Security, Java used in the network and distributed environment must prevent the invasion of viruses. 6. Architecturally neutral, as long as the Java runtime system is installed, it can run on any processor. 7. Portability, Java can be easily ported to different machines on the network. 8. Interpretation execution, Java interpreter directly interprets and executes Java bytecode.

These are all the contents of the article "java looking for minimum values in a rotated sorted array". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Array minimum sort article network interpretation element memory content example program migration management input output run different not much dichotomy advantages Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information OPPO Reno Redmi Apple Shulou Technology