当前位置:网站首页>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;
}
边栏推荐
- 浅谈sklearn中的数据预处理
- 通讯录的实现(文件版本)
- How to Create a Beautiful Plots in R with Summary Statistics Labels
- Mish shake the new successor of the deep learning relu activation function
- Log4j2
- 还不会安装WSL 2?看这一篇文章就够了
- XSS labs master shooting range environment construction and 1-6 problem solving ideas
- SSH automatically disconnects (pretends to be dead) after a period of no operation
- Log4j2
- 数据分析 - matplotlib示例代码
猜你喜欢
Develop scalable contracts based on hardhat and openzeppelin (I)
Mish shake the new successor of the deep learning relu activation function
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
【C语言】十进制数转换成二进制数
【2022 ACTF-wp】
[geek challenge 2019] upload
Take you ten days to easily finish the finale of go micro services (distributed transactions)
Read the Flink source code and join Alibaba cloud Flink group..
K-Means Clustering Visualization in R: Step By Step Guide
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
随机推荐
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
FLESH-DECT(MedIA 2021)——一个material decomposition的观点
K-Means Clustering Visualization in R: Step By Step Guide
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
Depth filter of SvO2 series
Seriation in R: How to Optimally Order Objects in a Data Matrice
MySQL stored procedure cursor traversal result set
H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
[multithreading] the main thread waits for the sub thread to finish executing, and records the way to execute and obtain the execution result (with annotated code and no pit)
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
SCM power supply
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
How to Create a Beautiful Plots in R with Summary Statistics Labels
GGPlot Examples Best Reference
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
CMake交叉编译
行業的分析
Filtre de profondeur de la série svo2
Log4j2
YYGH-9-预约下单