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, followed by all the odd elements of A.
You may return any answer array that satisfies this condition.
题目分析及思路
题目给出一个含有非负整数的数组A,要求返回一个数组,前面的元素都是A中的偶数,后面的元素都是A中的奇数。可以新建一个list,先遍历A一次,把偶数元素都插入到里面;再遍历A一次,这次只插入奇数元素。
python代码
class Solution:
def sortArrayByParity(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
res = list()
for e in A:
if e % 2 == 0:
res.append(e)
for e in A:
if e % 2 == 1:
res.append(e)
return res
LeetCode 905 Sort Array By Parity 解题报告的更多相关文章
- 【LeetCode】905. Sort Array By Parity 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 自定义sorted函数的cmp 日期 题目地址:h ...
- 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 ...
随机推荐
- Y460 安装ubuntu 12.04系统黑屏,登录界面黑屏
ubuntu 12.04系统黑屏,登录界面黑屏,但是命令行界面可以登录,也可以正常使用,当时在装CVS,装完重启就这样了,可能是因为前一天装更新时,突然断电导致图形界面损坏,参考他人方法,终于修复,总 ...
- 牛客网_Go语言相关练习_判断&选择题(4)
题目来源于牛客网 一.判断题 成员变量或者函数的首字母表示是否对外部可见. switch后面的声明语句和表达式语句都是可以选择的.例如: //可以什么都不加 switch: break; 错误指的是可 ...
- plsql 常用函数-转
PLSQL常用函数 1)处理字符的函数 || 或 CONCAT---并置运算符. 格式∶CONCAT(STRING1, STRING2) 例:’ABC’|| ’DE’=’ABCDE’ CONCAT(‘ ...
- 和我一起学Effective Java之创建和销毁对象
前言 主要学习创建和销毁对象: 1.何时以及如何创建对象 2.何时以及如何避免创建对象 3.如何确保它们能够适时地销毁 4.如何管理对象销毁之前必须进行的清理动作 正文 一.用静态工厂方法代替构造器 ...
- 用Physijs在场景中添加物理效果
1.创建可用Physijs的基本Three.js场景 创建一个可用Physijs的Three.js场景非常简单,只要几个步骤即可.首先我们要包含正确的文件, 需要引入physi.js文件.实际模拟物理 ...
- [Python] 00 - Books
A.I. & Optimization Advanced Machine Learning, Data Mining, and Online Advertising Services Ref: ...
- Mac 环境 Vue 开发 CPU 占用率高 问题
Mac开发Vue应用时,发现CPU风扇转的老高. htop查看一下: 问题找到了,就是这个dev-server.js,node起的进程. 然后就是 dtruss -p 1230(进程ID) 命名跟踪一 ...
- 验证java引用的小例子
1. 声明一个变量person指向一个引用对象, 然后将这个person添加到集合list中, 然后将变量person指向null, 问:list中添加的person变成null了吗? import ...
- [Python] Python 虚拟机 - virtualenv
virtualenv virtualenv 用于创建一个隔离的 Python 环境. 每个项目都有自己的依赖包,这些依赖包有时存在版本冲突,处理这种情况最好方法就是为每个项目创建一个专属的环境. 安装 ...
- Ubuntu 下 Sublime 无法输入中文?(已解决)
在 Ubuntu 里安装了 Sublime 却不能输入中文? 这可不好. 怎么办呢? Follow Me! 1 获得 sublime-imfix.c 文件 有 GitHub 账号的,可以从 https ...