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. urlrewrite与struts2结合使用基本配置

    1.更改web.xml,,,在struts2拦截器前面添加urlrewrite配置信息,,默认是forward的 <filter> <filter-name>UrlRewrit ...

  2. MOS管做开关之初理解

    杰杰 物联网IoT开发 2017-10-12 大家好,我是杰杰.       今晚,设计电路搞了一晚上,终于从模电渣渣的我,把MOS管理解了那么一丢丢,如有写的不好的地方,请指出来.谢谢.      ...

  3. element ui table的所有属性

    1. table 的props: data: { type: Array, default: function() { return []; } }, size: String, width: [St ...

  4. mongodb 开发规范

    一.命名规则 1.数据库命名规则 数据库名可以是满足以下条件的任意UTF-8字符串: (1)不能是空字符串(”") : (2)不能含有”(空格)...$./..和(空字符): (3)应全部小 ...

  5. SYSTEM_INFORMATION_CLASS

    source: https://github.com/processhacker/processhacker/blob/master/phnt/include/ntexapi.h // rev // ...

  6. Luogu5540 最小乘积生成树

    Luogu5540 最小乘积生成树 题目链接:洛谷 题目描述:对于一个\(n\)个点\(m\)条边的无向连通图,每条边有两个边权\(a_i,b_i\),求使\((\sum a_i)\times (\s ...

  7. 数据结构实验之图论四:迷宫探索【dfs 求路径】

    分析:起点已知,开个数组来存放路径,注意 vis 数组要初始化!另外,不能忘记了题目还要求回去的路径,只要在 dfs 之后加上就可以了. #include <bits/stdc++.h> ...

  8. 数据结构实验之图论三:判断可达性(SDUT 2138)(简单DFS)

    #include <bits/stdc++.h> using namespace std; int gra[1002][1005]; int vis[1002]; int n,m; voi ...

  9. Python -三目运算(三元运算)

    if 1==1: name = "jacky" else: name = "zhuyuanlu" name = 值1 if 条件 else 值2 #如果这个条件 ...

  10. decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值)

    decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下: IF 条件=值1 THEN RETURN(翻译值1) ELSIF 条件=值2 THEN RETU ...