mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #314 from morningsky/master
添加 0455.分发饼干 0376.摆动序列 python3版本
This commit is contained in:
@ -138,7 +138,21 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def wiggleMaxLength(self, nums: List[int]) -> int:
|
||||||
|
#贪心 求波峰数量 + 波谷数量
|
||||||
|
if len(nums)<=1:
|
||||||
|
return len(nums)
|
||||||
|
cur, pre = 0,0 #当前一对差值,前一对差值
|
||||||
|
count = 1#默认最右边有一个峰值
|
||||||
|
for i in range(len(nums)-1):
|
||||||
|
cur = nums[i+1] - nums[i]
|
||||||
|
if((cur>0 and pre<=0) or (cur<0 and pre>=0)):
|
||||||
|
count += 1
|
||||||
|
pre = cur
|
||||||
|
return count
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
@ -134,7 +134,21 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def findContentChildren(self, g: List[int], s: List[int]) -> int:
|
||||||
|
#先考虑胃口小的孩子
|
||||||
|
g.sort()
|
||||||
|
s.sort()
|
||||||
|
i=j=0
|
||||||
|
count = 0
|
||||||
|
while(i<len(g) and j<len(s)):
|
||||||
|
if g[i]<=s[j]:
|
||||||
|
count+=1
|
||||||
|
i+=1 #如果满足了,则继续遍历下一个孩子和下一块饼干
|
||||||
|
j += 1 #如果最小的饼干没有满足当前最小胃口的孩子,则遍历下一块饼干
|
||||||
|
return count
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user