当前位置:网站首页>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;
}边栏推荐
- BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
- 行业的分析
- PyTorch nn.RNN 参数全解析
- Fabric. JS 3 APIs to set canvas width and height
- 机械臂速成小指南(七):机械臂位姿的描述方法
- HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
- YYGH-BUG-04
- easyExcel和lombok注解以及swagger常用注解
- (C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- PHP query distance according to longitude and latitude
猜你喜欢

HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE

Yygh-9-make an appointment to place an order

HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R

6. Introduce you to LED soft film screen. LED soft film screen size | price | installation | application

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

基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)

文件操作(详解!)

SVO2系列之深度濾波DepthFilter

YYGH-BUG-05

H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
随机推荐
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
File operation (detailed!)
R HISTOGRAM EXAMPLE QUICK REFERENCE
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
二分刷题记录(洛谷题单)区间的甄别
自然语言处理系列(二)——使用RNN搭建字符级语言模型
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
Codeforces 771 div2 B (no one FST, refers to himself)
史上最易懂的f-string教程,收藏這一篇就够了
机械臂速成小指南(七):机械臂位姿的描述方法
HOW TO ADD P-VALUES TO GGPLOT FACETS
[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)
B high and beautiful code snippet sharing image generation
CONDA common command summary
还不会安装WSL 2?看这一篇文章就够了
How to Create a Beautiful Plots in R with Summary Statistics Labels
What week is a date obtained by QT
PyTorch中repeat、tile与repeat_interleave的区别
Leetcode739 每日温度
This article takes you to understand the operation of vim