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. 1 <= A.length <= 5000
  2. 0 <= 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的更多相关文章

  1. [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, ...

  2. LeetCode 905 Sort Array By Parity 解题报告

    题目要求 Given an array A of non-negative integers, return an array consisting of all the even elements ...

  3. LeetCode 905. Sort Array By Parity 按奇偶校验排列数组

    题目 Given an array A of non-negative integers, return an array consisting of all the even elements of ...

  4. [leetcode] 905. Sort Array By Parity [easy]

    原题链接 很水的一道题,就是数组内部交换. 水题就想着减少复杂度嘛,于是学到一种交换写法. class Solution { public: vector<int> sortArrayBy ...

  5. 【LEETCODE】41、905. Sort Array By Parity

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  6. 905. Sort Array By Parity - LeetCode

    Question 905. Sort Array By Parity Solution 题目大意:数组排序,偶数放前,奇数在后,偶数的数之间不用管顺序,奇数的数之间也不用管顺序 思路:建两个list, ...

  7. 【Leetcode_easy】905. Sort Array By Parity

    problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. 一张图教你读懂AI简史

  2. Linux中如何通过设备号找到设备

    关于Linux中的设备文件,设备文件用来为操作系统和用户提供它们代表的设备接口.所有的Linux设备文件均位于/dev目录下,是根(/)文件系统的一个组成部分,因为这些设备文件在操作系统启动过程中必须 ...

  3. The content of element type "package" must match "(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,default-class-ref?,global- results?,global-exception-mappings?,action*)".

    报错 The content of element type "package" must match "(result-types?,interceptors?,def ...

  4. 简单易懂的程序语言入门小册子(5):基于文本替换的解释器,递归,不动点,fix表达式,letrec表达式

    这个系列有个显著的特点,那就是标题越来越长.忽然发现今天是读书节,读书节多读书. ==下面是没有意义的一段话============================================== ...

  5. [Hive_add_11] Hive 使用 UDTF 实现日志降维

    0. 说明 对日志进行降维处理,将日志分为几个小表 通过编写 UDTF ,对日志降维,将日志聚合体相关字段抽取出来,形成新表. 1. 操作流程 1.0 日志部分内容 ##{\"appChan ...

  6. Hadoop2.7.6_01_部署

    1. 主机规划 主机名称 外网IP 内网IP 操作系统 备注 安装软件 mini01 10.0.0.11 172.16.1.11 CentOS 7.4 ssh port:22 Hadoop [Name ...

  7. Metasploit渗透测试梗概

    1.渗透测试基础内容 https://blog.csdn.net/Fly_hps/article/details/79492646 2.Metasploit基础 https://blog.csdn.n ...

  8. Spring的IOC注解开发入门2

    注解方式设置属性的值 在我们IOC基于xml属性注入的方式中有(一般推荐set方法) 构造方法注入普通值:<constructor-arg>的使用 set方法注入普通值:<prope ...

  9. 【Linux常见问题】SecureCRT 终端连接密钥交换失败错误

    SecureCRT 终端软件连接linux操作系统,出现如下错误: 英文描述:Key exchange failed. No compatible key exchange method. The s ...

  10. 阿里云CentOS下nodejs安装

    1. 下载node包(包含npm) cd /usr/local/src/ wget https://nodejs.org/dist/v10.11.0/node-v10.11.0-linux-x64.t ...