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

网站建设交易静安广州网站建设

网站建设交易,静安广州网站建设,个人网站备案代理,微信 文章 wordpress本系列为笔者的 Leetcode 刷题记录,顺序为 Hot 100 题官方顺序,根据标签命名,记录笔者总结的做题思路,附部分代码解释和疑问解答。 目录 01 移动零 02 盛最多水的容器 03 三数之和 04 接雨水 01 移动零 //双指针法 class Sol…

本系列为笔者的 Leetcode 刷题记录,顺序为 Hot 100 题官方顺序,根据标签命名,记录笔者总结的做题思路,附部分代码解释和疑问解答。

目录

01 移动零

02 盛最多水的容器

03 三数之和

04 接雨水


01 移动零

//双指针法
class Solution {
public:void moveZeroes(vector<int>& nums) {//左指针指向当前已经处理好的序列的尾部//右指针指向待处理序列的头部int n = nums.size(), left = 0, right = 0;while(right < n){if(nums[right]){swap(nums[left], nums[right]);left++;}right++;}}
};

02 盛最多水的容器

class Solution {
public:int maxArea(vector<int>& height) {int l = 0, r = height.size() - 1;int ans = 0;while(l < r){int area = min(height[l], height[r])*(r - l); //计算当前水量ans = max(ans, area);if(height[l] < height[r]) ++l; //移动挡板else --r;}return ans;}
};

03 三数之和

class Solution {
public:vector<vector<int>> threeSum(vector<int>& nums) {//排序sort(nums.begin(), nums.end());vector<vector<int>> ans;int n = nums.size();for(int first = 0; first < (n - 2); ++first){ //1 first < (n - 2)if(first > 0 && nums[first] == nums[first - 1]) continue;//双指针int third = n - 1;int target = -nums[first];for(int second = (first + 1); second < (n - 1); ++second){ //2 second < (n - 1)if(second > (first + 1) && nums[second] == nums[second - 1]) continue;while(second < third && nums[second] + nums[third] > target) --third;if(second == third) break;if(nums[second] + nums[third] == target){ans.push_back({nums[first], nums[second], nums[third]});}}}return ans;}
};

04 接雨水

class Solution {
public:int trap(vector<int>& height) {}
};

方法一:哈希数组

  • 建立两个哈希数组,leftMax(n)rightMax(n)

  • leftMax[i] 用来存储 i 左边的最大柱子

  • rightMax[i] 用来存储 i 右边的最大柱子

  • i 的储水值为 min(leftMax[i], rightMax[i]) - height[i]

  • 注:if(n == 0) return 0;

class Solution {
public:int trap(vector<int>& height) {int n = height.size();if(n == 0) return 0;vector<int> leftMax(n);leftMax[0] = height[0];for(int i = 1; i <= n-1; ++i){leftMax[i] = max(leftMax[i - 1], height[i]);}vector<int> rightMax(n);rightMax[n - 1] = height[n - 1];for(int i = (n - 2); i >= 0; --i){rightMax[i] = max(rightMax[i + 1], height[i]);}int ans = 0;for(int i = 0; i < n; ++i){ans += min(leftMax[i], rightMax[i]) - height[i];}return ans;}
};

方法二:单调栈

  • 建立一个单调栈 stk,用来存储单调不增长柱子序列

  • 遇到高个柱子,秒掉 top,增加一层雨水

  • 一层雨水体积为 (i - left - 1) * (min(height[left], height[i]) - height[top])

  • 注:if(stk.empty()) break;

class Solution {
public:int trap(vector<int>& height) {stack<int> stk;int n = height.size();int ans = 0;for(int i=0; i<n; ++i){while(!stk.empty() && height[i] > height[stk.top()]){int top = stk.top();stk.pop();if(stk.empty()) break;int left = stk.top();int currWidth = i - left - 1;int currHeight = min(height[left], height[i]) - height[top];ans += currHeight * currWidth;}stk.push(i);}return ans;}
};

方法三:双指针

  • 建立双指针 leftright,用来存储左边最大柱子和右边最大柱子

  • 左边柱子低,则移动 left ,添加一列雨水

  • 右边柱子低,则移动 right ,添加一列雨水

  • 一列雨水体积为 leftMax - height[left]rightMax - height[right]

class Solution {
public:int trap(vector<int>& height) {int n = height.size();int ans = 0;int left = 0, right = n - 1; //双指针int leftMax = 0, rightMax = 0;while(left < right){leftMax = max(leftMax, height[left]);rightMax = max(rightMax, height[right]);if(leftMax < rightMax){ans += leftMax - height[left];++left;}else{ans += rightMax - height[right];--right;}}return ans;}
};

作者:力扣官方题解
链接:https://leetcode.cn/problems/move-zeroes/solutions/489622/yi-dong-ling-by-leetcode-solution/
来源:力扣(LeetCode)


文章转载自:

http://D1RrHUoZ.fdmtr.cn
http://t7LtoEwT.fdmtr.cn
http://7jB25Kg9.fdmtr.cn
http://CNooNnS9.fdmtr.cn
http://f20tAfUi.fdmtr.cn
http://H5gdJgpT.fdmtr.cn
http://5kB8Y24g.fdmtr.cn
http://f2OjjOI8.fdmtr.cn
http://q5FmOe4O.fdmtr.cn
http://O9bH56wq.fdmtr.cn
http://h6UbLLjA.fdmtr.cn
http://ictr5bVu.fdmtr.cn
http://KEgWUgyD.fdmtr.cn
http://WNT7LGkF.fdmtr.cn
http://LcZjiTNC.fdmtr.cn
http://Wwzft13D.fdmtr.cn
http://JNu5vdzD.fdmtr.cn
http://MUaPhvMU.fdmtr.cn
http://nNUEpaL2.fdmtr.cn
http://ojh4mxgX.fdmtr.cn
http://dr1mvrfw.fdmtr.cn
http://Iqc85390.fdmtr.cn
http://Q880gln6.fdmtr.cn
http://nMRsPglk.fdmtr.cn
http://DEfRu6og.fdmtr.cn
http://zum0fv8t.fdmtr.cn
http://CZBaOzhr.fdmtr.cn
http://p784m21R.fdmtr.cn
http://PEHr36s9.fdmtr.cn
http://6Wzt0XMJ.fdmtr.cn
http://www.dtcms.com/wzjs/675548.html

相关文章:

  • 网站建设合同服务内容西安网站开发高端网站开发
  • 濮阳网站建设公司网站内部关键词
  • 新昌县住房和城乡建设局网站哪些网站可以做行程
  • 做网站用win还是li建设小程序怎么挂失
  • 微平台网站支持html5实现游戏火狐 wordpress
  • 如何创办网站网站加载慢图片做延时加载有用
  • 乐器网站模板wordpress 网页存在
  • 2016企业网站建设合同昭通学院教务管理系统
  • 中国物流网站wordpress获取自定义文章类型分类
  • 重庆响应式网站建设费用哪种语言的网站 做seo更好
  • 网站设计目标怎么写北京赵公口网站建设
  • 深圳市建设工程合同备案网站wordpress 太卡
  • 网站推广方案编写企业网站建完后没人
  • 网站外链发布电商网站购买的流程图
  • 营销型网站建设选择题前端做图表的网站
  • 设计一个网站要多少钱旅游网站源码下载
  • 建网络商城网站色块网站设计
  • 重庆建设公司网站国外做内容网站
  • 写作网站推荐17我们一起做网站
  • 云南工程建设信息网站温州logo设计公司
  • 网站方案报价wordpress 备份外链图片
  • 用易语言做网站如何展厅装修设计公司有
  • 游戏网站页面设计泰安网络信息化建设
  • 多媒体资源库网站建设永泰建设工程网站
  • 一般网站建设中的推广费用深圳网站优化方案
  • 装饰公司营销网站建设新会新闻官网
  • 企业简介 网站建设php做网站的公司有哪些
  • 做外汇消息面的网站如何做h5
  • 做幼儿网站的目标网页版梦幻西游礼包码
  • 怎样设计网站建设找网站的方法