Code Monkey home page Code Monkey logo

Comments (2)

bazinga-web avatar bazinga-web commented on August 13, 2024

解题思路:

  1. 如果数组长度为1,返回唯一的数
  2. 定义两个指针分别指向数组的开头及结尾
  3. 检查数组是否翻转,如果没有返回最开始的那个数
  4. 当left小于right时,取中间作为mid进行二分查找
    当mid右边值小于mid时返回nums[mid+1],
    当mid左边值大于mid时返回nums[mid]
  5. 上述两种都不符合时,如果left所在数小于mid所在数,则
    left右移mid+1位置,否则将right左移至mid-1位置
/**
 * @param {number[]} nums
 * @return {number}
 */
var findMin = function (nums) {
  if (nums.length < 2) {
    return nums[0];
  }
  let left = 0;
  let right = nums.length - 1;

  if (nums[right] > nums[0]) {
    return nums[0];
  }

  while (left < right) {
    let mid = Math.floor(left + (right - left) / 2);

    if (nums[mid] > nums[mid + 1]) {
      return nums[mid + 1];
    }

    if (nums[mid - 1] > nums[mid]) {
      return nums[mid];
    }

    if (nums[left] < nums[mid]) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
};

from like-algorithms.

Ray-56 avatar Ray-56 commented on August 13, 2024
/**
 * @param {number[]} nums
 * @return {number}
 */
var findMin = function (nums) {
	const len = nums.length;
	if (len == 1) return nums[0];

	let left = 0;
	let right = len - 1;
	// 左边小于右边,表示未旋转,直接将最左返回
	if (nums[left] < nums[right]) return nums[left];

	while (left < right) {
		const mid = Math.floor((left + right) / 2);

		if (nums[mid] > nums[mid + 1]) return nums[mid + 1];

		if (nums[mid] < nums[mid - 1]) return nums[mid];

		if (nums[left] < nums[mid]) {
			left = mid + 1;
		} else {
			right = mid - 1;
		}
	}
};

from like-algorithms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.