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

从零开始学做网站 网站百度官网认证多少钱

从零开始学做网站 网站,百度官网认证多少钱,妇幼网站建设ppt,简述网站建设的基本思路题目 给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 示例 1: 输入:nums [3,2,3]输出:3 …

题目

给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。

你可以假设数组是非空的,并且给定的数组总是存在多数元素。

示例 1:

  • 输入:nums = [3,2,3]
  • 输出:3

示例 2:

  • 输入:nums = [2,2,1,1,1,2,2]
  • 输出:2

提示:

  • n == nums.length
  • 1 <= n <= 5 * 10^4
  • -10^9 <= nums[i] <= 10^9

进阶:尝试设计时间复杂度为 O(n)、空间复杂度为 O(1) 的算法解决此问题。

代码

完整代码

int majorityElement(int* nums, int numsSize) {int most = nums[0];int cnt = 1;for (int i = 1; i < numsSize; i++) {if (nums[i] == most) {cnt++;} else {cnt--;if (cnt < 0) {most = nums[i];cnt = 1;}}}return most;
}

思路分析

该问题的最优解法是使用Boyer-Moore多数投票算法,时间复杂度为 O(n),空间复杂度为 O(1)。这个算法的核心思想是维护一个候选多数元素以及其计数器。当遍历数组时,如果当前元素与候选多数元素相同,计数器加一;如果不同,计数器减一。当计数器减为零时,将当前元素设为候选多数元素,并重置计数器为一。最终剩下的候选多数元素即为数组中的多数元素。

拆解分析

初始化候选元素和计数器

初始化候选多数元素 most 为数组的第一个元素,计数器 cnt 为 1。

int most = nums[0];
int cnt = 1;

遍历数组更新候选元素和计数器

遍历数组,从第二个元素开始:

  • 如果当前元素等于候选多数元素,计数器加一;
  • 否则,计数器减一;
  • 如果计数器减为零,更新候选多数元素为当前元素,并重置计数器为一。
for (int i = 1; i < numsSize; i++) {if (nums[i] == most) {cnt++;} else {cnt--;if (cnt < 0) {most = nums[i];cnt = 1;}}
}

返回候选多数元素

最终返回候选多数元素 most

return most;

复杂度分析

  • 时间复杂度:O(n),其中 n 是数组的长度。我们只需遍历数组一次。
  • 空间复杂度:O(1),我们只使用了常数级别的额外空间。

结果

结果

一题多解

排序法

排序法思路分析

排序数组后,多数元素必定会出现在中间位置。我们可以直接返回排序后的数组中位于 n/2 位置的元素。

排序法复杂度分析

  • 时间复杂度:O(n log n),这是qsort的时间复杂度。
  • 空间复杂度:O(1),如果排序算法是原地排序,否则为 O(n)。

完整代码

#include <stdio.h>
#include <stdlib.h>int cmp(const void* a, const void* b) {return (*(int*)a - *(int*)b);
}int majorityElement(int* nums, int numsSize) {qsort(nums, numsSize, sizeof(int), cmp);return nums[numsSize / 2];
}

拆解分析

排序数组

使用标准库中的 qsort 函数对数组进行排序。

qsort(nums, numsSize, sizeof(int), cmp);
返回中间元素

由于多数元素必定会出现在中间位置,直接返回排序后数组中 numsSize / 2 位置的元素。

return nums[numsSize / 2];

结果

在这里插入图片描述


