当前位置:网站首页>#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.
边栏推荐
- 我開導一個朋友的一些話以及我個人對《六祖壇經》的一點感悟
- nifi从入门到实战(保姆级教程)——环境篇
- Main steps of system test
- Geological disaster early warning monitoring RTU
- Perhaps the greatest romance of programmers is to commemorate their dead mother with a software
- “我这个白痴,招到了一堆只会“谷歌”的程序员!”
- Party, Google's autoregressive Wensheng graph model
- Nifi from introduction to practice (nanny level tutorial) - environment
- CVPR 2022 - Interpretation of selected papers of meituan technical team
- How can the new webmaster avoid the ups and downs caused by SEO optimization?
猜你喜欢

不用Home Assistant,智汀也开源接入HomeKit、绿米设备?
![[data mining] final review (sample questions + a few knowledge points)](/img/90/a7b1cc2063784fb53bb89b29ede5de.png)
[data mining] final review (sample questions + a few knowledge points)

#云原生征文#Ingress案例实战

Understanding openstack network

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

Beauty of script │ VBS introduction interactive practice

爱可可AI前沿推介(6.24)

Brief introduction to cluster analysis

Who said that "programmers are useless without computers? The big brother around me disagrees! It's true

Parti,谷歌的自回归文生图模型
随机推荐
Use abp Zero builds a third-party login module (I): Principles
Perhaps the greatest romance of programmers is to commemorate their dead mother with a software
One hour is worth seven days! Ingenuity in the work of programmers
8 lines of code to teach you how to build an intelligent robot platform
Baidu simian: talk about persistence mechanism and rdb/aof application scenario analysis!
Quickly understand the commonly used message summarization algorithms, and no longer have to worry about the thorough inquiry of the interviewer
Internet of things? Come and see Arduino on the cloud
I have fundamentally solved the problem of wechat occupying mobile memory
I enlighten a friend and my personal understanding of the six patriarchs' Tan Jing
go Cobra命令行工具入门
Leetcode 1218. Longest definite difference subsequence
面试官:MySQL 数据库查询慢,除了索引问题还可能是什么原因?
简述聚类分析
Pycharm中使用Terminal激活conda服务(终极方法,铁定可以)
About the hacked database
Getting started with the go Cobra command line tool
Who is the fish and who is the bait? Summary of honeypot recognition methods from the perspective of red team
Comparator 排序函数式接口
Kubernetes cluster deployment
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