Skip to content

Commit f395e60

Browse files
committedSep 30, 2024
new codes
1 parent 2a1f2cb commit f395e60

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
 

‎binary_search/first_last_position.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
2+
3+
# Idea: here is to find the lower bound and then iterate from lower bound to find the index when we stop getting target element.
4+
5+
6+
def searchRange(nums, target):
7+
8+
def getLowerBound():
9+
10+
l, r = 0, len(nums) - 1
11+
12+
while l < r:
13+
m = (l + r) // 2
14+
15+
if target <= nums[m]:
16+
r = m
17+
else:
18+
l = m + 1
19+
return l
20+
21+
l = getLowerBound()
22+
res = -1
23+
24+
for i in range(l, len(nums)):
25+
if nums[i] != target:
26+
break
27+
res = i
28+
29+
return [l, res] if res != -1 else [-1, -1]

‎linkedlist/remove_duplicate.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def deleteDuplicates(head):
2+
cur = head
3+
while cur:
4+
while cur.next and cur.next.val == cur.val:
5+
cur.next = cur.next.next
6+
cur = cur.next
7+
8+
return head

0 commit comments

Comments
 (0)
Please sign in to comment.