【Leetcode_easy】605. Can Place Flowers】的更多相关文章

problem 605. Can Place Flowers 题意: solution1: 先通过简单的例子(比如000)发现,通过计算连续0的个数,然后直接算出能放花的个数,就必须要对边界进行处理,处理方法是如果首位置是0,那么前面再加上个0,如果末位置是0,就在最后面再加上个0.这样处理之后就默认连续0的左右两边都是1了,这样如果有k个连续0,那么就可以通过(k-1)/2来快速计算出能放的花的数量. class Solution { public: bool canPlaceFlowers(…
Description Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die. Given a flowerbed (represented as an array…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 贪婪算法 日期 题目地址:https://leetcode.com/problems/can-place-flowers/description/ 题目描述 Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, fl…
problem 1021. Remove Outermost Parentheses 参考 1. Leetcode_easy_1021. Remove Outermost Parentheses; 完…
problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binary Numbers; 完…
problem 1025. Divisor Game 参考 1. Leetcode_easy_1025. Divisor Game; 完…
problem 1029. Two City Scheduling 参考 1. Leetcode_easy_1029. Two City Scheduling; 完…
problem 1030. Matrix Cells in Distance Order 参考 1. Leetcode_easy_1030. Matrix Cells in Distance Order; 完…
problem 1033. Moving Stones Until Consecutive 参考 1. Leetcode_easy_1033. Moving Stones Until Consecutive; 完…
problem 1037. Valid Boomerang 参考 1. Leetcode_easy_1037. Valid Boomerang; 完…
problem 1042. Flower Planting With No Adjacent 参考 1. Leetcode_easy_1042. Flower Planting With No Adjacent; 完…
problem 1046. Last Stone Weight 参考 1. Leetcode_easy_1046. Last Stone Weight; 完…
problem 1047. Remove All Adjacent Duplicates In String 参考 1. Leetcode_easy_1047. Remove All Adjacent Duplicates In String; 完…
problem 1051. Height Checker solution class Solution { public: int heightChecker(vector<int>& heights) { vector<int> orders = heights; sort(orders.begin(), orders.end()); ; ; i<heights.size(); i++) { if(heights[i]!=orders[i]) res++; } r…
problem 1071. Greatest Common Divisor of Strings solution class Solution { public: string gcdOfStrings(string str1, string str2) { return (str1+str2==str2+str1) ? (str1.substr(, gcd(str1.size(), str2.size()))) : ""; } }; 参考 1. Leetcode_easy_1071…
problem 1154. Day of the Year solution class Solution { public: int dayOfYear(string date) { // 平年 闰年 ] = {, , , , , , , , , , , }; , )), m = stoi(date.substr(, )), d = stoi(date.substr()); && y % == && (y % != || y % == )) d++;//leap year…
problem 1170. Compare Strings by Frequency of the Smallest Character 参考 1. Leetcode_easy_1170. Compare Strings by Frequency of the Smallest Character; 完…
problem 1160. Find Words That Can Be Formed by Characters solution class Solution { public: int countCharacters(vector<string>& words, string chars) { ; unordered_map<char, int> charmap; for(auto ch:chars) charmap[ch]++; for(auto word:word…
problem 997. Find the Town Judge solution: class Solution { public: int findJudge(int N, vector<vector<int>>& trust) { vector<); ]]--; balance[t[]]++; } ; i<=N; i++) { ) return i; } ; } }; 参考 1. Leetcode_easy_997. Find the Town Judge…
problem 994. Rotting Oranges 参考 1. Leetcode_easy_994. Rotting Oranges; 完…
problem 993. Cousins in Binary Tree 参考 1. Leetcode_easy_993. Cousins in Binary Tree; 完…
problem 989. Add to Array-Form of Integer 参考 1. Leetcode_easy_989. Add to Array-Form of Integer; 完…
problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) { vector<int> res; ; ==) sum +=a; for(auto query:queries) { ]]%== )…
problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSquares(vector<int>& A) { for(auto &a:A) a *=a; sort(A.begin(), A.end()); return A; } }; 参考1. Leetcode_easy_977. Squares of a Sorted Array; 完…
problem 976. Largest Perimeter Triangle solution: class Solution { public: int largestPerimeter(vector<int>& A) { sort(A.begin(), A.end());//decrease. ; i>; i--) { ]+A[i-]) ]+A[i-]; } ; } }; 参考 1. Leetcode_easy_976. Largest Perimeter Triangle…
problem 970. Powerful Integers solution: class Solution { public: vector<int> powerfulIntegers(int x, int y, int bound) { unordered_set<int> tmp; ; a<bound; a*=x)// { ; a+b<=bound; b*=y) { tmp.insert(a+b);// ) break;// } ) break; } retur…
problem 965. Univalued Binary Tree 参考 1. Leetcode_easy_965. Univalued Binary Tree; 完…
problem 1137. N-th Tribonacci Number solution: class Solution { public: int tribonacci(int n) { ) ; || n==) ; , t1 = , t2 = , t3 = ; ; i<=n; i++) { t3 = t0+t1+t2; t0 = t1; t1 = t2; t2 = t3; } return t3; } };     参考 1. Leetcode_easy_1137. N-th Tribona…
problem 961. N-Repeated Element in Size 2N Array solution: class Solution { public: int repeatedNTimes(vector<int>& A) { unordered_map<int, int> amap; for(auto a:A) { amap[a]++; } , max_val; for(auto a:amap) { if(a.second>max_num) { max…
problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vector<string>& words, string order) { unordered_map<char, int> alien_order; ; i<order.size(); i++) { alien_order[order[i]-'a'] = i; } for(…