File tree Expand file tree Collapse file tree 3 files changed +83
-0
lines changed Expand file tree Collapse file tree 3 files changed +83
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def trap (self , height ):
3
+ """
4
+ :type height: List[int]
5
+ :rtype: int
6
+ """
7
+ length = len (height )
8
+ if length == 0 or length == 1 :
9
+ return 0
10
+ slow = 0
11
+ fast = 1
12
+ total = 0
13
+ while fast < length :
14
+ # 每次更新stopPoint
15
+ stopPoint = fast
16
+ while fast < length and height [fast ] <= height [slow ]:
17
+ if height [fast ] > height [stopPoint ]:
18
+ stopPoint = fast
19
+ fast += 1
20
+ # 越界了要回到stopPoint
21
+ if fast >= length :
22
+ fast = stopPoint
23
+ tmp = 0
24
+ bottom = min (height [slow ], height [fast ])
25
+ for i in range (slow + 1 , fast ):
26
+ tmp += bottom - height [i ]
27
+ slow = fast
28
+ total += tmp
29
+ fast += 1
30
+ return total
Original file line number Diff line number Diff line change
1
+ # Definition for singly-linked list.
2
+ # class ListNode(object):
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.next = None
6
+
7
+
8
+ class Solution (object ):
9
+ def hasCycle (self , head ):
10
+ """
11
+ :type head: ListNode
12
+ :rtype: bool
13
+ """
14
+ slow = fast = head
15
+ while fast and fast .next :
16
+ slow = slow .next
17
+ fast = fast .next .next
18
+ if slow == fast :
19
+ return True
20
+
21
+ return False
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def summaryRanges (self , nums ):
3
+ """
4
+ :type nums: List[int]
5
+ :rtype: List[str]
6
+ """
7
+ length = len (nums )
8
+ if length == 0 :
9
+ return []
10
+ ans = []
11
+ count = 0
12
+ idx = 0
13
+ while idx + count < length - 1 :
14
+ if nums [idx + count ] == nums [idx + count + 1 ] - 1 :
15
+ count += 1
16
+ else :
17
+ string = ''
18
+ if count == 0 :
19
+ string = str (nums [idx ])
20
+ else :
21
+ string = str (nums [idx ]) + "->" + str (nums [idx + count ])
22
+ ans .append (string )
23
+ idx += count + 1
24
+ count = 0
25
+ # 末尾处理
26
+ string = ''
27
+ if count > 0 :
28
+ string = str (nums [idx ]) + "->" + str (nums [idx + count ])
29
+ else :
30
+ string = str (nums [idx ])
31
+ ans .append (string )
32
+ return ans
You can’t perform that action at this time.
0 commit comments