码迷,mamicode.com
首页 > 其他好文 > 详细

Find Minimum in Rotated Sorted Array

时间:2015-02-02 23:13:46      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:leetcode   数组   java   算法   

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43416613



Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

You may assume no duplicate exists in the array.


思路:

(1)题意为给定一个已经排好序的整形数组,数组的前m(m>0)个元素被移到数组的后面形成新的数组,求新数组中的最小元素。

(2)该题考查的是数组遍历问题。该题同样比较简单,解法一属于“简单暴力”型,直接使用类库中的方法进行排序,取得最小元素(下方算法附有Arrays.sort()中的排序算法实现,感兴趣可以看看,感觉写的好复杂);解法二属于“一般解法”,通过从前、从后遍历求得最小值,从后往前的效率要高于从前往后;解法三应该是属于"高效性",不过这里尚未给出实现,猜测的思路是通过类似“二分查找”的方式来寻求最小值,感兴趣的可以研究下。

(3)希望本文对你有所帮助。



算法代码实现如下:

	/**
	 * @author liqq
	 * 解法一:简单暴力
	 */
	public int findMin(int[] num) {
		if (num == null || num.length == 0)
			return 0;
		Arrays.sort(num);
		return num[0];
	}
/**
	 * 
	 * @author liqq 附加Arrays.sort() 源码 以供参考
	 */
	public int findMin(int[] num) {
		if (num == null || num.length == 0)
			return 0;
		sort(num, 0, num.length);
		return num[0];
	}

	private static void sort(int x[], int off, int len) {
		// Insertion sort on smallest arrays
		if (len < 7) {
			for (int i = off; i < len + off; i++)
				for (int j = i; j > off && x[j - 1] > x[j]; j--)
					swap(x, j, j - 1);
			return;
		}

		// Choose a partition element, v
		int m = off + (len >> 1); // Small arrays, middle element
		if (len > 7) {
			int l = off;
			int n = off + len - 1;
			if (len > 40) { // Big arrays, pseudomedian of 9
				int s = len / 8;
				l = med3(x, l, l + s, l + 2 * s);
				m = med3(x, m - s, m, m + s);
				n = med3(x, n - 2 * s, n - s, n);
			}
			m = med3(x, l, m, n); // Mid-size, med of 3
		}
		int v = x[m];

		// Establish Invariant: v* (<v)* (>v)* v*
		int a = off, b = a, c = off + len - 1, d = c;
		while (true) {
			while (b <= c && x[b] <= v) {
				if (x[b] == v)
					swap(x, a++, b);
				b++;
			}
			while (c >= b && x[c] >= v) {
				if (x[c] == v)
					swap(x, c, d--);
				c--;
			}
			if (b > c)
				break;
			swap(x, b++, c--);
		}

		// Swap partition elements back to middle
		int s, n = off + len;
		s = Math.min(a - off, b - a);
		vecswap(x, off, b - s, s);
		s = Math.min(d - c, n - d - 1);
		vecswap(x, b, n - s, s);

		// Recursively sort non-partition-elements
		if ((s = b - a) > 1)
			sort(x, off, s);
		if ((s = d - c) > 1)
			sort(x, n - s, s);
	}

	private static void swap(int x[], int a, int b) {
		int t = x[a];
		x[a] = x[b];
		x[b] = t;
	}

	private static int med3(int x[], int a, int b, int c) {
		return (x[a] < x[b] ? (x[b] < x[c] ? b : x[a] < x[c] ? c : a)
				: (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
	}

	private static void vecswap(int x[], int a, int b, int n) {
		for (int i = 0; i < n; i++, a++, b++)
			swap(x, a, b);
	}
	/**
	 * @author liqq 
	 * 解法二 分别从前、从后遍历 从后遍历效率稍微高一些
	 */
	public int findMinFromHead(int[] num) {
		if (num == null || num.length == 0)
			return 0;

		int len = num.length;
		int leftHalfMin = num[0];
		for (int i = 1; i < len; i++) {
			if (leftHalfMin >= num[i]) {
				leftHalfMin = num[i];
			}
		}
		return leftHalfMin;
	}

	public int findMinFromEnd(int[] num) {
		if (num == null || num.length == 0)
			return 0;
		if (num.length == 1)
			return num[0];

		int len = num.length;
		int min = num[len - 1];
		for (int i = len - 2; i >= 0; i--) {
			if (min <= num[i]) {
				return min;
			} else {
				min = num[i];
				if (i == 0) {
					return min;
				}
			}
		}
		return -1;
	}

Find Minimum in Rotated Sorted Array

标签:leetcode   数组   java   算法   

原文地址:http://blog.csdn.net/pistolove/article/details/43416613

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!