diff --git "a/Index/\345\217\214\346\214\207\351\222\210.md" "b/Index/\345\217\214\346\214\207\351\222\210.md" index 60601545..0424bf14 100644 --- "a/Index/\345\217\214\346\214\207\351\222\210.md" +++ "b/Index/\345\217\214\346\214\207\351\222\210.md" @@ -30,6 +30,7 @@ | [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/) | 简单 | 🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | +| [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩 | | [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/) | 困难 | 🤩🤩🤩🤩 | diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index 866d3b19..eb61741a 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -100,6 +100,7 @@ | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | +| [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/) | 简单 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 困难 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/901-910/905. \346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/901-910/905. \346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..ba97ca3c --- /dev/null +++ "b/LeetCode/901-910/905. \346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,68 @@ +### 题目描述 + +这是 LeetCode 上的 **[905. 按奇偶排序数组](https://leetcode-cn.com/problems/sort-array-by-parity/solution/by-ac_oier-nuz7/)** ,难度为 **简单**。 + +Tag : 「模拟」、「双指针」 + + + +给你一个整数数组 `nums`,将 `nums` 中的的所有偶数元素移动到数组的前面,后跟所有奇数元素。 + +返回满足此条件的 任一数组 作为答案。 + +示例 1: +``` +输入:nums = [3,1,2,4] + +输出:[2,4,3,1] + +解释:[4,2,3,1]、[2,4,1,3] 和 [4,2,1,3] 也会被视作正确答案。 +``` +示例 2: +``` +输入:nums = [0] + +输出:[0] +``` + +提示: +* $1 <= nums.length <= 5000$ +* $0 <= nums[i] <= 5000$ + +--- + +### 模拟 + +根据题意进行模拟即可:使用指针 $i$ 和 $j$ 分别代表未处理区间的左右端点,当 $nums[i]$ 不为偶数时,将 $i$ 和 $j$ 两个位置互换,原有位置 $j$ 边是奇数(已处理好),让 $j$ 自减左移,但原有位置 $i$ 交换后不确保是偶数,需要再次检查。 + +代码: +```Java +class Solution { + public int[] sortArrayByParity(int[] nums) { + int n = nums.length; + for (int i = 0, j = n - 1; i < j; i++) { + if (nums[i] % 2 == 1) { + int c = nums[j]; + nums[j--] = nums[i]; + nums[i--] = c; + } + } + return nums; + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(1)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.905` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +