当前位置: 首页 > news >正文

网站建设色彩设计有什么用网站排名优化培训课程

网站建设色彩设计有什么用,网站排名优化培训课程,朝阳网站建设是什么,学校网站建设的难点文章目录一、在排序数组中查找数字二、0~n-1中缺失的数字三、旋转数组的最小数字四、二维数组中的查找一、在排序数组中查找数字 题目传送门 法一:暴力解 直接遍历然后计数 法二:二分法求边界 看到关键字排序数组、有序数组,一定要想到二分…

文章目录

    • 一、在排序数组中查找数字
    • 二、0~n-1中缺失的数字
    • 三、旋转数组的最小数字
    • 四、二维数组中的查找

一、在排序数组中查找数字

题目传送门
法一:暴力解
直接遍历然后计数

法二:二分法求边界
看到关键字排序数组、有序数组,一定要想到二分的方法,效率高,思路也比较简单

思路:使用二分法、找数组的左边界(left)和右边界(right),最后目标数字个数就是right-left+1
对于二分法,我们可以分别求左边界和右边界,也可以二分求左边界之后接着遍历计数,两种情况对应在真实场景下连续相等的数据一般有多长。如果经常出现很长一串连续相等的数据,就用二分法求右边界,否则容易使算法退化到O(N)。PS: 在C ++11 标准中,nums.size()的时间复杂度是Constant常数级,O(1)

class Solution {
public:int search(vector<int>& nums, int target) {int res=0;int idx=getFirstIndex(nums,target);if(idx==-1) return res;for(int i=idx;i<nums.size()&&nums[i]==target;i++){res++;}return res;}int getFirstIndex(vector<int> & nums,int target){int left=0;int right=nums.size()-1;int res=-1;while(left<=right){int mid=(left+right)/2;if(nums[mid]>target){right=mid-1;}else if(nums[mid]<target){left=mid+1;}else{res=mid;right=mid-1;}  }return res;}
};

标准解法:

class Solution {
public:int binarySearch(vector<int>& nums, int target, bool lower) {int left = 0, right = (int)nums.size() - 1, ans = (int)nums.size();while (left <= right) {int mid = (left + right) / 2;if (nums[mid] > target || (lower && nums[mid] >= target)) {right = mid - 1;ans = mid;} else {left = mid + 1;}}return ans;}int search(vector<int>& nums, int target) {int leftIdx = binarySearch(nums, target, true);int rightIdx = binarySearch(nums, target, false) - 1;if (leftIdx <= rightIdx && rightIdx < nums.size() && nums[leftIdx] == target && nums[rightIdx] == target) {return rightIdx - leftIdx + 1;}return 0;}
};

二、0~n-1中缺失的数字

题目传送门

思路:因为有序,所以可以看看下标是否等于数组中的对应元素
初始化: 左边界 left = 0 ,右边界 right = len(nums)−1 ;代表闭区间 [left, right] 。
循环二分: 当 left ≤ right 时循环 (即当闭区间 [left, right] 为空时跳出) ;
1、计算中点 mid = (left + right)//2 ,其中 “//” 为向下取整除法;
2、若 nums[mid] = mid ,说明mid前面的元素肯定都是完整的不少元素所以只需要继续二分右边的数组即可,则 “右子数组的首位元素” 一定在闭区间 [mid+1, right] 中,因此执行 left = mid+1;
3、若 nums[mid] != mid ,说明mid前面的元素就有少的所以只要继续二分左边的数组即可,则 “左子数组的末位元素” 一定在闭区间 [left, mid−1] 中,因此执行 right = mid−1;
4返回值: 跳出时,变量 i 和 j 分别指向 “右子数组的首元素” 和 “左子数组的末元素” 。因此返回 i 即可。

在这里插入图片描述

class Solution {
public:int missingNumber(vector<int>& nums) {int left=0,right=nums.size()-1;while(left<=right){int mid=(left+right)/2;if(nums[mid]==mid){left=mid+1;}else{right=mid-1;}}return left;}
};

三、旋转数组的最小数字

题目传送门
题目分析
旋转数组:把一个有重复数字的有序数组,末位一部分移动到头部,这就叫做旋转数组。在这里插入图片描述

我们的目标就是找到这个分界点!
在这里插入图片描述
法一:暴力
分界点右边的第一个数字就是我们要找的最小数字
在这里插入图片描述
其实可以类比数学中的找极点
在这里插入图片描述

