Skip to content

Commit 284a61e

Browse files
committed
minInRotatedArray
1 parent c6cb6f0 commit 284a61e

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package problem07;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* 两个栈实现一个队列
7+
* @author bjyangrubing
8+
*
9+
*/
10+
public class QueueWithTwoStack
11+
{
12+
/*用两个栈实现一个队列,完成两个函数appendTail和deletedHead,分别是在队列尾部插入节点和
13+
在队列头部删除节点的功能*/
14+
private Stack<String> stack1 = new Stack<String>();
15+
private Stack<String> stack2 = new Stack<String>();
16+
17+
public void appendTail(String elem)
18+
{
19+
stack1.push(elem);
20+
}
21+
22+
public String deletedHead() throws Exception
23+
{
24+
while (!stack1.isEmpty())//如果栈1不为空,则把栈1的元素全部push到栈2
25+
{
26+
stack2.push(stack1.pop());
27+
}
28+
29+
if (stack2.size() == 0)
30+
throw new Exception("queue is empty");
31+
String head = stack2.pop();//从栈2里面出栈
32+
return head;
33+
}
34+
35+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package problem08;
2+
3+
/**
4+
* 旋转数组嘴小的数字,如{3,4,5,1,2}为{1,2,3,4,5}的一个旋转
5+
*
6+
* @author bjyangrubing
7+
*
8+
*/
9+
public class MinNumberInRotatedArray
10+
{
11+
public int min(int[] numbers, int length) throws Exception
12+
{
13+
if (numbers == null || length <= 0)//边界条件判断
14+
throw new Exception("Invalid parameters.");
15+
int index1 = 0;
16+
int index2 = length - 1;
17+
int indexMid = index1;//特殊情况处理,如果数组为{1,2,3,4,5},则直接返回1
18+
while (numbers[index1] >= numbers[index2])
19+
{
20+
if (index2 - index1 == 1)
21+
{
22+
indexMid = index2;//循环终止条件是index2-index1 = 1
23+
break;
24+
}
25+
26+
indexMid = (index1 + index2) / 2;
27+
//中间的元素,要不位于前面的递增序列(大于numbers[index1])
28+
//要不位于后面的递增序列(小于numbers[index2])。
29+
//注意特殊情况,如果numbers[index2] = numbers[index1] = numbers[indexMid]
30+
//如{1,0,1,1,1}和{1,1,1,0,1}这种情况时无法判断最小值是在前面的递增子序列,还是后面的递增子序列
31+
//此时可以采用顺序查找
32+
33+
if (numbers[index1] == numbers[index2] && numbers[index1] == numbers[indexMid])
34+
{
35+
return minInOrder(numbers, index1, index2);
36+
}
37+
38+
if (numbers[indexMid] >= numbers[index1])
39+
{
40+
index1 = indexMid;
41+
}
42+
else if (numbers[indexMid] <= numbers[index2])
43+
{
44+
index2 = indexMid;
45+
}
46+
}
47+
48+
return numbers[indexMid];
49+
}
50+
51+
int minInOrder(int[] numbers, int index1, int index2)
52+
{
53+
int result = numbers[index1];
54+
for (int i = index1 + 1; i <= index2; ++i)
55+
{
56+
if (result > numbers[i])
57+
{
58+
result = numbers[i];
59+
}
60+
}
61+
return result;
62+
}
63+
64+
public static void main(String[] args) throws Exception
65+
{
66+
int[] numbers =
67+
{ 1, 0, 1, 1, 1 };
68+
MinNumberInRotatedArray minNumberInRotatedArray = new MinNumberInRotatedArray();
69+
int min = minNumberInRotatedArray.min(numbers, numbers.length);
70+
System.out.println(min);
71+
}
72+
73+
}
683 KB
Loading
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package problem07;
2+
3+
import org.junit.Test;
4+
5+
import junit.framework.Assert;
6+
7+
public class QueueWithTwoStackTest
8+
{
9+
@Test
10+
public void test() throws Exception
11+
{
12+
QueueWithTwoStack queue = new QueueWithTwoStack();
13+
queue.appendTail("1");
14+
queue.appendTail("2");
15+
queue.appendTail("3");
16+
17+
Assert.assertEquals("1", queue.deletedHead());
18+
Assert.assertEquals("2", queue.deletedHead());
19+
Assert.assertEquals("3", queue.deletedHead());
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package problem08;
2+
3+
import org.junit.Test;
4+
5+
import junit.framework.Assert;
6+
7+
public class MinNumberInRotatedArrayTest
8+
{
9+
@Test
10+
public void test() throws Exception
11+
{
12+
MinNumberInRotatedArray min = new MinNumberInRotatedArray();
13+
Assert.assertEquals(0, min.min(new int[]
14+
{ 1, 1, 1, 0, 1 }, 5));
15+
Assert.assertEquals(0, min.min(new int[]
16+
{ 1, 0, 1, 1, 1 }, 5));
17+
Assert.assertEquals(1, min.min(new int[]
18+
{ 1, 2, 3, 4, 5 }, 5));
19+
Assert.assertEquals(1, min.min(new int[]
20+
{ 4, 5, 6, 1, 2, 3 }, 6));
21+
}
22+
}

0 commit comments

Comments
 (0)