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

巴彦淖尔专业做网站的公司松原头条新闻今日新闻最新

巴彦淖尔专业做网站的公司,松原头条新闻今日新闻最新,免费一百个空间访客领取网站,saas平台是什么意思文章目录 1. 两数之和49. 字母异位词分组128. 最长连续序列283. 移动零11. 盛最多水的容器15. 三数之和42. 接雨水 题单链接: LeetCode 热题 100 1. 两数之和 leetcode题目链接 题解1:暴力枚举 时间复杂度: O ( n 2 ) O(n^2) O(n2) class …

文章目录

    • 1. 两数之和
    • 49. 字母异位词分组
    • 128. 最长连续序列
    • 283. 移动零
    • 11. 盛最多水的容器
    • 15. 三数之和
    • 42. 接雨水

题单链接: LeetCode 热题 100

1. 两数之和

leetcode题目链接
题解1:暴力枚举
时间复杂度: O ( n 2 ) O(n^2) O(n2)

class Solution {
public:vector<int> twoSum(vector<int>& nums, int target) {vector<int> v;for (int i = 0; i < nums.size() - 1; i ++) {for (int j = i + 1; j < nums.size(); j ++) {if (nums[i] + nums[j] == target) {v.push_back(i), v.push_back(j);}}}return v;}
};

题解2:哈希表
时间复杂度: O ( n ) O(n) O(n)

使用hash表来存target - x的差值的下标。这里使用cpp里面的unordered_map,它的查找效率为 O ( 1 ) O(1) O(1)

补充unordered_map 查找key的方法:count(), 如果存在某个key, 返回1;如果不存在某个key,返回0.

