Skip to content

Commit 28bc67a

Browse files
author
robot
committed
2 parents 2032394 + a889eb0 commit 28bc67a

4 files changed

+56
-14
lines changed

problems/1015.smallest-integer-divisible-by-k.md

+3-11
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ https://leetcode-cn.com/problems/smallest-integer-divisible-by-k/
4646

4747
这道题是说给定一个 K 值,能否找到一个形如 1,11,111,1111 。。。 的数字 n,使得 n % K == 0,并要求 n 尽可能地小。
4848

49-
由于题目要找一个尽可能小的 n ,那么我们可以从小到大进行枚举,知道找到这样的一个 n 值即可。即从 1,11,111,1111 。。。 这样一直除下去,直到碰到可以整除的,我们返回即可。
49+
由于题目要找一个尽可能小的 n ,那么我们可以从小到大进行枚举,直到找到这样的一个 n 值即可。即从 1,11,111,1111 。。。 这样一直除下去,直到碰到可以整除的,我们返回即可。
5050

51-
但是如果这个数字根本就无法整除怎么办?没错,我们会无限循环下去。我们应该在什么时刻跳出循环返回 - 1 (表示不能整除)呢?
51+
但是如果这个数字根本就无法整除怎么办?没错,我们会无限循环下去。那么我们应该在什么时刻跳出循环返回 - 1 (表示不能整除)呢?
5252

5353
比如 k = 2 来说我们的算法过程如下:
5454

@@ -78,15 +78,7 @@ https://leetcode-cn.com/problems/smallest-integer-divisible-by-k/
7878

7979
## 代码
8080

81-
```python
82-
#
83-
# @lc app=leetcode.cn id=1015 lang=python3
84-
#
85-
# [1015] 可被 K 整除的最小整数
86-
#
87-
88-
# @lc code=start
89-
81+
```py
9082

9183
class Solution:
9284
def smallestRepunitDivByK(self, K: int) -> int:

problems/437.path-sum-iii.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ var pathSum = function (root, sum) {
117117

118118
## 代码
119119

120-
- 语言支持:JS
120+
- 语言支持:JS, Python
121121

122+
JS code:
122123
```js
123124
/*
124125
* @lc app=leetcode id=437 lang=javascript
@@ -164,6 +165,39 @@ var pathSum = function (root, sum) {
164165
};
165166
```
166167

168+
Python Code:
169+
```python
170+
import collections
171+
'''
172+
class TreeNode:
173+
def __init__(self, val=0, left=None, right=None):
174+
self.val = val
175+
self.left = left
176+
self.right = right
177+
'''
178+
class Solution:
179+
def helper(self,root,acc,target,hashmap):
180+
if not root:
181+
return 0
182+
count=0
183+
acc+=root.val
184+
if acc==target:
185+
count+=1
186+
if acc-target in hashmap:
187+
count+=hashmap[acc-target]
188+
hashmap[acc]+=1
189+
if root.left:
190+
count+=self.helper(root.left,acc,target,hashmap)
191+
if root.right:
192+
count+=self.helper(root.right,acc,target,hashmap)
193+
hashmap[acc]-=1
194+
return count
195+
196+
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:
197+
hashmap=collections.defaultdict(lambda:0)
198+
return self.helper(root,0,targetSum,hashmap)
199+
```
200+
167201
**复杂度分析**
168202

169203
- 时间复杂度:$O(N)$

problems/455.AssignCookies.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ https://leetcode-cn.com/problems/assign-cookies/
6161

6262
## 代码
6363

64-
语言支持:JS
64+
语言支持:JS, Python
6565

66+
JS Code:
6667
```js
6768
/**
6869
* @param {number[]} g
@@ -90,6 +91,21 @@ const findContentChildren = function (g, s) {
9091
};
9192
```
9293

94+
Python Code:
95+
```python
96+
class Solution:
97+
def findContentChildren(self, g: List[int], s: List[int]) -> int:
98+
g.sort()
99+
s.sort()
100+
count=gIdx=sIdx=0
101+
while gIdx<len(g) and sIdx<len(s):
102+
if s[sIdx]>=g[gIdx]:
103+
gIdx+=1
104+
count+=1
105+
sIdx+=1
106+
return count
107+
```
108+
93109
***复杂度分析***
94110

95111
- 时间复杂度:由于使用了排序,因此时间复杂度为 O(NlogN)

problems/84.largest-rectangle-in-histogram.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class Solution:
143143

144144
实际上,读完第二种方法的时候,你应该注意到了。我们的核心是求左边第一个比 i 小的和右边第一个比 i 小的。 如果你熟悉单调栈的话,那么应该会想到这是非常适合使用单调栈来处理的场景。
145145

146-
从左到右遍历柱子,对于每一个柱子,我们想找到第一个高度小于它的柱子,那么我们就可以使用一个单调递增栈来实现。 如果柱子大于栈顶的柱子,那么说明不是我们要找的柱子,我们把它塞进去继续遍历,如果比栈顶小,那么我们就找到了第一个小于的柱子。 **对于栈顶元素,其右边第一个小于它的就是当前遍历到的柱子,左边第一个小于它的就是栈中下一个要被弹出的元素**,因此以当前栈顶为最小柱子的面积为**当前栈顶的柱子高度 \* (当前遍历到的柱子索引 - 1 - (栈中下一个要被弹出的元素索引 + 1) + 1)**,化简一下就是 **当前栈顶的柱子高度 \* (当前遍历到的柱子索引 - 栈中下一个要被弹出的元素索引 - 1)**
146+
从左到右遍历柱子,对于每一个柱子,我们想找到第一个高度小于它的柱子,那么我们就可以使用一个单调递减栈来实现。 如果柱子大于栈顶的柱子,那么说明不是我们要找的柱子,我们把它塞进去继续遍历,如果比栈顶小,那么我们就找到了第一个小于的柱子。 **对于栈顶元素,其右边第一个小于它的就是当前遍历到的柱子,左边第一个小于它的就是栈中下一个要被弹出的元素**,因此以当前栈顶为最小柱子的面积为**当前栈顶的柱子高度 \* (当前遍历到的柱子索引 - 1 - (栈中下一个要被弹出的元素索引 + 1) + 1)**,化简一下就是 **当前栈顶的柱子高度 \* (当前遍历到的柱子索引 - 栈中下一个要被弹出的元素索引 - 1)**
147147

148148
这种方法只需要遍历一次,并用一个栈。由于每一个元素最多进栈出栈一次,因此时间和空间复杂度都是$O(N)$。
149149

0 commit comments

Comments
 (0)