【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 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.
 *
 * 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.
 *
 * @Date: 2019/7/3 8:48
 * @Version: 1.0
 */
public class SortArrayByParity {

    public int[] solution(int[] A) {
        //先获取所有偶元素,然后获取所有奇元元素,还有一个方法就是交换前后位置
        //吧所有偶数放到前面,奇数放后面,类似快排
        int index1 = 0, index2 = A.length - 1;
        while(index1 < index2) {
            //遍历起始第一个不是偶数的位置
            while(index1 < A.length && (A[index1] & 1) == 0) {
                index1++;
            }
            //后面往前,找到第一个不是奇数
            while(index2 > 0 && (A[index2] & 1) == 1) {
                index2--;
            }
            //交换位置
            if(index1 < index2 && index1 < A.length && index2 > 0) {
                //交换位置
                int temp = A[index1];
                A[index1] = A[index2];
                A[index2] = temp;
            }
        }

        return A;
    }

    public static void main(String args[]) {
        int A[] = {3,1,2,4};
        SortArrayByParity fuc = new SortArrayByParity();
        System.out.println(fuc.solution(A));
    }
}

原文地址:https://www.cnblogs.com/cutter-point/p/11128151.html

时间: 2024-10-09 00:38:13

【LEETCODE】41、905. Sort Array By Parity的相关文章

【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

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

【Leetcode】Median of Two Sorted Array II

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 非常经典的一个问题,如果直接查找中位数,即偶数约定向下取整,奇数就是严格的中位数,这个题目直接可以比较每个数组的mid来计算得到. 但是这个问题的需求更改为如果为偶数,则中

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,

【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][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】Search in Rotated Sorted Array——旋转有序数列找目标值

[题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no d

【LeetCode】Search in Rotated Sorted Array 解题报告

[题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no d