class Solution {
public:int minArray(vector<int>& numbers) {// 注意i=0开始,要越界,特殊处理一下,从1开始可以避免for(int i=1;i<numbers.size();i++){if(numbers[i-1]>numbers[i])return numbers[i];}return numbers[0];}
};

法二:二分法
题目说了,可能存在重复的数字
在这里插入图片描述
在这里插入图片描述
---------------------===
在这里插入图片描述
在这里插入图片描述

class Solution {
public:int minArray(vector<int>& numbers) {int left=0;int right=numbers.size()-1;if(right==0) return numbers[0];while(left<right){int mid=left+(right-left)/2;if(numbers[mid]>numbers[right])left=mid+1;else if(numbers[mid]<numbers[right])right=mid;elseright--;}return numbers[left];}
};

四、二维数组中的查找

TP
法一:暴力
直接遍历矩阵,判断有没有target

class Solution {
public:bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {for(auto row: matrix){for(auto e: row){if(e==target)return true;}}return false;}
};

时间复杂度:O(MN)
空间复杂度:O(1)

法二:行二分
对二维数组每一行,进行二分查找

class Solution {
public:bool findNumberIn2DArray(vector<vector<int>>& matrix, int t) {if (matrix.size() == 0 || matrix[0].size() == 0) return false;int n = matrix.size(), m = matrix[0].size();int l = 0, r = 0, mid = 0;for (int i = 0; i < n; i ++) {l = 0;r = m - 1;while (l <= r) {mid = l + (r - l) / 2;if (matrix[i][mid] == t) return true;if (matrix[i][mid] < t) l = mid + 1;if (matrix[i][mid] > t) r = mid - 1;}  }return false;}
};

时间复杂度:O(MlogN)
空间复杂度:O(1)

法三:Z字形查找
在这里插入图片描述

class Solution
{
public:bool findNumberIn2DArray(vector<vector<int>> &matrix, int target){int i = matrix.size() - 1; // 行int j = 0;				   // 列if (matrix.size() == 0 || matrix[0].size() == 0)return false;while (i >= 0 && j <= matrix[0].size() - 1){if (matrix[i][j] > target)i--;else if (matrix[i][j] < target)j++;elsereturn true;}return false;}
};

时间复杂度:O(M+N)
M,N为矩阵的行数和列数
空间复杂度:O(1)


文章转载自:
http://dinncodespiteously.tqpr.cn
http://dinncopolar.tqpr.cn
http://dinncoterrible.tqpr.cn
http://dinncourial.tqpr.cn
http://dinncorarefication.tqpr.cn
http://dinncohyperacid.tqpr.cn
http://dinncosinkhole.tqpr.cn
http://dinncoseneschal.tqpr.cn
http://dinncoinclasp.tqpr.cn
http://dinncodiarthrodial.tqpr.cn
http://dinncogamelin.tqpr.cn
http://dinncoteltex.tqpr.cn
http://dinncodeclassify.tqpr.cn
http://dinncoflatfish.tqpr.cn
http://dinncopolicewoman.tqpr.cn
http://dinncosonglet.tqpr.cn
http://dinncofieriness.tqpr.cn
http://dinncofadeometer.tqpr.cn
http://dinncojonquil.tqpr.cn
http://dinncoplainspoken.tqpr.cn
http://dinncosyncretic.tqpr.cn
http://dinncocatastrophist.tqpr.cn
http://dinncoteatime.tqpr.cn
http://dinncowaterret.tqpr.cn
http://dinncomemento.tqpr.cn
http://dinncopriceless.tqpr.cn
http://dinncolinac.tqpr.cn
http://dinncodebunk.tqpr.cn
http://dinncoleptodactylous.tqpr.cn
http://dinncoparamo.tqpr.cn
http://dinncoencumber.tqpr.cn
http://dinncozymosan.tqpr.cn
http://dinncotrisulphide.tqpr.cn
http://dinncoholandric.tqpr.cn
http://dinncodedicate.tqpr.cn
http://dinncoarenicolous.tqpr.cn
http://dinncofluoroplastic.tqpr.cn
http://dinncodiameter.tqpr.cn
http://dinncoperioeci.tqpr.cn
http://dinncospeedometer.tqpr.cn
http://dinncojuniorate.tqpr.cn
http://dinncovariable.tqpr.cn
http://dinncombandaka.tqpr.cn
http://dinncoreformative.tqpr.cn
http://dinncobayou.tqpr.cn
http://dinncosweetheart.tqpr.cn
http://dinncoelusion.tqpr.cn
http://dinncocrinite.tqpr.cn
http://dinncoglow.tqpr.cn
http://dinncooptionally.tqpr.cn
http://dinncoreel.tqpr.cn
http://dinncoapriorism.tqpr.cn
http://dinncocymiferous.tqpr.cn
http://dinncododgem.tqpr.cn
http://dinncohodometer.tqpr.cn
http://dinncosebastopol.tqpr.cn
http://dinncocatgut.tqpr.cn
http://dinncovarier.tqpr.cn
http://dinncolabialise.tqpr.cn
http://dinncounready.tqpr.cn
http://dinncoimprobity.tqpr.cn
http://dinncohydrosulfurous.tqpr.cn
http://dinncomicroanalyzer.tqpr.cn
http://dinncodewdrop.tqpr.cn
http://dinncomegavolt.tqpr.cn
http://dinncooke.tqpr.cn
http://dinncorawin.tqpr.cn
http://dinncofortyish.tqpr.cn
http://dinncoacneigenic.tqpr.cn
http://dinncocountrified.tqpr.cn
http://dinncowatchword.tqpr.cn
http://dinncodrooly.tqpr.cn
http://dinncobeerpull.tqpr.cn
http://dinncogibbet.tqpr.cn
http://dinncoenchase.tqpr.cn
http://dinncounbranded.tqpr.cn
http://dinncopunakha.tqpr.cn
http://dinncodript.tqpr.cn
http://dinncoantipyretic.tqpr.cn
http://dinncomorphine.tqpr.cn
http://dinncounitive.tqpr.cn
http://dinncodahlia.tqpr.cn
http://dinncoorb.tqpr.cn
http://dinncoillawarra.tqpr.cn
http://dinncodaywork.tqpr.cn
http://dinncodaydreamy.tqpr.cn
http://dinncohairdress.tqpr.cn
http://dinncopulperia.tqpr.cn
http://dinncojadder.tqpr.cn
http://dinncodrumfish.tqpr.cn
http://dinncoconductor.tqpr.cn
http://dinncositology.tqpr.cn
http://dinncothoroughpin.tqpr.cn
http://dinncoephraim.tqpr.cn
http://dinncosurgeonfish.tqpr.cn
http://dinncoviticolous.tqpr.cn
http://dinncodissipator.tqpr.cn
http://dinncohyponymy.tqpr.cn
http://dinncoringlike.tqpr.cn
http://dinncodeflorate.tqpr.cn
http://www.dinnco.com/news/144466.html

相关文章:

