LeetCode FindMinimuminRotatedSorteArray &&FindMinimuminRotatedSorteArray2
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的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- ALV可输入状态下输入金额字段变小数的问题
http://blog.163.com/mxb_sap@yeah/blog/static/10335262520167109022155/ 小数位数两位 当我在给ALV上给该字段输入整数 '12 ...
- Android 调用系统的分享[完美实现同一时候分享图片和文字]
android 系统的分享功能 private void share(String content, Uri uri){ Intent shareIntent = new Intent(Intent. ...
- mysql学习--mysql必知必会1
例如以下为mysql必知必会第九章開始: 正則表達式用于匹配特殊的字符集合.mysql通过where子句对正則表達式提供初步的支持. keywordregexp用来表示后面跟的东西作为正則表達式 ...
- 理解shared_ptr<T> ---2
1.引用计数字段不能放在资源管理类中.我们的解决办法是,把引用计数和资源绑在一起,进行二次封装.但是这样存在一个大问题,不同类型的资源管理类不能兼容.也就是说,shared_ptr<Dog> ...
- ThinkPHP CURD方法盘点:order方法
order方法属于模型的连贯操作方法之一,用于对操作的结果排序. 用法 $Model->where('status=1')->order('id desc')->limit(5)-& ...
- js 类似php中foreach的方法
参考下面实例 <script>var arr = { 'a' : '111111', 'b' : '222222', 'c' : '333333'};for(var k ...
- hdu 5438 Ponds 拓扑排序
Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...
- C# 网络编程之豆瓣OAuth2.0认证具体解释和遇到的各种问题及解决
近期在帮人弄一个豆瓣API应用,在豆瓣的OAuth2.0认证过程中遇到了各种问题,同一时候自己须要一个个的尝试与解决,终于完毕了豆瓣API的訪问.作者这里就不再吐槽豆瓣的认证文档了,毕 ...
- HT518V311
HT518V311.rar
- protocol buffer的简单使用
protocol buffer是一个高效的结构化数据存储格式,用来结构化数据的序列化与反序列化.目前支持java.c++.Python 相对于json而言: 数据量跟小 其他的还没看出什么优势 下载地 ...