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. vue使用axios请求后端数据

    1. 安装axios $ npm install axios 2.在main.js里面导入axios import axios from 'axios' Vue.prototype.$http = a ...

  2. 四、Tableau如何设置数据格式

    一.要求 ‘销售额’:K为单位 ‘利润’:        M为单位,负值用括号括起来,但是正值 ‘利润率’:带百分号,负值用括号括起来仍然时负值 二.解决方案 1.‘销售额’:m为单位 2.‘利润’: ...

  3. Python 提案

    学习Java 不可不知JSR,学习Python自然也得知道 PEP了 1- PEP简介 PEP是Python增强提案(Python Enhancement Proposal)的缩写.https://w ...

  4. LeetCode算法题-Number of 1 Bits(Java实现)

    这是悦乐书的第186次更新,第188篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第45题(顺位题号是191).编写一个带无符号整数的函数,并返回它所具有的"1 ...

  5. java操作elasticsearch实现query String

    1.CommonTersQuery: 指定字段进行模糊查询 //commonTermsQuery @Test public void test35() throws UnknownHostExcept ...

  6. 【Teradata Utility】系统工具使用

    List two ways in which a system utility can be started. Explain how to use the following utilities t ...

  7. 如何在linux平台上编译安装zlib软件(公司部分线上机器缺少zlib不能安装supervisor)

    文章在Centos  6.5 linux平台上演示一下如何进行编译安装zlib软件,并配置相关的选项加载使用.示范从下载到安装并配置进行使用过程一系列整套讲解,希望可以给网友考虑使用,谢谢.   工具 ...

  8. (转)Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏

    http://www.ityouknow.com/springboot/2018/04/02/docker-favorites.html 云收藏项目已经开源2年多了,作为当初刚开始学习 Spring ...

  9. linux命令中的“<”和“|”是什么意思?

    ”<” 表示的是输入重定向的意思,就是把<后面跟的文件取代键盘作为新的输入设备.”| ”则表示一个管道的意思,可以理解为东西从管道的一边流向另外一边.   cat file.json | ...

  10. (二 -3-3) 天猫精灵接入Home Assistant-自动发现Mqtt设备-自动生成配置信息

    http://www.hassmart.com/products/switches/#tab=config switch: - platform: mqtt name: keting state_to ...