File tree Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ # 每次取出最大的两个元素相减,结果不为0则添加到列表中
2
+ class Solution (object ):
3
+ def lastStoneWeight (self , stones ):
4
+ while len (stones ) > 1 :
5
+ y = stones .pop (stones .index (max (stones )))
6
+ x = stones .pop (stones .index (max (stones )))
7
+ if x < y :
8
+ stones .append (y - x )
9
+ return stones [0 ] if stones else 0
Original file line number Diff line number Diff line change
1
+ # 前缀和解法,类似公交站记录上车人数和下车人数,每一站的人数为上一站的人数加上当前站的变化人数
2
+ class Solution (object ):
3
+ def corpFlightBookings (self , bookings , n ):
4
+ res = [0 ]* n
5
+ for b in bookings :
6
+ l , r = b [0 ]- 1 , b [1 ]
7
+ res [l ] += b [2 ]
8
+ if r < n :
9
+ res [r ] -= b [2 ]
10
+ for i in range (1 , n ):
11
+ res [i ] += res [i - 1 ]
12
+ return res
Original file line number Diff line number Diff line change
1
+ # 使用全局变量作为标记
2
+ class Foo (object ):
3
+ def __init__ (self ):
4
+ self .flag = 1
5
+
6
+ def first (self , printFirst ):
7
+ printFirst ()
8
+ self .flag = 2
9
+
10
+ def second (self , printSecond ):
11
+ while self .flag != 2 :
12
+ pass
13
+ printSecond ()
14
+ self .flag = 3
15
+
16
+ def third (self , printThird ):
17
+ while self .flag != 3 :
18
+ pass
19
+ printThird ()
You can’t perform that action at this time.
0 commit comments