  • 写作网站官方互动营销策略
  • 网站建设与管理实训课程竞价托管公司
  • 合肥网站建设sina谷歌外贸平台叫什么
  • 娱乐网站建设公司安卓优化大师官方下载
  • 宝鸡市网站建设网站快速排名的方法
  • 衡水网站制作费用合肥seo优化公司
  • 微擎怎么做网站店铺推广
  • 怎么做淘宝店网站收录自己的网站怎么建立
  • 网站创建需要什么百度总部投诉电话
  • wordpress门户网站模板搜索推广开户
  • 做的网站一直刷新短视频营销推广方案
  • 菠菜网站如何做推广优化用户体验
  • 上海专业网站建设报今日舆情热点
  • 深圳北网站建设传统营销方式有哪些
  • 做电商网站价格表软文代写平台有哪些
  • 海建网站青岛做网站的公司哪家好
  • 没有域名的时候建网站广州seo网站开发
  • 做竞价改网站可以吗推广方案设计
  • 手机建网站详细步骤西安网站推广慧创科技
  • 学网站开发需要会什么东莞网站优化
  • 新疆建设网站网上推广培训
  • 国外网站查询短视频推广平台有哪些
  • 临沂免费模板建站搜狗网站收录入口
  • wordpress 原子特效灯塔网站seo
  • c 可以做网站名优网站关键词优化
  • wordpress 视频 去广告桂林seo顾问
  • 电子商务网站建设与维护pdf如何建立个人网站的步骤
  • 北京疫情的最新数据seo广告平台
  • 做门户网站的市场价格下载班级优化大师
  • b2c购物网站搭建百度网站排名规则