【Leetcode_easy】605. Can Place Flowers
problem
题意:
solution1:
先通过简单的例子(比如000)发现,通过计算连续0的个数,然后直接算出能放花的个数,就必须要对边界进行处理,处理方法是如果首位置是0,那么前面再加上个0,如果末位置是0,就在最后面再加上个0。这样处理之后就默认连续0的左右两边都是1了,这样如果有k个连续0,那么就可以通过(k-1)/2来快速计算出能放的花的数量。
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
if(flowerbed.empty()) return false;
//if(flowerbed.front()==0) flowerbed.insert(flowerbed.begin(), 0);
if(flowerbed[]==) flowerbed.insert(flowerbed.begin(), );
//if(flowerbed.back()==0) flowerbed.insert(flowerbed.end(), 0);
if(flowerbed.back()==) flowerbed.push_back();
int sum = , cnt = ;
int len = flowerbed.size();
for(int i=; i<=len; i++)//err.
{
if(i<len && flowerbed[i]==) cnt++;//err.
else//need to compute sum when end with 0.
{
sum += (cnt-)/;
cnt = ;
}
}
return sum >= n;
}
};
参考
1. Leetcode_easy_605. Can Place Flowers;
2. Grandyang;
完
【Leetcode_easy】605. Can Place Flowers的更多相关文章
- 【Leetcode】605. Can Place Flowers
Description Suppose you have a long flowerbed in which some of the plots are planted and some are no ...
- 【LeetCode】605. Can Place Flowers 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 贪婪算法 日期 题目地址:https://leetcode.c ...
- 【Leetcode_easy】1021. Remove Outermost Parentheses
problem 1021. Remove Outermost Parentheses 参考 1. Leetcode_easy_1021. Remove Outermost Parentheses; 完
- 【Leetcode_easy】1022. Sum of Root To Leaf Binary Numbers
problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binar ...
- 【Leetcode_easy】1025. Divisor Game
problem 1025. Divisor Game 参考 1. Leetcode_easy_1025. Divisor Game; 完
- 【Leetcode_easy】1029. Two City Scheduling
problem 1029. Two City Scheduling 参考 1. Leetcode_easy_1029. Two City Scheduling; 完
- 【Leetcode_easy】1030. Matrix Cells in Distance Order
problem 1030. Matrix Cells in Distance Order 参考 1. Leetcode_easy_1030. Matrix Cells in Distance Orde ...
- 【Leetcode_easy】1033. Moving Stones Until Consecutive
problem 1033. Moving Stones Until Consecutive 参考 1. Leetcode_easy_1033. Moving Stones Until Consecut ...
- 【Leetcode_easy】1037. Valid Boomerang
problem 1037. Valid Boomerang 参考 1. Leetcode_easy_1037. Valid Boomerang; 完
随机推荐
- 基于pg_qualstats和hypopg的自动索引调优
pg-qualstats的安装和配置 1.安装pg-qualstats -pg-qualstats 2.将pg_qualstats和pg_stat_statements添加到shared_preloa ...
- msaa mrt load store action unity
unity buildin renderpipeline 和lightweight rp 对于开了msaa的rt 的load store action设置失效 buildin的时候set render ...
- 15 Vue计算属性和侦听器
计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的. 在模板中放入太多的逻辑会让模板过重且难以维护.例如: split = 字符中间空格分割, reverse= 反转 join('' ...
- 40、扩展原理-BeanDefinitionRegistryPostProcessor
40.扩展原理-BeanDefinitionRegistryPostProcessor BeanDefinitionRegistryPostProcessor extends BeanFactoryP ...
- Nginx 做JavaWeb负载均衡
随着用户量的增大,单台服务器已经满足不了用户的需求. 准备工作:安装 gcc.pcre-devel.zlib.OpenSSL 一下是在线 离线请戳这里 gcc 安装安装 nginx 需要先将官网下 ...
- PHP+下载文件夹
php下载文件我整理了这三种方法,和大家分享一下: 第一种:直接添加文件下载的绝对路径连接 //如:我有一个文件在demo.xx.cn/demo.zip<button> <a ...
- jQuery相关方法2
一.元素样式设置的方式(css,json键值对,链式编程) <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js ...
- serviceWorker
推荐阅读:Service Worker 简介 在 Service Worker 之前,我们一般用 AppCache 来实现离线体验(就是配置 Manifest 文件的方式),这个会有很多问题(博主曾尝 ...
- puppteer的使用
官方文档:Puppeteer 今天大概介绍一下我项目用到的puppeteer操作: // 启动浏览器 const browser = await puppeteer.launch({ executab ...
- 图灵学院java架构课程
1.wps文档地址 https://docs.qq.com/doc/DRVNLUndvTmFSdEhO 2.百度网盘地址 https://pan.baidu.com/s/1uxaTzJZHKrsw_H ...