当前位置:网站首页>#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.
边栏推荐
- openGauss内核:简单查询的执行
- The data value reported by DTU cannot be filled into Tencent cloud database through Tencent cloud rule engine
- I enlighten a friend and my personal understanding of the six patriarchs' Tan Jing
- Common special characters in JS and TS
- 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
- Open source monitoring system Prometheus
- Use terminal to activate CONDA service in pypharm (the ultimate method is definitely OK)
- 我开导一个朋友的一些话以及我个人对《六祖坛经》的一点感悟
- Without home assistant, zhiting can also open source access homekit and green rice devices?
- Quickly understand the commonly used message summarization algorithms, and no longer have to worry about the thorough inquiry of the interviewer
猜你喜欢
随机推荐
The difference between apt and apt get
Configuration (enable_*) parameter related to execution plan in PG
Pycharm中使用Terminal激活conda服务(终极方法,铁定可以)
Use abp Zero builds a third-party login module (I): Principles
Nifi from introduction to practice (nanny level tutorial) - environment
“我这个白痴,招到了一堆只会“谷歌”的程序员!”
Richard Sutton, the father of reinforcement learning, paper: pursuing a general model for intelligent decision makers
3. Caller 服务调用 - dapr
What is SCRM? What is the difference between SCRM and CRM
Attack Science: ARP attack
关于被黑数据库那些事
go Cobra命令行工具入门
Several common DoS attacks
what the fuck! I'm flattered. He actually wrote down the answers to the redis interview questions that big companies often ask!
[data mining] final review (sample questions + a few knowledge points)
Opengauss kernel: simple query execution
3. caller service call - dapr
Getting started with the lvgl Library - colors and images
Leetcode 1218. Longest definite difference subsequence
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享








