problem

605. Can Place Flowers

题意:

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的更多相关文章

  1. 【Leetcode】605. Can Place Flowers

    Description Suppose you have a long flowerbed in which some of the plots are planted and some are no ...

  2. 【LeetCode】605. Can Place Flowers 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 贪婪算法 日期 题目地址:https://leetcode.c ...

  3. 【Leetcode_easy】1021. Remove Outermost Parentheses

    problem 1021. Remove Outermost Parentheses 参考 1. Leetcode_easy_1021. Remove Outermost Parentheses; 完

  4. 【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 ...

  5. 【Leetcode_easy】1025. Divisor Game

    problem 1025. Divisor Game 参考 1. Leetcode_easy_1025. Divisor Game; 完

  6. 【Leetcode_easy】1029. Two City Scheduling

    problem 1029. Two City Scheduling 参考 1. Leetcode_easy_1029. Two City Scheduling; 完

  7. 【Leetcode_easy】1030. Matrix Cells in Distance Order

    problem 1030. Matrix Cells in Distance Order 参考 1. Leetcode_easy_1030. Matrix Cells in Distance Orde ...

  8. 【Leetcode_easy】1033. Moving Stones Until Consecutive

    problem 1033. Moving Stones Until Consecutive 参考 1. Leetcode_easy_1033. Moving Stones Until Consecut ...

  9. 【Leetcode_easy】1037. Valid Boomerang

    problem 1037. Valid Boomerang 参考 1. Leetcode_easy_1037. Valid Boomerang; 完

随机推荐

  1. 靠边的列表如果没有设置margin-left:20px,那么是看不到列表序号的。

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. text-transform

    text-transform 语法: text-transform:none | capitalize | uppercase | lowercase |  默认值:none 适用于:所有元素 继承性 ...

  3. sql server 存储过程---游标的循环

    sqlserver中的循环遍历(普通循环和游标循环) sql 经常用到循环,下面介绍一下普通循环和游标循环 1.首先需要一个测试表数据Student

  4. 数据库学习之七--视图(View)

    一.定义 视图:指计算机数据库中的一个临时虚拟表,其内容由查询定义:同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在. 二.优点 1. 优点: a. ...

  5. 一个milller_rabin模板

    #include <iostream> #include <cstdlib> #include <stdio.h> #include <algorithm&g ...

  6. CSP初赛复习

    初赛复习 初赛一定要过啊,否则付出的那么多都白搭了! while(1) ++csp.rp,++csp.luck,++csp.scores; 历史 2020年开始,除NOIP以外的NOI系列其他赛事(包 ...

  7. 使用MockMVC与Junit进行单体测试

    1.pom.xml追加 junit spring-test 2.测试共通类 @ContextConfiguration(locations = { "classpath:springfram ...

  8. 如何查看Linux cpu核数、版本等信息

    CPU总核数 = 物理CPU个数 * 每颗物理CPU的核数 总逻辑CPU数 = 物理CPU个数 * 每颗物理CPU的核数 * 超线程数 1.查看CPU信息(型号): [root@iZ2ze1rl2qy ...

  9. vue tab嵌入iframe切换不刷新,相对完整的方案

    说到Vue的简单.便捷.高效,谁用谁喜欢,自然企业应用也来玩一把,三大经典组件:树控件,网格控件,选项卡控件: 本章先说选项卡tab控件的嵌入iframe. 本次主要解决以下问题: 1.tab控件混合 ...

  10. 综述类文章(Peng 等)阅读笔记Cross-media analysis and reasoning: advances and directions

    综述类文章 Cross-media analysis and reasoning: advances and directions Yu-xin PENG et al. Front Inform Te ...