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. 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;
    }
};

原文地址:https://www.cnblogs.com/ECJTUACM-873284962/p/10162828.html

时间: 2024-11-08 10:06:11

LeetCode 905. Sort Array By Parity的相关文章

【LEETCODE】41、905. Sort Array By Parity

package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * @ClassName: SortArrayByParity * @Author: xiaof * @Description: 905. Sort Array By Parity * Given an array A of non-negative integers, return an array

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

Sort Array By Parity 题目链接 题目描述 给定一个非负整型数组A,返回一个数组,数组中靠前的位置是A中的偶数,靠后的位置是A中的奇数.偶数范围内顺序不限,奇数也是. 约定 1 <= A.length <= 5000 0 <= A[i] <= 5000 示例 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 accepte

905. Sort Array By Parity

题目来源: https://leetcode.com/problems/sort-array-by-parity/ 自我感觉难度/真实难度: easy/easy 题意: 把列表里的偶数放在前面,奇数放在后面 分析: 自己的代码: class Solution(object): def sortArrayByParity(self, A): """ :type A: List[int] :rtype: List[int] """ c=[] b=[]

【leetcode】905. Sort Array By Parity

题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数组头部,否则追加到尾部. 代码如下: class Solution(object): def sortArrayByParity(self, A): """ :type A: List[int] :rtype: List[int] """ res =

[LeetCode&amp;Python] Problem 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] T

C解905. Sort Array By Parity——LeetCode刷题

给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入:[3,1,2,4] 输出:[2,4,3,1] 输出 [4,2,3,1],[2,4,1,3] 和 [4,2,1,3] 也会被接受. 提示: 1 <= A.length <= 5000 0 <= A[i] <= 5000 代码: 1 int* sortArrayByParity(int* A, int ASize, int* retu

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 integers are even. Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even. You may return any answer array that sa

LeetCode 922. Sort Array By Parity II (按奇偶排序数组 II)

题目标签:Sort 利用两个指针,在偶数位置上找到第一个奇数:在奇数位置上找到第一个偶数,然后互相转换数字. 具体看code. Java Solution: Runtime:  2ms, faster than 99.61% Memory Usage: 42.9MB, less than 29.63% 完成日期:03/06/2020 关键点:two pointers class Solution { public int[] sortArrayByParityII(int[] A) { int

【LEETCODE】42、922. Sort Array By Parity II

package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * @ClassName: SortArrayByParityII * @Author: xiaof * @Description: 922. Sort Array By Parity II * Given an array A of non-negative integers, half of the