LeetCode C++13-接雨水

题目描述

        给定n个非负整数表示每个宽度为1的柱子的高度图就,计算按此排列的柱子,下雨之后能接多少雨水。

举例

上图浅绿的表示柱子的高度,深蓝色的表示最多能接的雨水量。      

 输入:nums={3, 1, 0, 2, 1}       输出:3

解题思路:双指针。左右两个指针left和right同时扫描数组,用两个变量记录当前左右两边的最大值lmax和rmax,然后比较lmax和rmax,将小一边计算当前的接雨量,同时更新对应的运行指针。

代码

#include <iostream>
#include <vector>

int get_water_num(const std::vector<int>& nums) {
	int n = nums.size();
	int water_num = 0;
	int lmax = 0;
	int rmax = 0;
	int left = 0;
	int right = n - 1;
	while (left <= right) {
		lmax = std::max(nums[left], lmax);
		rmax = std::max(nums[right], rmax);
		if (lmax <= rmax) {
			water_num += (lmax - nums[left]);
			left++;
		} else {
			water_num += (rmax - nums[right]);
			right--;
		}
	}
	return water_num;
}

void print_result(const std::vector<int>& input, int output) {
	// input
	std::cout << "input:";
	for (auto value : input) {
		std::cout << value << " ";
	}
	std::cout << std::endl;
	// output
	std::cout << "output:" << output << std::endl;
	
}
int main()
{
	// case1: nums={0,1,0,2,1,0,1,3,2,1,2,1} ==> 6
	auto nums = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
	auto result = get_water_num(nums);
	print_result(nums, result);
	// case2: nums={4,2,0,3,2,5} ==> 9
	nums = {4, 2, 0, 3, 2, 5};
	result = get_water_num(nums);
	print_result(nums, result);
	// case3: nums = {3, 1, 0, 2, 1}
	nums = {3, 1, 0, 2, 1};
	result = get_water_num(nums);
	print_result(nums, result);
    return 0;
}

代码运行结果如下:

input:0 1 0 2 1 0 1 3 2 1 2 1 
output:6
input:4 2 0 3 2 5 
output:9
input:3 1 0 2 1 
output:3

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值