Skip to content

Commit 00d943a

Browse files
committed
add find min in rotated array ii
1 parent ee0f2a7 commit 00d943a

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

find-minimum-in-rotated-sorted-array-ii/README.md

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class Solution {
2+
public int findMin(int[] num) {
3+
if(num.length == 1) return num[0];
4+
if(num.length == 2) return Math.min(num[0], num[1]);
5+
6+
int s = 0;
7+
int e = num.length;
8+
9+
int m = (s + e) / 2;
10+
11+
// bad case
12+
if (num[s] == num[m] && num[m] == num[e - 1]){
13+
return Math.min(num[s], findMin(Arrays.copyOfRange(num, s + 1, e)));
14+
}
15+
16+
// s < m < e
17+
if ( num[s] <= num[m] && num[m] <= num[e - 1]){
18+
return num[s];
19+
}
20+
21+
// s < m > e
22+
if ( num[s] <= num[m] && num[m] >= num[e - 1]){
23+
return findMin(Arrays.copyOfRange(num, m, e));
24+
}
25+
26+
// s > m < e
27+
return findMin(Arrays.copyOfRange(num, s, m + 1));
28+
29+
}
30+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
layout: solution
3+
title: Find Minimum in Rotated Sorted Array II
4+
date: 2014-10-21 13:20:36+08:00
5+
---
6+
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
7+
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_root/' }} %}
8+
{% include {{leetcode_readme}} %}

0 commit comments

Comments
 (0)