当前位置:网站首页>Leetcode- sort arrays by parity
Leetcode- sort arrays by parity
2022-06-13 08:05:00 【Sharp soldier fruit】
Give you an array of integers nums, take nums Move all even elements in the array to the front of the array , Followed by all odd elements . Returns... That meets this condition Any array As the answer .
Example 1:
Input :nums = [3,1,2,4]
Output :[2,4,3,1]
explain :[4,2,3,1]、[2,4,1,3] and [4,2,1,3] It will also be seen as the right answer .
source : Power button (LeetCode)
Analysis of the title shows that
- Traversal array , Take out even numbers , Insert header data into result array
- Take out the even tail and insert data into the result array
So use JS It's easy to write
/** * @param {number[]} nums * @return {number[]} * @ give an example nums = [3,1,2,4],sortArrayByParity(nums)=[4,2,3,1] */
var sortArrayByParity = function(nums) {
let res=[];
nums.map((item)=>{
if(item%2==0)
{
res.unshift(item);
}
else{
res.push(item);
}
})
return res;
};
Two methods
unshift
Insert data in the head , On the contrary ,shift The header is deleted
push
Insert data at the end , It's also related to unshift contrary
that C Language can use the same idea , It's actually a double pointer , One points to the beginning , One points to the end
/** * Note: The returned array must be malloced, assume caller calls free(). */
int* sortArrayByParity(int* nums, int numsSize, int* returnSize){
int *returnNums = (int *)malloc(sizeof(int) * numsSize);
int *left=returnNums,*right=returnNums+numsSize-1;
int index=0;
while(index<numsSize)
{
if(nums[index]%2==0)
{
*left=nums[index];
left++;
}
else{
*right=nums[index];
right--;
}
index++;
}
*returnSize=numsSize;
return returnNums;
}
Advanced
Sort the array , For convenience nums[i] In an odd number of ,i It's also Odd number ; When nums[i] For even when , i It's also even numbers .
Input :nums = [4,2,5,7]
Output :[4,5,2,7]
explain :[4,7,2,5],[2,5,4,7],[2,7,4,5] It will also be accepted .
You can get by the question
- First judge whether it is odd or even
- Odd number , The subscript is odd , That is from 1 Start , Every time 2
- even numbers , The following table shows even numbers , from 0 Start , Every time 2
C Language
/** * Note: The returned array must be malloced, assume caller calls free(). */
int* sortArrayByParityII(int* nums, int numsSize, int* returnSize){
int *returnNums = (int *)malloc(sizeof(int) * numsSize);
int even=0,odd=1;
int index=0;
while(index<numsSize)
{
if(nums[index]%2==0)
{
returnNums[even]=nums[index];
even+=2;
}
else{
returnNums[odd]=nums[index];
odd+=2;
}
index++;
}
*returnSize=numsSize;
return returnNums;
}
边栏推荐
- OpenHarmony笔记-----------(一)
- Redis Cluster - the underlying principle of cluster execution commands
- set实现名单查找与排除
- Tidb source code series: immersive compilation of tidb
- 【完全信息静态博弈-Nash均衡的特性】
- [redis problem] record a big key problem handling
- 2022 simulated examination question bank and online simulated examination of hoisting machinery command examination questions
- Dfinity (ICP) identity authentication and ledger quick start-3
- Redis Cluster - the underlying principle of slot assignment
- Coalesce() function
猜你喜欢

Disk C is full? A few simple tips teach you to release and clean up tens of gigabytes of space on the C disk, the most effective way to clean up the C disk

Structural analysis of hyperledger fabric (I)

Tidb source code series: immersive compilation of tidb

Index push down (ICP) for mysql5.6

批发商为什么要使用订单系统

【博弈论-完全信息静态博弈】 Nash均衡的应用
![[pytorch] pytorch0.4.0 installation tutorial and GPU configuration collection (including test code)](/img/b4/138b7ae8c73e38663ea84ece79273b.jpg)
[pytorch] pytorch0.4.0 installation tutorial and GPU configuration collection (including test code)

Detailed explanation of digital certificate and Ca

6. fabric2.2 stop clustering and delete data (use the official demo)

22 | 冒险和预测(一):hazard是“危”也是“机”
随机推荐
4. fabric2.2 create and join channels (use the official demo)
24 | 冒险和预测(三):CPU里的“线程池”
ERP基础数据 金蝶
[MySQL] rapid data deletion recovery tool - binlog2sql
Leetcode 163 Missing interval (June 12, 2022)
Success logarithm of leetcode spells and potions
6. fabric2.2 stop clustering and delete data (use the official demo)
适合生鲜批发行业的几种精准接单方式
Give code vitality -- the way to read code neatly
【深度学习】:《PyTorch入门到项目实战》(十二)卷积神经网络:填充(padding)和步幅(stride)
26 | superscalar and VLIW: how to make the CPU throughput exceed 1
基于paddlepaddle的新冠肺炎识别
实践出真知--你的字节对齐和堆栈认知可能是错误的
SolidWorks修改工程图中文字字体的方法
Shell script Basics
22 | adventure and prediction (I): hazard is both "danger" and "opportunity"
钉钉小程序如何隐藏tab
Structural analysis of hyperledger fabric (I)
Coalesce() function
[redis problem] record a big key problem handling