当前位置:网站首页>【模拟】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;
}
}边栏推荐
- Mustache syntax
- Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
- Use of typora
- MFC TCP通信服务端客户端Demo备忘vs2019
- Two position relay st2-2l/ac220v
- TypeError: Argument ‘angle‘ can not be treated as a double
- 软硬件基础知识学习--小日记(1)
- 人穷志不短,穷学生也能玩转树莓派
- JS方法大全的一个小文档
- 45岁程序员告诉你:程序员为什么要跳槽,太真实...
猜你喜欢

闭锁继电器YDB-100、100V

Poor students can also play raspberry pie

基础知识之二——STA相关的基本定义

Service grid ASM year end summary: how do end users use the service grid?

图的连通性基础

TCP三次握手为什么不是两次或四次

Principes de formation de la programmation robotique

Dx-11q signal relay

Call the classic architecture and build the model based on the classic

Docker 部署 MySQL 8
随机推荐
Service grid ASM year end summary: how do end users use the service grid?
MATLAB 最远点采样(FPS改进版)
解决IDEA:Class ‘XXX‘ not found in module ‘XXX‘
Fluent JSON serialization deserialization
User defined annotation implementation verification
MFC TCP communication server client demo notes vs2019
【go】go 实现行专列 将集合进行转列
[问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号
ASCII、Unicode、GBK、UTF-8之间的关系
Impact relay zc-23/dc220v
Visual studio 2019 shortcut notes
How to do the performance pressure test of "Health Code"
06.论Redis持久化的几种方式
探索互联网时代STEAM教育创新之路
双位置继电器DLS-5/2 DC220V
【网络丢包,网络延迟?这款神器帮你搞定所有!】
機器人編程的培訓學科類原理
(learning power + thinking power) x action power, summary of flywheel effect on the growth of technicians
奇偶链表[链表操作的两种大方向]
[leetcode] climb stairs [70]