【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 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 satisfies this condition.
 *
 * Input: [4,2,5,7]
 * Output: [4,5,2,7]
 * Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
 *
 * 有一个数组A,其中奇元素和偶元素的数量相等。请把A排序,使得奇元素位于奇数位置,偶元素位于偶数位置。任何满足上述条件的排序都是合法的。
 *
 * @Date: 2019/7/3 17:54
 * @Version: 1.0
 */
public class SortArrayByParityII {

    public int[] solution(int[] A) {
        //定义2个索引,第一指向奇数索引,第二个偶数索引
        int oddIndex = 1;
        int evenIndex = 0;
        while(oddIndex < A.length && evenIndex < A.length) {
            while(oddIndex < A.length && (A[oddIndex] & 1) == 1) {
                oddIndex += 2;
            }

            while (evenIndex < A.length && (A[evenIndex] & 1) == 0) {
                evenIndex += 2;
            }

            //交换位置
            if(oddIndex < A.length && evenIndex < A.length) {
                int temp = A[oddIndex];
                A[oddIndex] = A[evenIndex];
                A[evenIndex] = temp;
                oddIndex += 2;
                evenIndex += 2;
            }
        }

        return A;
    }

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

}

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

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

【LEETCODE】42、922. Sort Array By Parity II的相关文章

【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 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 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

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 satisfi

【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】Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one sha

Sort Array By Parity II LT922

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 satisfi

【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