Skip to content

Commit fe6e021

Browse files
authored
Update 2026-04-25-leetcode-python-general.md
1 parent eeaa48a commit fe6e021

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

_posts/2026-04-25-leetcode-python-general.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,38 @@ tags:
1313

1414
#### Array
1515
#### String
16+
1. list sort
17+
```python
18+
# in original list
19+
a = [3, 1, 2]
20+
a.sort()
21+
print(a) # [1, 2, 3]
22+
23+
# in new sorted list
24+
a = [3, 1, 2]
25+
b = sorted(a)
26+
print(b) # [1, 2, 3]
27+
print(a) # keep the same
28+
```
29+
30+
2. sort parameters
31+
32+
Using Timsort, which is a combination of insertion sort(small runs) and merge sort(merge sorted runs)
33+
34+
- time = best O(n), avg and worst O(nlogn)
35+
- space = O(n)
36+
37+
38+
```python
39+
# descending sort
40+
a.sort(reverse=True)
41+
42+
a = [(1, 3), (2, 1), (4, 2)]
43+
a.sort(key=lambda x: x[1]) # by second variable
44+
a = [(1, 3), (1, 2), (2, 1)]
45+
a.sort(key=lambda x: (x[0], x[1])) # by sort first variable and then second variable
46+
a.sort(key=lambda x: (x[0], -x[1]))
47+
```
1648
#### Binary Search
1749

1850
1. Find the index of the first appearance (nums[index] <= target)

0 commit comments

Comments
 (0)