LeetCode 905. Sort Array By Parity
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.
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 <= 50000 <= A[i] <= 5000
题目描述:题意是要求给数组排序,排序的原则是偶数在前,奇数在后。
题目分析:很简单,我们直接给数组分分类就好了,然后用一个新的数组去存取值就行了。
python 代码:
class Solution(object):
def sortArrayByParity(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
odd_list = []
even_list = []
final_list = []
A_length = len(A)
for i in range(A_length):
if A[i] % 2 == 0:
even_list.append(A[i])
else:
odd_list.append(A[i])
final_list = even_list + odd_list
return final_list
C++ 代码:
class Solution {
public:
vector<int> sortArrayByParity(vector<int>& A) {
vector<int> final_list(A.size());
int count_odd = 0;
int count_even = 0;
for(int i = 0; i < A.size(); i++){
if(A[i] % 2 == 0){
count_even++;
}
else{
count_odd++;
}
}
vector<int> odd_list(count_odd);
vector<int> even_list(count_even);
int odd = 0;
int even = 0;
for(int i = 0; i < A.size(); i++){
if(A[i] % 2 == 0){
even_list[even++] = A[i];
}
else{
odd_list[odd++] = A[i];
}
}
final_list = even_list;
final_list.insert(final_list.end(),odd_list.begin(),odd_list.end());
return final_list;
}
};
LeetCode 905. Sort Array By Parity的更多相关文章
- [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 ...
- 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 按奇偶排序数组之二
Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...
- 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 ...
随机推荐
- PyCharm 在PyCharm中使用GitHub
PyCharm是当前进行Python开发,尤其是Django开发最好的IDE,GitHub是程序员的圣地,几乎人人都在用,就不详细介绍两者了. 本文假设你对PyCharm和Github都有一定的了解, ...
- Orchard详解--第三篇 依赖注入之基础设施
Orchard提供了依赖注入机制,并且框架的实现也离不开依赖注入如模块管理.日志.事件等.在前一篇中提到在Global.asax中定义的HostInitialization创建了Autofac的IoC ...
- Linux中DNS的设置
1.查看本机的域名 hostname 2.修改DNS 临时修改: hostname desktop0.example.com 永久修改: hostnamectl set-hostname deskto ...
- Java中数组、List、Set互相转换
数组转List String[] staffs = new String[]{"Tom", "Bob", "Jane"}; List sta ...
- MySQL open_tables和opened_tables
官网解释参见:https://dev.mysql.com/doc/refman/5.7/en/table-cache.html 其他可供参考的文章有: 关于表限制参数的使用:https://dba.s ...
- SQL Server has encountered 1 occurrence(s) of cachestore flush for the 'Object Plans' cachestore (part of plan cache) due to some database maintenance or reconfigure operations.
2017-11-01 09:49:44.35 spid166 SQL Server has encountered 1 occurrence(s) of cachestore flush for th ...
- mysql中导入导出sql文件
1.导出整个数据库: mysqldump -u用户名 -p密码 数据库名 > 导出的文件名 例:mysqldump -uroot -proot user > user.sql 2.导出一个 ...
- MATLAB常微分方程数值解——欧拉法、改进的欧拉法与四阶龙格库塔方法
MATLAB常微分方程数值解 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1.一阶常微分方程初值问题 2.欧拉法 3.改进的欧拉法 4.四阶龙格库塔 ...
- [WeChall] Training: Encodings I (Training, Encoding)
Training: Encodings I (Training, Encoding) We intercepted this message from one challenger to anothe ...
- 【Teradata System】How Teradata uses MPP Systems
内存分配 内存初始化时将分配给操作系统和Vprocs,内存不使用部分的90%做为FSG (File Segment Cache) ,由PDE对FSG进行管理. FSG Cache:缓存常驻内存的dat ...