当前位置:网站首页>【模拟】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;
}
}边栏推荐
- 1175. Prime Arrangements
- 基础知识之一——STA基础概述
- 双位置继电器ST2-2L/AC220V
- 人穷志不短,穷学生也能玩转树莓派
- [learning notes] double + two points
- Solve idea:class' xxx 'not found in module' xxx‘
- None of the following candidates is applicable because of a receiver type mismatch
- Locking relay ydb-100, 100V
- [LeetCode] 两数之和【1】
- Why build a personal blog
猜你喜欢

Looksrare team's "cash out" caused disturbance

DLS-20型双位置继电器 220VDC

Note d'étude du DC: zéro dans le chapitre officiel - - Aperçu et introduction du processus de base

2021电赛F题openmv和K210调用openmv api巡线,完全开源。

【学习笔记】倍增 + 二分

Windows环境下安装MongoDB数据库

Digital IC design process summary

二十多年来第一次!CVPR最佳学生论文授予中国高校学生!

ESP8266 RC522

TCP三次握手为什么不是两次或四次
随机推荐
冲击继电器ZC-23/DC220V
Service grid ASM year end summary: how do end users use the service grid?
Open3D 点云包围盒
使用StrictMode-StrictMode原理(1)
qt5-MVC:数据可视化的层次揭秘
OCR的一些项目
ESP8266 RC522
ORB-SLAM2源码学习(二)地图初始化
[问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号
[leetcode] sum of two numbers [1]
06.论Redis持久化的几种方式
自定义注解实现校验
[network packet loss and network delay? This artifact can help you deal with everything!]
Docker deployment MySQL 8
1175. Prime Arrangements
Orb-slam2 source code learning (II) map initialization
基础知识之二——STA相关的基本定义
Construction and beautification of personal blog
系统设置大页
小程序自定义宫格