【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; 完
随机推荐
- linux 安装MySql 5.7.20 操作步骤【亲测】
一. #卸载系统自带的Mariadb[root@master ~]# rpm -qa|grep mariadbmariadb-libs-5.5.44-2.el7.centos.x86_64[root@ ...
- Spring MVC 学习笔记(一)
• 1.SpringMVC概述 • 2.SpringMVC的HelloWorld • 3.使用@RequestMapping映射请求 • 4.映射请求参数&请求头 • 5.处理模型数据 • 6 ...
- Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes
链接: https://codeforces.com/contest/1251/problem/B 题意: A palindrome is a string t which reads the sam ...
- 47、[源码]-Spring容器创建-初始化MessageSource
47.[源码]-Spring容器创建-初始化MessageSource 7.initMessageSource();初始化MessageSource组件(做国际化功能:消息绑定,消息解析): 获取Be ...
- Appium自动化测试教程-自学网-monkeyrunner API
MonkeyRunner API MonkeyRunner工具主要有三个类: MonkeyRunner MonkeyDevice MonkeyImage 官方API文档 :http://www.and ...
- 学到了林海峰,武沛齐讲的Day26 反射 组合的方式完成授
class BlackMedium: feature='Ugly' def __init__(self,name,addr): self.name=name self.addr=addr def se ...
- ftp连接
package enterprise.celerity.ac.util; import java.io.IOException;import java.io.InputStream;import ja ...
- hdu6731 Angle Beats(ccpc秦皇岛A,计算几何)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6731 题意: 给出$n$个点,有$q$次询问 每次询问给出一个点$b$,求这$n+1$个点,组成直角 ...
- MySQL表连接
有3种: JOIN 按照功能大致分为如下三类: CROSS JOIN (交叉连接) INNER JOIN (内连接或等值连接). OUTER JOIN (外连接) 交叉连接CROSS JOIN 交叉连 ...
- CSS 交集选择器和并集选择器
交集选择器是and 也就是要同时满足 且只能交2个只能交2个只能交2个,第一个是标记,第二个是class或者id,之间不可以有空格 eg: span.small-height 并集选择器是or,也就 ...