当前位置:网站首页>【模拟】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;
}
}边栏推荐
- Kongyiji's first question: how much do you know about service communication?
- For the first time in more than 20 years! CVPR best student thesis awarded to Chinese college students!
- 数字IC设计流程总结
- Visual studio 2019 shortcut notes
- fluttertoast
- 技术人进阶画业务大图,手把手教学来了
- 【qt5-tab标签精讲】Tab标签及内容分层解析
- Split the linked list [take next first and then cut the linked list to prevent chain breakage]
- 软件开发完整流程
- User defined annotation implementation verification
猜你喜欢
随机推荐
dc_labs--lab1的学习与总结
小程序自定义宫格
(learning power + thinking power) x action power, summary of flywheel effect on the growth of technicians
冲击继电器ZC-23/DC220V
Training discipline principle of robot programming
"Open math input panel" in MathType editing in win11 is gray and cannot be edited
友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)
机器人编程的培训学科类原理
DC學習筆記正式篇之零——綜述與基本流程介紹
Open3D 点云包围盒
奇偶链表[链表操作的两种大方向]
js中把数字转换成汉字输出
Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming
Poor students can also play raspberry pie
使用StrictMode-StrictMode原理(1)
做生意更加务实
【office办公-pdf篇】pdf合并与拆分让我们摆脱付费软件的功能限制好不好
软件开发完整流程
Windows环境下安装MongoDB数据库
About vctk datasets