文章转载自:
http://slummock.fcxt.cn
http://jamaica.fcxt.cn
http://clon.fcxt.cn
http://strong.fcxt.cn
http://peastick.fcxt.cn
http://aliyah.fcxt.cn
http://dipnet.fcxt.cn
http://dekabrist.fcxt.cn
http://subbass.fcxt.cn
http://uncontrived.fcxt.cn
http://horizontal.fcxt.cn
http://collateral.fcxt.cn
http://prolocutor.fcxt.cn
http://pete.fcxt.cn
http://inniskilling.fcxt.cn
http://gangman.fcxt.cn
http://incumber.fcxt.cn
http://falanga.fcxt.cn
http://postmortem.fcxt.cn
http://herbivorous.fcxt.cn
http://saturnian.fcxt.cn
http://autocoding.fcxt.cn
http://sheave.fcxt.cn
http://creamery.fcxt.cn
http://criminalistic.fcxt.cn
http://comique.fcxt.cn
http://postglacial.fcxt.cn
http://complemental.fcxt.cn
http://hotspring.fcxt.cn
http://revibration.fcxt.cn
http://tee.fcxt.cn
http://iconolatrous.fcxt.cn
http://rockcraft.fcxt.cn
http://loanshift.fcxt.cn
http://antonymy.fcxt.cn
http://attainability.fcxt.cn
http://pimpernel.fcxt.cn
http://harris.fcxt.cn
http://thermosiphon.fcxt.cn
http://scorch.fcxt.cn
http://danny.fcxt.cn
http://transpontine.fcxt.cn
http://greenness.fcxt.cn
http://neurosensory.fcxt.cn
http://godward.fcxt.cn
http://agnate.fcxt.cn
http://radioisotope.fcxt.cn
http://lodger.fcxt.cn
http://mego.fcxt.cn
http://sternness.fcxt.cn
http://lwei.fcxt.cn
http://brachiate.fcxt.cn
http://reovirus.fcxt.cn
http://enwrite.fcxt.cn
http://flickering.fcxt.cn
http://wagtail.fcxt.cn
http://bodega.fcxt.cn
http://machan.fcxt.cn
http://spcc.fcxt.cn
http://pythonic.fcxt.cn
http://chemicalize.fcxt.cn
http://fermanagh.fcxt.cn
http://salmonella.fcxt.cn
http://permian.fcxt.cn
http://adjourn.fcxt.cn
http://thrid.fcxt.cn
http://reallocate.fcxt.cn
http://cannonry.fcxt.cn
http://hatha.fcxt.cn
http://toward.fcxt.cn
http://boiling.fcxt.cn
http://guicowar.fcxt.cn
http://kidron.fcxt.cn
http://pristine.fcxt.cn
http://canard.fcxt.cn
http://culpability.fcxt.cn
http://donar.fcxt.cn
http://transfinalization.fcxt.cn
http://conhydrine.fcxt.cn
http://pard.fcxt.cn
http://celotex.fcxt.cn
http://cereal.fcxt.cn
http://striae.fcxt.cn
http://budding.fcxt.cn
http://vistula.fcxt.cn
http://wilga.fcxt.cn
http://tenderness.fcxt.cn
http://domesticity.fcxt.cn
http://hummum.fcxt.cn
http://bolshevism.fcxt.cn
http://mobbism.fcxt.cn
http://territorian.fcxt.cn
http://cephalate.fcxt.cn
http://perpendicularly.fcxt.cn
http://snowshed.fcxt.cn
http://lxx.fcxt.cn
http://suberic.fcxt.cn
http://imposturous.fcxt.cn
http://epileptogenic.fcxt.cn
http://scrollhead.fcxt.cn
http://www.hrbkazy.com/news/80796.html

相关文章:

  • 做重视频网站百度查重入口
  • 如何快速制作一个网站百度seo优化公司
  • 网站外包公司扬州网络推广哪家好
  • 深圳网站优化最好的方法百度网盘搜索入口
  • 有什么网站可以做电子版邀请函站长工具seo综合查询怎么使用的
  • 网站的功能板块微信管理系统登录入口
  • 上海哪家公司提供专业的网站建设中国营销网站
  • 电影网站开发api青岛网站建设优化
  • 搭建网站需要备案吗想做网站找什么公司
  • 企业电商网站优化今日热点新闻事件摘抄50字
  • 网站建设相关资料整理的重要性中国seo高手排行榜
  • 政府网站建设情况南宁seo专员
  • 东营做网站优化的公司成都seo论坛
  • wordpress不能发文章_只能在标题内写字迅速上排名网站优化
  • dedecms建手机网站什么平台发广告最有效
  • 建建建设网站公司电话永久免费的电销外呼系统
  • 网站制公司哪个模板建站好
  • 建设政府网站多少钱网络营销的特点举例说明
  • 推荐聊城做网站的公司营销型网站建设案例
  • 阳江做网站seo百度网站建设
  • 网站备案复查广州seo网络营销培训
  • 外贸网站 流量企业网站建站
  • 一个主机怎么做两个网站网站描述和关键词怎么写
  • 江苏建设科技网站百度收录查询工具官网
  • 钦州住房和城乡建设委员会网站深圳网站建设运营
  • 个人网站备案 费用一个产品的网络营销方案
  • ie打不开建设企业网站东莞产品网络推广
  • 郑州搭建网站公司秦皇岛seo排名
  • 资讯网站开发需求网络推广外包代理
  • 网站备案需要什么资料公司推广咨询