当前位置:网站首页>Adjust the array order so that odd numbers precede even numbers and their relative positions remain the same
Adjust the array order so that odd numbers precede even numbers and their relative positions remain the same
2022-07-26 19:38:00 【Lin Shu ໌້ᮨ】
1. Power button _ Adjust the array so that odd numbers precede even numbers
Possible changes in relative position : With two pointers left、right, Traverse left and right at the same time , Left even stop traversal , The odd number on the right stops traversing , In exchange for left and right The corresponding value is just .
/**
* Enter an array of integers , Implement a function to adjust the order of the Numbers in the array , Make all odd numbers in the first half of the array , All even numbers are in the second half of the array .
**/
public class Num21_ Odd numbers precede even numbers {
public int[] exchange(int[] nums) {
// Double pointer i,j,i Point to the front ,j Point to the back ,i Find an even number ,j Find odd number , Find the exchange
int i=0;
int j=nums.length-1;
while(i<j){
while((i<j)&&(nums[i]&1)!=0){// Odd number
i++;
}
while((i<j)&&(nums[j]&1)==0){
j--;
}
if(i<j){
swap(nums,i,j);
}
}
return nums;
}
private void swap(int[] nums, int i, int j) {
int t=nums[i];
nums[i]=nums[j];
nums[j]=t;
}
}
2. Adjust the array order so that odd numbers precede even numbers and their relative positions remain unchanged __ Cattle from
Ideas : Using the idea of insertion sorting , Odd numbers can be inserted forward continuously
import java.util.*;
public class Solution {
public void reOrderArray(int [] array) {
int len=array.length;
int k=0;// Record the number of odd numbers that have been sorted
for(int i=0;i<len;i++){
if((array[i]&1)==1){
int j=i;
// Put an odd number j Exchange to k+1 Location 【 Odd numbers are constantly inserted forward 】
while(j>k){
int temp=array[j];
array[j]=array[j-1];
array[j-1]=temp;
j--;
}
// Out of cycle odd j Just swap it k+1 Location ,k++ Continue to cycle
k++;
}
}
}
}边栏推荐
猜你喜欢

博客维护记录之图片预览嵌入位置问题

MySQL tutorial: MySQL database learning classic (from getting started to mastering)

Do you know the difference between safety test, functional test and penetration test?

这22个绘图(可视化)方法很重要,值得收藏!

The inventory of chips in the United States is high, and the shipment of chips in China has increased rapidly and the import of 28.3 billion chips has been greatly reduced. TSMC has a showdown

canvas概述

线性代数第4章线性方程组

C # get local time / system time

基于华为云 IOT 设计智能称重系统 (STM32)【一】

聊聊如何用 Redis 实现分布式锁?
随机推荐
Don't casually pass the request to the asynchronous thread. You can't handle it. You have to use the startasync method
[yolov5] - detailed version of training your own dataset, nanny level learning, logging, hand-in-hand tutorial
How to solve the problem that win11 has been switched on after upgrading
Spatiotemporal prediction 5-gat
conda+pytorch环境教程
Save gas chitoken usage
时空预测4-graph wavenet
C#创建及读取DAT文件案例
How to write the test case of mobile app? What are the mobile app test points?
Volatile keyword of JVM memory model
PyQt5快速开发与实战 3.6 打包资源文件
NLP learning path
MySQL tutorial: MySQL database learning classic (from getting started to mastering)
Redis6
如果密钥忘记,多个设备分别不同的密钥,云端是如何同步
The role of @requestmapping in the project and how to use it
[MySQL must know and know] log details
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_ scale_ factor‘
Configure the server environment
配置服务器环境