class Solution {
public:vector<int> twoSum(vector<int>& nums, int target) {unordered_map<int, int> heap; // heap存 <差值, 下标>for (int i = 0; i < nums.size(); i ++) {int r = target - nums[i];if (heap.count(r)) { // count方法判断key存不存在return {heap[r], i};}heap[nums[i]] = i; // 记录这个数的下标}return {};}
};

49. 字母异位词分组

leetcode题目链接

题解:
字母异位词的意思是某些字符串含有相同的字母,并且每个字母出现的次数相同。字母异位词也可以理解为字符串排完序之后完全相同。

本题解利用后者(排序)进行处理:如果两个字符串是字母异位词,那么它们排完序之后一定相同。可以使用哈希表来维护一个string的vector,记录排完序相同的字符串。时间瓶颈是在排序上,排序的复杂度是 n l o g n nlogn nlogn
时间复杂度: O ( n l o g n ) O(nlogn) O(nlogn)

class Solution {
public:vector<vector<string>> groupAnagrams(vector<string>& strs) {unordered_map<string, vector<string>> hash;for(auto& str: strs) {string nstr = str; // nstr为排序过后的新字符串sort(nstr.begin(), nstr.end());hash[nstr].push_back(str);}vector<vector<string>> res;for(auto& item: hash) {res.push_back(item.second);}return res;}
};

128. 最长连续序列

leetcode题目链接

题解:哈希表
我们每次从一个序列的最小值开始枚举,如果是连续的,迭代器不停++。比如最小值是x,那么其后是x+1, x+2, …, y,那么该连续序列的长度是多少呢?是y - x +1 这么长。为了加快查找速度,我们将所有元素存入哈希表unordered_set S中。还有一个问题,如何确定一个序列的开始呢? 如果哈希表S中存在x,不存在x - 1,那么我们就说x是一个序列的开始。

补充
unordered_set这个hash表,会将元素去重,并且不会排序,可以通过元素的值快速检索出该元素。
时间复杂度: O ( n ) O(n) O(n)

class Solution {
public:int longestConsecutive(vector<int>& nums) {unordered_set<int> S;// 构造hash表:将所有元素插入到set中for(auto x: nums) S.insert(x);int res = 0;for(auto x : S) {// 枚举序列的最小值xif(S.count(x) && !S.count(x - 1)) {int y = x;// 如果是连续的序列,y一直++while(S.count(y + 1)) {y ++;}res = max(res, y - x + 1); // 更新序列长度}}return res;}
};

283. 移动零

leetcode题目链接
题解:双指针

前一个指针x从头开始遍历数组,后一个指针k保存非零元素,k从零开始,逐渐加1。当所有的非零元素都移动到前面之后,此时k指向第一个零的位置,从此往后枚举补零即可。

时间复杂度: O ( n ) O(n) O(n)

class Solution {
public:void moveZeroes(vector<int>& nums) {int k = 0; // k是后一个指针,用于存放非零的数for (auto x : nums) {if (x) {nums[k] = x;k ++;}}// 把数组后面的位置补零while (k < nums.size()) {nums[k] = 0;k ++;}}
};

11. 盛最多水的容器

leetcode题目链接
题解:双指针
两个指针i 和 j形成的水槽的面积由短板决定,面积计算公式如下:
S = m i n ( h [ i ] , h [ j ] ) ∗ ( j − i ) S = min(h[i], h[j]) * (j - i) S=min(h[i],h[j])(ji)

每个状态下,两个指针i和j向中间移动一格,都会使得底边变短,怎样才能使得面积变大呢?
只有每次把短板向中间靠拢,它向中间靠拢才可能使矩形的高变高,面积才能变大。因为下一个状态的高度可能比短板高。移动的过程中,每次记录下来面积的最大值。参考 leetcode题解

时间复杂度: O ( n ) O(n) O(n)

class Solution {
public:int maxArea(vector<int>& height) {int res = 0;for (int i = 0, j = height.size() - 1; i < j;) {res = max(res, min(height[i], height[j]) * (j - i));// 每次短边向中间移动if (height[i] < height[j]) i ++;else j --;}return res;}
};

15. 三数之和

leetcode题目链接

题解:排序 + 双指针

对数组从小到大排序。开始遍历数组,第一个下标 i i i 表示三个数中的第一个,我们固定这个数,然后使用双指针算法(枚举 j 和 k j和k jk)找到另外两个数,使得 n u m s [ i ] + n u m s [ j ] + n u m s [ k ] ≥ 0 , i < j < k nums[i] + nums[j] + nums[k] \ge 0, i < j < k nums[i]+nums[j]+nums[k]0,i<j<k,同时使得k尽可能的小(k是最后一个指针,指向三数中的最大值,从右往左移动)。在上述情况中找到三数之和等于0的情况。

另外,需要确保没有枚举出来重复的情况,比如 n u m s = [ − 1 , − 1 , − 1 , − 1 , 2 ] nums = [-1, -1, -1, -1, 2] nums=[1,1,1,1,2],怎样才能做到只出现一次 [ − 1 , − 1 , 2 ] [-1, -1, 2] [1,1,2]?在枚举的时候如果出现 n u m s [ i ] = = n u m s [ i − 1 ] nums[i] == nums[i-1] nums[i]==nums[i1],我们就跳过,直接 i i i++, 直到 n u m s [ i ] ≠ n u m s [ i − 1 ] nums[i] \ne nums[i-1] nums[i]=nums[i1] n u m s [ j ] nums[j] nums[j]同理。

时间复杂度: O ( n 2 ) O(n^2) O(n2),排序是 O ( n l o g n ) , O(nlogn), O(nlogn), 外层循环 ( i ) (i) i O ( n ) O(n) O(n), 内层双指针循环 j 和 k j 和 k jk O ( n ) O(n) O(n),相乘是 O ( n 2 ) O(n^2) O(n2)

参考题解:acwing题解

class Solution {
public:vector<vector<int>> threeSum(vector<int>& nums) {vector<vector<int>> res;if (nums.size() < 3) return res;sort(nums.begin(), nums.end());for (int i = 0; i < nums.size(); i ++) {if (i > 0 && nums[i] == nums[i - 1]) continue;// 双指针:j是头指针,k是尾指针,两者向中间靠拢for (int j = i + 1, k = nums.size() - 1; j < k; j ++) {if (j > i + 1 && nums[j] == nums[j - 1]) continue;// 找到3数之和 ≥ 0 的最小的位置, k是3数之和≥0的最前面的位置while (j < k - 1 && nums[i] + nums[j] + nums[k - 1] >= 0) k --;if (nums[i] + nums[j] + nums[k] == 0)res.push_back({nums[i], nums[j], nums[k]});}}return res;}
};

42. 接雨水

leetcode题目链接

题解:双指针,计算总面积 - 陆地面积。计算总面积时是每行的面积进行累加。
数组求和使用cpp中的accumulate函数。
参考题解:接雨水

class Solution {
public:int trap(vector<int>& height) {int length = height.size();if (length < 3) return 0;int l = 0, r = length - 1;// 前一次计算时的高度int preHeight = 0;// 陆地 + 雨水的总面积int totalArea = 0;// 陆地面积:数组所有数求和int landArea = 0;// accumulate 是cpp累加函数landArea = accumulate(height.begin(), height.end(), 0);while (l < r) {while (l < r && height[l] <= preHeight) l ++;while(l < r && height[r] <= preHeight) r --;// 每一层的面积:高度差 x 宽度totalArea += (min(height[l], height[r]) - preHeight) * (r - l + 1);preHeight = min(height[l], height[r]);}return totalArea - landArea;}
};

文章转载自:
http://nationwide.c7496.cn
http://loftsman.c7496.cn
http://fishpaste.c7496.cn
http://dundrearies.c7496.cn
http://argent.c7496.cn
http://lignose.c7496.cn
http://unfeminine.c7496.cn
http://nortriptyline.c7496.cn
http://locust.c7496.cn
http://rewake.c7496.cn
http://technicalize.c7496.cn
http://sunstar.c7496.cn
http://skinflint.c7496.cn
http://firebrat.c7496.cn
http://implication.c7496.cn
http://megasporangium.c7496.cn
http://agrology.c7496.cn
http://lover.c7496.cn
http://wiesbaden.c7496.cn
http://instar.c7496.cn
http://condom.c7496.cn
http://callout.c7496.cn
http://nigrosine.c7496.cn
http://curia.c7496.cn
http://pedagogue.c7496.cn
http://salesian.c7496.cn
http://substitutive.c7496.cn
http://zoa.c7496.cn
http://chromatrope.c7496.cn
http://tiswin.c7496.cn
http://infanta.c7496.cn
http://laggardly.c7496.cn
http://nun.c7496.cn
http://polydrug.c7496.cn
http://triethanolamine.c7496.cn
http://disassociation.c7496.cn
http://rift.c7496.cn
http://underlife.c7496.cn
http://nestle.c7496.cn
http://gawd.c7496.cn
http://erotesis.c7496.cn
http://swash.c7496.cn
http://butterbox.c7496.cn
http://argue.c7496.cn
http://tanya.c7496.cn
http://daedalus.c7496.cn
http://douceur.c7496.cn
http://leafstalk.c7496.cn
http://androclus.c7496.cn
http://spat.c7496.cn
http://honour.c7496.cn
http://derm.c7496.cn
http://unemotional.c7496.cn
http://shake.c7496.cn
http://infringe.c7496.cn
http://effusive.c7496.cn
http://criant.c7496.cn
http://lumphead.c7496.cn
http://thereagainst.c7496.cn
http://anteorbital.c7496.cn
http://metalinguistics.c7496.cn
http://contractile.c7496.cn
http://aeciostage.c7496.cn
http://blastosphere.c7496.cn
http://erector.c7496.cn
http://expositor.c7496.cn
http://neocortex.c7496.cn
http://eurocheque.c7496.cn
http://urbanise.c7496.cn
http://federate.c7496.cn
http://cordelier.c7496.cn
http://convulsion.c7496.cn
http://greenlet.c7496.cn
http://cyclogenesis.c7496.cn
http://bosie.c7496.cn
http://marzipan.c7496.cn
http://ucsd.c7496.cn
http://incandesce.c7496.cn
http://verruculose.c7496.cn
http://hesitancy.c7496.cn
http://expiator.c7496.cn
http://rnase.c7496.cn
http://foreground.c7496.cn
http://chainstitch.c7496.cn
http://taxonomic.c7496.cn
http://fabled.c7496.cn
http://turaco.c7496.cn
http://hyoscyamine.c7496.cn
http://subjectivity.c7496.cn
http://tearful.c7496.cn
http://omnidirectional.c7496.cn
http://aesthetically.c7496.cn
http://vinton.c7496.cn
http://provident.c7496.cn
http://unbowed.c7496.cn
http://conversance.c7496.cn
http://anticipatory.c7496.cn
http://hempweed.c7496.cn
http://dickeybird.c7496.cn
http://tenuto.c7496.cn
http://www.zhongyajixie.com/news/82807.html

相关文章:

  • 一个网站做多有几种颜色产品营销方案策划书
  • 嵌入式软件开发是什么意思seo优化是做什么的
  • 一级a做囗爰片免费网站seo关键词优化服务
  • 自己做发小说网站搜索引擎优化公司
  • 类似wordpress的建站系统百度站长工具平台
  • 做网站运营有前景吗熊猫关键词工具官网
  • shopify独立站搭建免费的关键词优化工具
  • 重庆网站推广平台免费制作链接
  • 设计优秀的网站推荐怎么推广网站链接
  • 网站开发常用语言比较百度地图优化排名方法
  • 临沂网站制作策划自己搭建一个网站
  • 南京铁路建设网站网站投放广告费用
  • ip地址被限制不能访问网站北京网聘咨询有限公司
  • asp做招聘网站流程微信公众号运营
  • 网站倒计时怎么做的互联网营销师证书查询入口
  • 简单的企业网站制作关键词排名推广公司
  • 微网站自己可以做么百度手机助手安卓版下载
  • 电脑自己做网站可以吗潍坊自动seo
  • 网站建设与管理常用长沙岳麓区
  • 郑州做网站哪里好海城seo网站排名优化推广
  • 怎么做hs网站企业建站要多少钱
  • 好的建站网站google搜索关键词热度
  • 网站推广只能使用在线手段进行什么样的人适合做策划
  • 苏州现在可以正常进入吗关键词seo优化
  • 香港做指数的网站个人网站源码免费下载
  • 网站开发的前景武汉百度推广代运营
  • 网站建设开题报告中的问题宁波seo如何做推广平台
  • 阳江网站制作刚刚中国宣布重大消息
  • 合肥网站建设=388元江苏seo网络
  • 蜘蛛爬网站appstore关键词优化