Skip to content

Commit 33d4936

Browse files
authored
docs: add a description of the solution to lcof problem: No.21 (doocs#709)
面试题21. 调整数组顺序使奇数位于偶数前面
1 parent 333b955 commit 33d4936

File tree

1 file changed

+22
-1
lines changed
  • lcof/面试题21. 调整数组顺序使奇数位于偶数前面

1 file changed

+22
-1
lines changed

lcof/面试题21. 调整数组顺序使奇数位于偶数前面/README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,28 @@
1919

2020
## 解法
2121

22-
双指针。
22+
**双指针**
23+
24+
定义两个指针,分别指向数组左右边缘。
25+
- 查看左指针所指向的元素。
26+
- 若为 **奇数**,则左指针往右移动。
27+
- 若为 **偶数**,则与右指针交换元素,并将右指针往左移动。
28+
- 重复该过程,直到左指针超过右指针。
29+
30+
```txt
31+
EXCHANGE(n)
32+
l = 0
33+
r = n.length - 1
34+
while l < r
35+
if n[l] % 2 == 0
36+
t = n[l]
37+
n[l] = n[r]
38+
n[r] = t
39+
r--
40+
else
41+
l++
42+
return n
43+
```
2344

2445
<!-- tabs:start -->
2546

0 commit comments

Comments
 (0)