LeetCode上这两道题主要是使用二分搜索解决,是二分搜索算法的一个应用,根据条件每次舍弃一半,保留一半。

首先第一题: FindMinimuminRotatedSorteArray(时间复杂度为二分算法的时间复杂度O(logN))

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Leetcode
{
public class FindMinimuminRotatedSorteArray
{
public int FindMin(int[] nums)
{
int length = nums.Length;
int beg = ;
int end = length - ;//闭区间,注意-1
int mid = ;
while (beg < end)
{
if (nums[beg] < nums[end])//如果成立代表数组是有序的,直接退出循环
break;
mid = (beg + end) / ;
if (nums[beg] > nums[mid])//这里面将两个元素的特殊情况包括在内
{
end = mid;
}
else
{
beg = mid + ;
}
}
return nums[beg];
}
} /*参考博客地址:http://blog.csdn.net/loverooney/article/details/40921751*/
}

第二题由于存在重复元素,无法判定每次舍弃哪一半,故需要一次一次的判断,时间复杂度为O(n)

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Leetcode
{
class FindMinimuminRotatedSorteArray2
{
public int FindMin(int[] nums)
{
int length = nums.Length;
int beg = ;
int end = length - ;
int mid = ;
while (beg < end)
{
if (nums[beg] < nums[end])
break;
mid = (beg + end) / ;
if (nums[beg] > nums[mid])
{
end = mid;//闭区间(也有可能mid是最小值)
}
else if (nums[mid] > nums[end])
{
beg = mid + ;//开区间,因此+1(mid不可能是最小值)
}
else//这种情况是nums[beg]=nums[mid]=nums[end],因为此时nums[end]<nums[beg]
{
beg++;
}
}
return nums[beg];
}
}
}

LeetCode FindMinimuminRotatedSorteArray &&FindMinimuminRotatedSorteArray2的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. 找回丢失的SQL Server性能计数器

    There was one time when I was delivering a Service using a tool that gathers performance data throug ...

  2. 【24】若所有参数皆需类型转换,请为此采用non-members函数

    1.令class支持隐式类型转换,往往是个糟糕的主意.但有些情况是合理的,比如数值类型.考虑,有理数Rational有分子,分母两个字段,缺省参数值为0,1.Ration a = 2;我们期望构造一个 ...

  3. jquery回车提交表单

    jquery回车提交表单,比较方便的功能. 三个事件keydown,keypress,keyup,分别是按下,按着没上抬,上抬键盘 ,所以用keyup. $(document).keyup(funct ...

  4. struts2标签具体解释

    要在jsp中使用Struts2的标志,先要指明标志的引入.通过jsp的代码的顶部增加下面的代码: <%@taglib prefix="s" uri="/struts ...

  5. iOS之宏定义#define

    最基本的宏定义用法 #define aaa bbb 表示用aaa替换bbb的内容. 宏作用范围 宏的作用范围是在当前文件内, 如果需要作用于其他类(如在类b调用类a已定义宏),那么需要在类b引入类a的 ...

  6. Mailing API

    Mailing API Configuration Basic Usage Embedding Inline Attachments Mail & Local Development Work ...

  7. iOS-制作Framework

    步骤 打开Xcode,创建新工程.手下留情,请先看图! 在TARGETS下选中工程,在Build Settings下更改几个参数. 更改参数 在Architectures下增加armv7s,并选中.将 ...

  8. org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge]

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFacto ...

  9. Java基础知识强化之IO流笔记67:Properties的特殊功能使用

    1. Properties的特殊功能 public Object setProperty(String key,String value):添加元素 public String getProperty ...

  10. 【转】Git代码提交最佳实践

      GIT Commit Good Practice The following document is based on experience doing code development, bug ...