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 ...
随机推荐
- error: variable '__this_module' has initializer but incomplete type错误解决
版权所有,转载必须说明转自 http://my.csdn.net/weiqing1981127 原创作者:南京邮电大学 通信与信息系统专业 研二 魏清 问题描述:使用SAM9X25 内核版本是2. ...
- 序列化与反序列化Serialize&Deserialize
序列化是指一个对象的实例可以被保存,保存成一个二进制串,当然,一旦被保存成二进制串,那么也可以保存成文本串了.比如,一个计数器,数值为2,我们可以用字符串“2”表示.如果有个对象,叫做connter, ...
- 如何关闭UINavigationController 向右滑动 返回上一层视图
说明一下: 我的nav 设置的rootview 是 tabbarcontroller,登录界面是push进去的,所以,在登录界面,如果靠近最左边 向右滑动 会出现 tabbarcontroller的视 ...
- 【转】与BT下载相关的概念
1. DHT DHT全称叫分布式哈希表(Distributed Hash Table),是一种分布式存储方法.在不需要服务器的情况下,每个客户端负责一个小范围的路由,并负责存储一小部分数据,从而实现整 ...
- JavaScript toFixed() 方法
定义和用法toFixed() 方法可把 Number 四舍五入为指定小数位数的数字. 语法NumberObject.toFixed(num) 参数 描述num 必需.规定小数的位数,是 0 ~ 20 ...
- [Effective C++ --023]宁以non-member、non-friend替换member函数
作者在这一节中花了大幅度的篇幅来介绍为什么最好使用non-member.non-friend函数. 思路如下: 场景:如果有一个class用来表示网页浏览器,那么清楚缓存及历史记录的时候,我们可能定义 ...
- php关于日期时间 php日期 php时间
strtotime 的牛逼用法: $a='-4 days '.date('Y-m-d');$day = date('Y-m-d', strtotime($a));var_dump($day); /** ...
- java_jdbc_3层 解耦
Dao - 提供接口 DaoImpl - 实现 DaoFactory - 工厂模式获取实现 DaoExcetpion - jdbc异常处理 实现runtime exception类即可 TestDem ...
- How to allow/block PING on Linux server – IPTables rules for icmp---reference
BY ADMIN - APRIL, 9TH 2014 The ‘PING’, it’s a command-line tool to check a host is reachable or not. ...
- jsp The requested resource (/demo10/loginBean) is not available.
The requested resource (/demo10/loginBean) is not available. <?xml version="1.0" encodi ...