当前位置:网站首页>Sword finger offer 21 Adjust array order so that odd numbers precede even numbers
Sword finger offer 21 Adjust array order so that odd numbers precede even numbers
2022-06-11 08:43:00 【zmm_ mohua】
The finger of the sword Offer 21. Adjust the array order so that the odd Numbers precede the even Numbers
subject

Code
#include <iostream>
#include <vector>
using namespace std;
// Method 1 : Time limit exceeded
vector<int> exchange1(vector<int>& nums) {
int n = nums.size();
if(n == 0 || n == 1){
return nums;
}
for(int i = 0; i < n - 1; i++){
if(nums[i] % 2 == 1){
continue;
}
for(int j = i + 1; j < n; j++){
if(nums[j] % 2 == 1){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
break;
}
}
}
return nums;
}
// Method 2 : Using the array , Odd number from to save , Even numbers are saved from back to front
vector<int> exchange(vector<int>& nums) {
int n = nums.size();
if(n == 0 || n == 1){
return nums;
}
vector<int> res (n);
int j = 0, k = n - 1;
for(int i = 0; i < n; i++){
if(nums[i] % 2 == 1){
res[j++] = nums[i];
}else{
res[k--] = nums[i];
}
}
return res;
}
int main(){
int n;
cin>>n;
vector<int> nums(n);
vector<int> res;
for(int i = 0; i < n; i++){
cin>>nums[i];
}
res = exchange(nums);
for(int i = 0; i < res.size(); i++){
cout<<res[i]<<" ";
}
return 0;
}
边栏推荐
- node报错整理
- (resolved) pychart debug error -unicode decodeerror: 'UTF-8' codec can't decode byte 0xe8 in position 1023
- CentOS essay 03:centos8.2 installing MySQL
- Icml2022 interesting articles
- Go language: string connection, digital conversion string
- Process control: process waiting (recycling child processes)
- Jupyter notebook code completion plug-in + Solution
- Cyclic sort
- 盘它!用「飞项」轻松管理各类型项目
- How CSDN reports plagiarized articles
猜你喜欢

leetcode - 460. LFU 缓存

What is concurrent search set? Are you still worried about it? In fact, it is a problem of connected graph, which is not so difficult to understand

Idea pulls items from remote warehouse

leetcode - 518. 零钱兑换 II

Hibernate L2 cache

Introduction to database system experiment report answer Experiment 5: database single table query

Difference between threadpooltaskexecutor and ThreadPoolExecutor

uniapp 插件开发

B+ super tree helps you know the underlying structure of MySQL

Web design and website planning assignment 11 game selection form
随机推荐
知识图谱入门之---yedda标注
盘它!用「飞项」轻松管理各类型项目
Solve valueerror: no model found in config file
for in / for of / forEach 循环
Idea annotation settings
指定环境下安装Jupyter
结果和目标出入太大?不妨借助目标管理精准直达目标!
Timestamp of PostgreSQL and Oracle
torch. unbind()
@Usage details of postconstruct, initializingbean and initmethod
Codeworks round 680 div2
Use of Excel to XML tool of TestLink
Can not connect to local MySQL server through socket ‘/tmp/mysql. sock (2)‘
TRUNC in pytorch_ normal_ principle
【CVPR2022】QueryDet论文精读
【node】npm部分
Introduction to database system experiment report answer Experiment 5: database single table query
Don't want the project out of control? You need to use project management tools
ActiveMQ simple tutorial, suitable for beginners, learning notes yyds
Testing firebase with postman