Leetcode 922. Sort Array By Parity II , Java解法

Leetcode——Sort算法题 Easy 350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. 正确版本: import java.util.ArrayList; import java.util.HashMap; public class Solution { publ... 阅读详情

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.

Example 1:

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.

Note:

  1. 2 <= A.length <= 20000
  2. A.length % 2 == 0
  3. 0 <= A[i] <= 1000

Approach #1 Brute Force 我的解法。

class Solution {
    public int[] sortArrayByParityII(int[] A) {
        int len = A.length;
        int[] odd = new int[len/2];
        int[] even = new int[len/2];
        int k = 0;
        int j = 0;
        int[] ans = new int[len];
        for(int i=0;i<len;i++){
            if(A[i]%2 == 0){
                even[j++] = A[i];
            }
            else{
                odd[k++] = A[i];
            }
        }
        
        for(int i= 0; i<len/2; i++){
            ans[2*i] = even[i];
            ans[2*i+1] = odd[i];
        }
        return ans;
    }
}

分析

时间复杂度: O(N)

空间复杂度: O(N)

Approach 2: Read / Write Heads

class Solution {
    public int[] sortArrayByParityII(int[] A) {
        int j = 1;
        for (int i = 0; i < A.length; i += 2)  //先搞定偶数部分
            if (A[i] % 2 == 1) {
                while (A[j] % 2 == 1)          //直到找到一个偶数记下其位置
                    j += 2;

                // Swap A[i] and A[j]          // 交换在奇数位置的偶数和在偶数位置的奇数
                int tmp = A[i];
                A[i] = A[j];
                A[j] = tmp;
            }

        return A;
    }
}

Modify the original array A in place.

Algorithm

For each even i, let's make A[i] even. To do it, we will draft an element from the odd slice. We pass j through the odd slice until we find an even element, then swap. Our invariant is maintained, so the algorithm is correct.

Complexity Analysis

  • Time Complexity: O(N), where NN is the length of A.

  • Space Complexity: O(1). 

亚麻 OA 2019 题目 Most Common Word Prison Cells After N Days K Closest Points to Origin Reorder Log Files Partition Labels Min Cost to Add New Roads Min distance to remove the obstacle Roll Dice Min Cost to Con... 阅读详情

相关推荐

Wordpress实战之第二章安装wordpress 软件需要的软件

使用下载并安装phpstudy面版工具 wget -O install.sh?https://notdocker.xp.cn/install.sh?&& sudo bash install.sh 2.安装成功如下 ==============运行状态========================= webpanel stop phpstudy running =================...

安全参透之旅 611

922. Sort Array By Parity II

这种类型的题 我都是使用两个指针,前面一个指针指向当前需要替换的元素位置,后面一个指针指向能替换前一个位置的位置,然后替换 以后复习的时候不用看原题 vector&lt;int&gt; sortArrayByParityII(vector&lt;int&gt;&amp; A) { int size = A.size(); for(int i = 0 ; i &lt...

Do_not_be_shy的博客 224

ANSYS命令流黑箱揭秘:流体渗透压力载荷的七种收敛优化策略

本文深入解析ANSYS Workbench中流体渗透压力载荷的七种收敛优化策略,重点探讨接触探测点迭代逻辑、PPCN阈值动态调整及FPAT参数优化等关键技术。通过命令流示例和实战技巧,帮助工程师有效解决流体与结构相互作用仿真中的收敛难题,提升计算效率和精度。

weixin_29274337的博客 521

leetcode 905. Sort Array By Parity(按奇偶校验排序数组)--Java题解

题目描述 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素。 你可以返回满足此条件的任何数组作为答案。 示例 输入:[3, 1, 2, 4] 输出:[2, 4, 3, 1] 输出   [4, 2, 3, 1],[2, 4, 1, 3]和[4, 2, 1, 3]也会被接受 提示 1 &lt;= A.length &lt;= 5000 0...

leyao的博客 558

论文阅读--射频电源在半导体领域的应用

射频电源(RF Power Supply)是能够输出300kHz~300GHz正弦波电压的高频交流电源,在半导体制造领域,射频电源的典型工作频率范围为400kHz∼60 MHz,主要由射频发生器(功率源)、匹配器及配套控制电路组件构成,其关键组件包括变压器与电抗器、晶闸管、绝缘栅双极型晶体管(IGBT)、钣金件、铜材及各种电子元器件。射频电源是刻蚀和薄膜沉积工艺的核心,主要用于产生等离子体,以增强气体反应活性,其性能直接影响反应腔体内等离子体的浓度、均匀性和稳定性。

伊丽莎白的学习之路 2337

MicroPython ubluetooth 库详细介绍

MicroPython的ubluetooth库为蓝牙通信提供了强大的支持,使得开发者能够在MicroPython环境下轻松实现蓝牙设备之间的通信。其中ubluetooth库为MicroPython提供了强大的蓝牙通信功能,使得开发者能够轻松地在各种支持MicroPython的硬件平台上实现蓝牙设备之间的通信。它提供了一系列的函数和类,用于实现蓝牙设备的扫描、连接、数据传输等功能。- 上述代码中,首先创建了一个蓝牙服务和一个特征,然后可以通过read和write函数读取和写入特征值。# 处理连接成功事件。

m0_46311182的博客 1761

leetcode 922 Sort Array By Parity II (奇偶性排序)

题目要求 给定非负整数的数组A,A中的一半整数是奇数,而整数的一半是偶数。 对数组进行排序,以便每当A [i]为奇数时,i就是奇数; 只要A [i]是偶数,i就是偶数。 您可以返回满足此条件的任何答案数组 解题思路 要保证当A [i]为奇数时,i就是奇数; 只要A [i]是偶数,i就是偶数。 我们使用两个指针分别指向第一个偶数位和奇数位,然后分别判断偶数位和奇数位是否符合题意,当出现不符合的时候进...

爱喝舒化奶的博客 250

MATLAB GUI 水果成熟度分级系统设计与实现

本文基于 MATLAB GUI 设计并实现了一款水果成熟度分级系统,能够准确地评估水果的成熟程度。在实现过程中,我们运用了 MATLAB 中的函数实现对图像的处理和分析,同时使用 GUI 界面增加了系统的易用性。值得一提的是,在实际操作中,我们应该结合实际需求对系统进行相应的改进与完善,以满足不同领域和应用场景下的需求。实验结果表明,本系统可以有效地检测出水果的成熟度,并对其进行精准的评分。因此,本文设计并实现了一款基于 MATLAB GUI 的水果成熟度分级系统。三、MATLAB 代码实现。

uote_e的博客 707

【LeetCode】922. Sort Array By Parity II 解题报告(Python)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/sort-array-by-parity-ii 题目描述: Given an array A of non-negative integers, half of the integers in A are...

负雪明烛 2132

【忍者算法】从拼积木到最长连续序列:一道别出心裁的数组题目|LeetCode 128 最长连续序列

本题核心:通过预处理数据结构来优化查找!

忍者算法的博客 737

【第一章】核心思维:那个在此刻“消失”的 CPU

摘要:RTOS本质是"有状态的抢占"而非"多任务",单核MCU通过时间片分时复用实现任务切换。裸机编程的线性思维在RTOS中不再适用,开发者需接受代码随时被打断的现实。RTOS通过保存/恢复CPU寄存器组(上下文)实现任务切换,每个任务拥有独立栈空间。非原子操作(如全局变量自增)会导致竞态条件,必须使用临界区或互斥锁保护。RTOS编程需要建立并发思维,关注代码原子性、可重入性和栈空间分配。

一路往蓝-Anbo 的博客 103

【Array-easy】922. Sort Array By Parity II 奇数放奇数下标偶数放偶数下标

1. 题目原址 https://leetcode.com/problems/sort-array-by-parity-ii/ 2. 题目描述 3. 题目大意 给定一个数组,其中奇数和偶数各一半,要求返回数组中,奇数索引下标的元素为奇数,偶数索引下标中的元素为偶数。 4. 解题思路 签到题: 两个指针,指针每次移动两个位置。 5. AC代码 class Solution { public ...

想当厨子的程序媛 323

TBOX电源方案的详细讲解与分析

输入保护电路是确保车载电子系统稳定性的关键。通过合理选择TVS二极管、保险丝和浪涌抑制器等元器件,能够有效防止来自电源系统的瞬态过电压、浪涌和过流问题,保护后续电路免受损害。针对不同的电源环境和应用场景,可以通过提升功率、优化热管理以及选择高效元器件来进一步提高系统的保护能力和可靠性。

weixin_43199439的博客 1074

[LeetCode] 922. Sort Array By Parity II 按奇偶排序数组之二

Given an array Aof non-negative integers, half of the integers in A are odd, and hal...

azl397985856的专栏 167

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

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

【LeetCode】 922. Sort Array By Parity II 按奇偶排序数组 II(Easy)(JAVA) 题目地址: https://leetcode.com/problems/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

qq_16927853的博客 188

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Leetcode学习笔记:#922. Sort Array By Parity II

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

ccystewart的博客 152

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

leetcode922-Sort Array By Parity II

我们可以用快慢指针来解决这个问题,慢指针永远指向偶数的i,快指针永远指向奇数的i,那么当发生慢指针指向的元素是奇数快指针指向的元素是偶数的时候,交换俩个元素的位置,我们遍历数组也是为了寻找这样的俩个元素。对数组进行排序,以便当 nums[i] 为奇数时,i 也是 奇数;给定一个非负整数数组 nums, nums 中一半整数是 奇数 ,一半整数是 偶数。解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。输入:nums = [4,2,5,7]输出:[4,5,2,7]

我的程序人生 293

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

给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。 你可以返回任何满足上述条件的数组作为答案。   示例: 输入:[4,2,5,7] 输出:[4,5,2,7] 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。   提示: 2 &lt;...

一叶之修的博客 330

【模拟】922. Sort Array By Parity II

Given an array of integers nums, half of the integers in nums are odd, and the other half are even.Sort the array so that whenever nums[i] is odd, i is odd, and w...

weixin_41442072的博客 107

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

weixin_34192816的博客 103

LeetCode题解(十)0900-0999,android实现文件下载

return A; } } 922. Sort Array By Parity II [Description] 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

m0_64319496的博客 528

leetcode - 922 - 按奇偶排序数组 II

class Solution:     def sortArrayByParityII(self, A):         """         :type A: List[int]         :rtype: List[int]         """         a=[]         b=[]         c=[]                  for i in A: ...

这些个事儿 302

LeetCode-Sort Array By Parity II

LeetCode Java Sort Array By Parity II

坟头草一米七五 402

Leetcode 1086. High Five Java 解法

先吐槽一下这个难度,咋也不能是easy啊,和其他easy的不在一个量级。 Given a list of scores of different students, return the average score of each student'stop five scoresinthe order of each student's id. Each entryitems[i]h...

YuanTheCoder的博客 1473

Leetcode 1051. Height Checker, Java解法

Students are asked to stand in non-decreasing order of heights for an annual photo. Return the minimum number of students not standing in the right positions. (This is the number of students that mu...

YuanTheCoder的博客 649
上一篇: Leetcode 1051. Height Checker, Java解法
下一篇: 公开课 CS61B Data Structures, UCB
YuanTheCoder
博客等级 码龄9年 2粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值