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... 阅读详情

相关推荐

深入解析EDID中Display Descriptor Block的配置技巧与实战应用

本文深入解析EDID中Display Descriptor Block的配置技巧与实战应用。通过剖析像素时钟、有效像素、消隐区与同步脉冲等核心参数,并结合1280x720@60Hz的实战配置案例,指导开发者如何正确为定制硬件或非标准显示器编写“身份证”,避免黑屏、分辨率异常等问题,确保显示设备完美点亮。

1a2s3d4f5g的博客 878

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

S7-1200PLC 与温控仪表Modbus RTU通信

硬件:cpu 1214C 目的:通过Modbus RTU协议,实现对温控仪表的控制 软件:Portal V14SP1

weixin_43922025的博客 3万+

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

教程 | VeriStand硬件在环仿真测试第一章

这篇是系列文章的第一章,满满的都是干货,感兴趣的话欢迎一起交流探讨。

HUASUITECH的博客 1万+

AD9371实战:如何用ZCU106搭建JESD204B TX时钟链(含关键参数计算)

本文详细解析了如何基于Xilinx ZCU106开发板为AD9371搭建JESD204B TX时钟链。文章从系统需求出发,逐步推导出从IQ采样率到GTH线速率的关键参数计算公式,并提供了在Vivado中配置GTH收发器(包括QPLL选择、分频比设置)的实战步骤与调试技巧,旨在帮助硬件工程师快速实现稳定可靠的JESD204B高速数据传输链路。

weixin_29164843的博客 292

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

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

爱喝舒化奶的博客 250

配置 SAP Fiori 服务的 ICF 节点:从 SICF 激活到访问链路打通的完整实践

在SAP Fiori项目实施中,ICF节点配置是关键前置条件却常被忽视。ICF(Internet Communication Framework)作为HTTP请求入口,需要通过SICF事务码激活相关服务节点,否则即使应用开发完善,用户仍可能遇到页面无法访问等问题。典型问题表现为:磁贴可点击但页面空白、资源加载失败等。激活路径主要涉及三组节点:bsp分支下的管理入口、ui5_ui5分支下的应用资源,以及public/bc下的公共资源。项目实施中需特别注意同名服务在不同分支下的差异化作用,确保完整链路通畅。IC

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 263

【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

使用Simulink对氢燃料电池与锂电池的混合储能系统进行建模

本文介绍了基于Simulink的氢燃料电池与锂电池混合储能系统建模与协同控制方法。系统通过氢燃料电池提供稳定能源,锂电池实现快速响应,结合DC-DC变换器和负载管理器实现动态调控。详细阐述了从模型创建、关键模块配置到控制器设计的完整流程,重点说明了功率分配算法和充放电控制策略的实现。通过仿真分析验证了系统在不同负载条件下的性能表现,包括响应速度、工作效率和稳定性等指标。该方案为新能源混合储能系统的设计与优化提供了实用指导,有助于提升系统可靠性和延长电池寿命。

amy_mhd的博客 314

12306自动抢票系统:基于DrissionPage的智能购票实战

摘要:本文介绍了一个基于DrissionPage的12306自动抢票系统实现方案。该系统通过模拟真实用户操作,实现了车票查询、登录认证、座位选择和订单提交的全流程自动化。项目采用DrissionPage库替代传统Selenium方案,具有更简洁的API设计和更好的性能表现。核心技术包括数据包监听、动作链模拟和反爬虫应对策略,实现了高效可靠的车票抢购功能。系统包含完整的业务流程设计,从环境初始化到最终订单提交,并提供了清晰的数据展示界面。本文强调该项目仅用于技术研究,在实际使用中应严格遵守12306平台规则

那些看似不起波澜的日复一日,会在某天让你看到坚持的意义 2789

【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

项目模块提取生成jar包上传到nexus供其他项目使用

项目场景: 微服务项目当中的某一个模块其他项目进行复用,资源管理搭建的有私服 nexus。 问题描述: 如果把功能用到其他项目,代码重复量太大,不想些重复的代码。 原因分析: 如果把这个模块单独提取出来进行复用,至于操作数据库的功能,使用远程调用进行数据库操作,这样就能解决相同模块重复编写的工作。量。 解决方案: 首先对模块代码进行封装提取,把一些用到的参数提取出来,以传参的形式来对参数进行传递。这个需要根据具体的情况进行代码提取,提取之后把这个模块的代码进行打包,打包可以借助开发工具进行打包(记得使用 d

lierjun 的专栏 450

[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

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

【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

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

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

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

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、付费专栏及课程。

余额充值