Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e70709e

Browse files
Merge pull request SharingSource#487 from SharingSource/ac_oier
✨feat: Add 905
2 parents ef00854 + 82a2eb1 commit e70709e

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

‎Index/双指针.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
| [832. 翻转图像](https://leetcode-cn.com/problems/flipping-an-image/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/flipping-an-image/solution/shuang-zhi-zhen-yi-bian-chu-li-huan-you-ik0v1/) | 简单 | 🤩🤩 |
3131
| [838. 推多米诺](https://leetcode-cn.com/problems/push-dominoes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/push-dominoes/solution/gong-shui-san-xie-yi-ti-shuang-jie-bfs-y-z52w/) | 中等 | 🤩🤩🤩🤩 |
3232
| [881. 救生艇](https://leetcode-cn.com/problems/boats-to-save-people/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/boats-to-save-people/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-hosg8/) | 中等 | 🤩🤩🤩🤩 |
33+
| [905. 按奇偶排序数组](https://leetcode-cn.com/problems/sort-array-by-parity/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sort-array-by-parity/solution/by-ac_oier-nuz7/) | 简单 | 🤩🤩🤩🤩 |
3334
| [917. 仅仅反转字母](https://leetcode-cn.com/problems/reverse-only-letters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-only-letters/solution/gong-shui-san-xie-jian-dan-shuang-zhi-zh-xrpt/) | 简单 | 🤩🤩🤩🤩 |
3435
| [930. 和相同的二元子数组](https://leetcode-cn.com/problems/binary-subarrays-with-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-subarrays-with-sum/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-hfoc0/) | 中等 | 🤩🤩🤩 |
3536
| [992. K 个不同整数的子数组](https://leetcode-cn.com/problems/subarrays-with-k-different-integers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/subarrays-with-k-different-integers/solution/miao-dong-xi-lie-xiang-jie-shuang-zhi-zh-9k8w/) | 困难 | 🤩🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
| [883. 三维形体投影面积](https://leetcode-cn.com/problems/projection-area-of-3d-shapes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/projection-area-of-3d-shapes/solution/by-ac_oier-r6hj/) | 简单 | 🤩🤩🤩🤩 |
101101
| [884. 两句话中的不常见单词](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-wwam/) | 简单 | 🤩🤩🤩🤩 |
102102
| [896. 单调数列](https://leetcode-cn.com/problems/monotonic-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/monotonic-array/solution/wei-shi-yao-yi-ci-bian-li-yao-bi-liang-c-uglp/) | 简单 | 🤩🤩🤩🤩 |
103+
| [905. 按奇偶排序数组](https://leetcode-cn.com/problems/sort-array-by-parity/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sort-array-by-parity/solution/by-ac_oier-nuz7/) | 简单 | 🤩🤩🤩 |
103104
| [997. 找到小镇的法官](https://leetcode-cn.com/problems/find-the-town-judge/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-town-judge/solution/gong-shui-san-xie-jian-dan-chu-du-ru-du-5ms57/) | 简单 | 🤩🤩🤩🤩 |
104105
| [1001. 网格照明](https://leetcode-cn.com/problems/grid-illumination/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/grid-illumination/solution/gong-shui-san-xie-ha-xi-biao-xian-ying-s-s48d/) | 困难 | 🤩🤩🤩🤩 |
105106
| [1005. K 次取反后最大化的数组和](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/solution/gong-shui-san-xie-jian-dan-fen-qing-kuan-6qwu/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[905. 按奇偶排序数组](https://leetcode-cn.com/problems/sort-array-by-parity/solution/by-ac_oier-nuz7/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」、「双指针」
6+
7+
8+
9+
给你一个整数数组 `nums`,将 `nums` 中的的所有偶数元素移动到数组的前面,后跟所有奇数元素。
10+
11+
返回满足此条件的 任一数组 作为答案。
12+
13+
示例 1:
14+
```
15+
输入:nums = [3,1,2,4]
16+
17+
输出:[2,4,3,1]
18+
19+
解释:[4,2,3,1]、[2,4,1,3] 和 [4,2,1,3] 也会被视作正确答案。
20+
```
21+
示例 2:
22+
```
23+
输入:nums = [0]
24+
25+
输出:[0]
26+
```
27+
28+
提示:
29+
* 1ドル <= nums.length <= 5000$
30+
* 0ドル <= nums[i] <= 5000$
31+
32+
---
33+
34+
### 模拟
35+
36+
根据题意进行模拟即可:使用指针 $i$ 和 $j$ 分别代表未处理区间的左右端点,当 $nums[i]$ 不为偶数时,将 $i$ 和 $j$ 两个位置互换,原有位置 $j$ 边是奇数(已处理好),让 $j$ 自减左移,但原有位置 $i$ 交换后不确保是偶数,需要再次检查。
37+
38+
代码:
39+
```Java
40+
class Solution {
41+
public int[] sortArrayByParity(int[] nums) {
42+
int n = nums.length;
43+
for (int i = 0, j = n - 1; i < j; i++) {
44+
if (nums[i] % 2 == 1) {
45+
int c = nums[j];
46+
nums[j--] = nums[i];
47+
nums[i--] = c;
48+
}
49+
}
50+
return nums;
51+
}
52+
}
53+
```
54+
* 时间复杂度:$O(n)$
55+
* 空间复杂度:$O(1)$
56+
57+
---
58+
59+
### 最后
60+
61+
这是我们「刷穿 LeetCode」系列文章的第 `No.905` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
62+
63+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
64+
65+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
66+
67+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
68+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /