当前位置:网站首页>#yyds干货盘点# 解决剑指offer:调整数组顺序使奇数位于偶数前面(二)
#yyds干货盘点# 解决剑指offer:调整数组顺序使奇数位于偶数前面(二)
2022-06-24 12:49:00 【51CTO】
1.简述:
描述
输入一个长度为 n 整数数组,数组里面可能含有相同的元素,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前面部分,所有的偶数位于数组的后面部分,对奇数和奇数,偶数和偶数之间的相对位置不做要求,但是时间复杂度和空间复杂度必须如下要求。
数据范围:,数组中每个数的值
要求:时间复杂度
,空间复杂度
示例1
输入:
返回值:
说明:
示例2
输入:
返回值:
说明:
示例3
输入:
返回值:
2.代码实现:
import java.util.*;
public class Solution {
public int[] reOrderArrayTwo (int[] array) {
//双指针
int i = 0;
int j = array.length - 1;
//向中间聚合
while(i < j){
//左右都是奇数,左移右不动
if(array[i] % 2 == 1 && array[j] % 2 == 1)
i++;
//左奇数右偶数,左右都向中间缩
else if(array[i] % 2 == 1 && array[j] % 2 == 0){
i++;
j--;
}
//左偶右奇数
else if(array[i] % 2 == 0 && array[j] % 2 == 1){
//交换
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
//左右都是偶数,只移动右指针
else
j--;
}
return array;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
边栏推荐
- Sinomeni vine was selected as the "typical solution for digital technology integration and innovative application in 2021" of the network security center of the Ministry of industry and information te
- 不用Home Assistant,智汀也开源接入HomeKit、绿米设备?
- Kubernetes集群部署
- Optimization of MP4 file missing seconds caused by TS files when downloading videos from easydss video platform
- AGCO AI frontier promotion (6.24)
- Summary of the process of restoring damaged data in MySQL database
- CVPR 2022 - Interpretation of selected papers of meituan technical team
- what the fuck! I'm flattered. He actually wrote down the answers to the redis interview questions that big companies often ask!
- “有趣” 是新时代的竞争力
- How can the new webmaster avoid the ups and downs caused by SEO optimization?
猜你喜欢
CVPR 2022 | 美团技术团队精选论文解读
openGauss内核:简单查询的执行
Getting started with the go Cobra command line tool
Pycharm中使用Terminal激活conda服务(终极方法,铁定可以)
手把手教你用AirtestIDE无线连接手机!
A hero's note stirred up a thousand waves across 10 countries, and the first-line big factories sent people here- Gwei 2022 Singapore
Definition and use of constants in C language
Without home assistant, zhiting can also open source access homekit and green rice devices?
我真傻,招了一堆只会“谷歌”的程序员!
Detailed explanation of abstractqueuedsynchronizer, the cornerstone of thread synchronization
随机推荐
RAID5 array recovery case tutorial of a company in Shanghai
Attack Science: DDoS (Part 2)
YOLOv6:又快又准的目标检测框架开源啦
实现领域驱动设计 - 使用ABP框架 - 创建实体
One hour is worth seven days! Ingenuity in the work of programmers
Richard Sutton, the father of reinforcement learning, paper: pursuing a general model for intelligent decision makers
Use terminal to activate CONDA service in pypharm (the ultimate method is definitely OK)
Preparation and operation & Maintenance Guide for 'high concurrency & high performance & high availability service program'
一文讲透研发效能!您关心的问题都在
Process basic properties
The data value reported by DTU cannot be filled into Tencent cloud database through Tencent cloud rule engine
Use abp Zero builds a third-party login module (I): Principles
敏捷之道 | 敏捷开发真的过时了么?
Why does the kubernetes environment require that bridge NF call iptables be enabled?
1. Snake game design
系统测试主要步骤
Party, Google's autoregressive Wensheng graph model
#云原生征文#Ingress案例实战
CVPR 2022 | interprétation de certains documents de l'équipe technique de meituan
Geological disaster early warning monitoring RTU