当前位置:网站首页>Leetcode922 sort array by parity II
Leetcode922 sort array by parity II
2022-07-02 12:05:00 【Monsters 114】
Given an array of nonnegative integers nums, nums The middle half integer is Odd number , Half the whole number is even numbers .
Sort the array , For convenience nums[i] In an odd number of ,i It's also Odd number ; When nums[i] For even when , i It's also even numbers .
Input :nums = [4,2,5,7] Output :[4,5,2,7] explain :[4,7,2,5],[2,5,4,7],[2,7,4,5] It will also be accepted .
Ideas : Double pointer
If the original array can be modified , Then the local algorithm can be used to solve .
Maintain pointers for the even and odd subscript parts of the array respectively i, j. And then , At each step , If nums[i] It's odd , And keep moving forward j Move two units at a time , Until I meet the next even number . here , You can directly nums[i] And nums[j] In exchange for . We continue this process , Finally, all integers can be put in the correct position .
Implementation code :
public int[] sortArrayByParityII(int[] nums) {
int j = 1;
for(int i = 0; i < nums.length; i+=2){
if(nums[i] % 2 == 1){
while(nums[j] % 2 == 1){
j += 2;
}
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
return nums;
}边栏推荐
- Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
- 【C语言】十进制数转换成二进制数
- Three transparent LED displays that were "crowded" in 2022
- [visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl
- PyTorch中repeat、tile与repeat_interleave的区别
- Easyexcel and Lombok annotations and commonly used swagger annotations
- 【C语言】杨辉三角,自定义三角的行数
- Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
- Analyse de l'industrie
- PyTorch nn. Full analysis of RNN parameters
猜你喜欢

BEAUTIFUL GGPLOT VENN DIAGRAM WITH R

Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law

How to Create a Beautiful Plots in R with Summary Statistics Labels

Power Spectral Density Estimates Using FFT---MATLAB
![[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)](/img/18/f0c9ef6250a717f8e66c95da4de08c.jpg)
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)

Take you ten days to easily finish the finale of go micro services (distributed transactions)

机械臂速成小指南(七):机械臂位姿的描述方法

K-Means Clustering Visualization in R: Step By Step Guide

jenkins 凭证管理

FLESH-DECT(MedIA 2021)——一个material decomposition的观点
随机推荐
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
Easyexcel and Lombok annotations and commonly used swagger annotations
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
GGPlot Examples Best Reference
行業的分析
YYGH-BUG-04
ES集群中节点与分片的区别
时间格式化显示
What week is a date obtained by QT
Leetcode922 按奇偶排序数组 II
文件操作(详解!)
Fabric.js 3个api设置画布宽高
PyTorch nn. Full analysis of RNN parameters
JZ63 股票的最大利润
K-Means Clustering Visualization in R: Step By Step Guide
From scratch, develop a web office suite (3): mouse events
How to Add P-Values onto Horizontal GGPLOTS
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
Analyse de l'industrie