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 ...
随机推荐
- 判断一个Bitmap图像是否是.9图
见BitmapFactory的源码中 byte[] np = bm.getNinePatchChunk(); final boolean isNinePatch = np != null & ...
- 最全面的Android Intent机制讲解
对于大型软件开发经验较少的程序员来说,这可能是一个不太容易理解的抽象概念,因为它与我们平常使用的简单函数调用,或者通过库调用接口的方式不太一样.在 Intent 的使用中你看不到直接的函数调用,相对函 ...
- json处理复杂对象jsonConfig
我们通常的Json字符串和java当对象互转.经常有选择性地过滤掉一些属性值,和json-lib包JsonConfig为我们提供了这样一个 特征,有几种方法实施细则. (1)建立JsonConf ...
- java_可变参数构造器 Bulder模式
package com.test1.www; class NutritionFacts { //必须 private int servingSize; private int servings; // ...
- linux 查看文件命令总结
linux 查看文件命令总结 1.cat 查看文件内容 选项-b 空白行不显示行号.-n,空白行显示 2.more 查看文件内容,通过空格键查看下一页 q键退出查看 3.less 和上同,多了方向键( ...
- JAVA_Gson_example
package cn.kjxy.GSON; import java.util.List; import cn.kjxy.JSON.HttpHelpers; import com.google.gson ...
- mysql 按月按周统计
http://hi.baidu.com/liangjian1024/blog/item/6861541b1416094842a9ad7c.html 表finance有俩个字段如下 date date ...
- html select用法总结
本文将介绍select 原生的常用方法,这些都是经过测试,兼容ie6到ie10,及chrome,火狐等,也就是说大部分浏览器都兼容.如果大家发现有不兼容的情况,可以跟我留言. 我们对基本的用法了如指掌 ...
- PHP之ThinkPHP数据操作CURD
两个数据表 具体操作如下: /**********************数据库操作********************/ ////////添加数据////////// ...
- 【转】adb uninstall卸载apk文件说明
昨天在使用adb卸载程序,结果死活卸载不了.我输入的命令和系统提示如下: [plain] view plaincopy arthur@arthur-laptop:~$ adb uninstall ...