【LeetCode】905. Sort Array By Parity 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/sort-array-by-parity/description/
题目描述:
Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.
You may return any answer array that satisfies this condition.
Example 1:
Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Note:
- 1 <= A.length <= 5000
- 0 <= A[i] <= 5000
题目大意
对数组A按照偶数和奇数重新排序,使得偶数在前,奇数在后。可以返回任何一种满足这个条件的数组即可。
解题方法
自定义sorted函数的cmp
看到排序就写个排序就行了,比较的key是对2取余数之后的值。这样偶数取余得0,排在前面,奇数取余得1,排在后面。
时间复杂度是O(nlogn),空间复杂度是O(1)。
代码如下:
class Solution(object):
def sortArrayByParity(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
return sorted(A, key = lambda x : x % 2)
参考资料:
日期
2018 年 9 月 17 日 —— 早上很凉,夜里更凉
【LeetCode】905. Sort Array By Parity 解题报告(Python)的更多相关文章
- LeetCode 905 Sort Array By Parity 解题报告
题目要求 Given an array A of non-negative integers, return an array consisting of all the even elements ...
- LeetCode 905. Sort Array By Parity
905. Sort Array By Parity Given an array A of non-negative integers, return an array consisting of a ...
- [LeetCode] 905. Sort Array By Parity 按奇偶排序数组
Given an array A of non-negative integers, return an array consisting of all the even elements of A, ...
- LeetCode 905. Sort Array By Parity 按奇偶校验排列数组
题目 Given an array A of non-negative integers, return an array consisting of all the even elements of ...
- [leetcode] 905. Sort Array By Parity [easy]
原题链接 很水的一道题,就是数组内部交换. 水题就想着减少复杂度嘛,于是学到一种交换写法. class Solution { public: vector<int> sortArrayBy ...
- 【LEETCODE】41、905. Sort Array By Parity
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 905. Sort Array By Parity - LeetCode
Question 905. Sort Array By Parity Solution 题目大意:数组排序,偶数放前,奇数在后,偶数的数之间不用管顺序,奇数的数之间也不用管顺序 思路:建两个list, ...
- 【Leetcode_easy】905. Sort Array By Parity
problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...
- LeetCode 922. Sort Array By Parity II C++ 解题报告
922. Sort Array By Parity II 题目描述 Given an array A of non-negative integers, half of the integers in ...
随机推荐
- PC端申请表
公司项目需求中要做用html做一个PDF申请表的样式出来.有点意思,贴上来大家看看. 先上效果图: 附上源代码: HTML:<div id="form"> <h2 ...
- 使用Rainbond实现离线环境软件交付
一.离线交付的痛点 在传统行业,如政府.能源.军工.公安.工业.交通等行业,为了防止数据泄露和运行安全考虑,一般情况下网络会采取内外网隔离的策略,以防范不必要的风险,毕竟在安全防护方面,网络物理隔离是 ...
- 表格合并单元格【c#】
gridBranchInfo.DataSource = dtBranchViewList; gridBranchInfo.DataBind(); Random random = new Random( ...
- Git提交规范
Commit message 的格式 每次提交,Commit message 都包括三个部分:Header,Body 和 Footer. <type>(<scope>): &l ...
- 2021广东工业大学十月月赛 F-hnjhd爱序列
题目:GDUTOJ | hnjhd爱序列 (gdutcode.cn) 一开始是用双指针从尾至头遍历,但发现会tle!! 后来朋友@77给出了一种用桶的做法,相当于是用空间换时间了. 其中用到的一个原理 ...
- Oracle中的索引
1.Oracle 索引简介 在Oracle数据库中,存储的每一行数据都有一个rowID来标识.当Oracle中存储着大量的数据时,意味着有大量的rowID,此时想要快速定位指定的rowID, ...
- winXP 下安装python3.3.2
1. 安装python-3.3.2 2. 安装setuptools 下载解压后,进入路径 python setup.py install 3.安装pip 下载解压后,进入路径 python setup ...
- EasyExcel读写Excel
使用过 poi 的开发同学可能都有此体会,每次都要写一坨代码,最后的代码如下面一样: 这样的代码是不是又臭又长?当字段数量多的时候,一不小心还容易写错.阿粉还记得当初使用 poi 导出一个二十多字段的 ...
- JmxTest
package mbeanTest; import java.util.Set; import javax.management.Attribute; import javax.management. ...
- Mybatis中 SIMPLE、REUSE、BATCH的区别
Executor分成两大类,一类是CacheExecutor,另一类是普通Executor. 普通类又分为: ExecutorType.SIMPLE: 这个执行器类型不做特殊的事情.它为每个语句的执行 ...