当前位置:网站首页>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;
}边栏推荐
- PyTorch中repeat、tile与repeat_interleave的区别
- BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
- YYGH-9-预约下单
- Develop scalable contracts based on hardhat and openzeppelin (I)
- Take you ten days to easily finish the finale of go micro services (distributed transactions)
- 动态内存(进阶四)
- The most understandable f-string tutorial in history, collecting this one is enough
- Filtre de profondeur de la série svo2
- Mish shake the new successor of the deep learning relu activation function
- Leetcode739 每日温度
猜你喜欢

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

Depth filter of SvO2 series

Data analysis - Matplotlib sample code

动态内存(进阶四)

File operation (detailed!)

jenkins 凭证管理

XSS labs master shooting range environment construction and 1-6 problem solving ideas

Power Spectral Density Estimates Using FFT---MATLAB

The selected cells in Excel form have the selection effect of cross shading

Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
随机推荐
自然语言处理系列(二)——使用RNN搭建字符级语言模型
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
Mish shake the new successor of the deep learning relu activation function
史上最易懂的f-string教程,收藏这一篇就够了
How to Easily Create Barplots with Error Bars in R
PHP query distance according to longitude and latitude
How to Add P-Values onto Horizontal GGPLOTS
Time format display
MSI announced that its motherboard products will cancel all paper accessories
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
Research on and off the Oracle chain
qt 仪表自定义控件
Data analysis - Matplotlib sample code
自然语言处理系列(一)——RNN基础
YYGH-BUG-05
浅谈sklearn中的数据预处理
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
ESP32 Arduino 引入LVGL 碰到的一些问题
The position of the first underline selected by the vant tabs component is abnormal
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)