当前位置:网站首页>【模拟】922. Sort Array By Parity II
【模拟】922. Sort Array By Parity II
2022-07-01 00:41:00 【暮色_年华】
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 whenever nums[i] is even, i is even.
Return any answer array that satisfies this condition.
题意:重排nums数组,使nums[i]和i奇偶性相同
新开一个数组(用两个指针表示奇数位和偶数为),扫描原数组,如果是奇数,放在奇数位;否则放在偶数位。
class Solution {
public int[] sortArrayByParityII(int[] nums) {
int[] t=new int[nums.length];
for(int i=0,j=0,k=1;i<nums.length;i++){
if(nums[i]%2==0){
t[j]=nums[i];
j+=2;
}else{
t[k]=nums[i];
k+=2;
}
}
return t;
}
}边栏推荐
猜你喜欢

解决IDEA:Class ‘XXX‘ not found in module ‘XXX‘

XJY-220/43AC220V静态信号继电器

Impact relay zc-23/dc220v

Q弹松软的大号吐司,带来更舒服的睡眠

Exploration and practice of "flow batch integration" in JD

双位置继电器DLS-5/2 DC220V

Two position relay st2-2l/ac220v

Use of typora

Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming

ESP8266 RC522
随机推荐
【学习笔记】构造
C# 自定义并动态切换光标
DC學習筆記正式篇之零——綜述與基本流程介紹
Analyzing the wisdom principle in maker education practice
做生意更加务实
DX-11Q信号继电器
DC学习笔记正式篇之零——综述与基本流程介绍
一些本质的区别
[leetcode] sum of two numbers [1]
Web compatibility testing of software testing
Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
[go] go implements row column conversion of sets
Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming
dc_labs--lab1的学习与总结
(学习力+思考力) x 行动力,技术人成长的飞轮效应总结
sort自定义函数
ESP8266 RC522
Analyze the maker education path integrating the essence of discipline
JS to convert numbers into Chinese characters for output
关于VCTK数据集