本文是在学习中的总结,欢迎转载但请注明出处: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;
	}

Leetcode_154_Find Minimum in Rotated Sorted Array的更多相关文章

  1. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  2. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  3. 【leetcode】Find Minimum in Rotated Sorted Array I&&II

    题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...

  4. Leetcode | Find Minimum in Rotated Sorted Array I && II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  5. LeetCode Find Minimum in Rotated Sorted Array II

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...

  6. LeetCode Find Minimum in Rotated Sorted Array

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...

  7. Find Minimum in Rotated Sorted Array II

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  8. leetcode 154. Find Minimum in Rotated Sorted Array II --------- java

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  9. 154 Find Minimum in Rotated Sorted Array II

    多写限制条件可以加快调试速度. ======= Follow up for "Find Minimum in Rotated Sorted Array":What if dupli ...

随机推荐

  1. Lucene总结

    数据的分类 结构化数据:有固定类型或者有固定长度的数据 例如:数据库中的数据(mysql,oracle等), 元数据(就是windows中的数据) 结构化数据搜索方法: 数据库中数据通过sql语句可以 ...

  2. 临时关闭Mac SIP系统完整性保护机制

    # 修正更新 [2016-12-27] 晚上给我笔记本安装的时候,使用user权限安装成功,mac最后是关闭sip才安装成功. $ pip install -r requirements.txt -- ...

  3. Ajax原理学习

    一.AJAX 简介 AJAX即"Asynchronous Javascript And XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. A ...

  4. CentOS7.2安装Weblogic12c出现的问题

    Weblogic12c安装到步骤:Prerequisite  Checks 时,会进行操作系统版本的校验,即checking  operating  system  certification. 此处 ...

  5. iter 函数另类用法

    它可以很简单地构造一个无限迭代器: ): print(i) #将无限打印出0 原来,如果iter有第二个参数,那么第一个参数必须是一个参数可以省略的可调用对象.int函数符合这种要求. 迭代什么时候停 ...

  6. Dynamics CRM2016 Web API之Expand related entities & $ref & $count

    本篇介绍两个关于1:N关系中通过主实体取关联子实体的api,这两个api会经常被用到而且比原来的odata方式更加方便,之前如果我们要取主实体下所有的关联实体的记录都是通过Retrieve Multi ...

  7. Scikit-learn:模型选择Model selection

    http://blog.csdn.net/pipisorry/article/details/52250983 选择合适的estimator 通常机器学习最难的一部分是选择合适的estimator,不 ...

  8. Java并发框架——什么是AQS框架

    什么是AQS框架 1995年sun公司发布了第一个java语言版本,可以说从jdk1.1到jdk1.4期间java的使用主要是在移动应用和中小型企业应用中,在此类领域中基本不用设计大型并发场景,当然也 ...

  9. JAVA面向对象-----instanceof 关键字

    instanceof 关键字 1:快速演示instanceof Person p=new Person(); System.out.println( p instanceof Person); 2:i ...

  10. 4.2、Android Studio压缩你的代码和资源

    为了让你的APK文件尽可能的小,你需要在构建的时候开启压缩来移除无用的代码和资源. 代码压缩可在ProGuard中使用,可以检测和清除无用的类,变量,方法和属性,甚至包括你引用的库.ProGuard同 ...