当前位置:网站首页>Leetcode:905. Sort array by parity [double pointer + implementation in three languages]
Leetcode:905. Sort array by parity [double pointer + implementation in three languages]
2022-07-20 10:04:00 【Starry sky and bright moon】
Title Description
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 .
Input and output
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 .
Tips
- 1 <= nums.length <= 5000
- 0 <= nums[i] <= 5000
Ideas
Using double pointers to achieve , Similar to quick sort idea , Scan from the left until you encounter an odd number , Scan from the right until you encounter an even number , And then exchange the two numbers .
AC Code (C++)
class Solution {
public:
vector<int> sortArrayByParity(vector<int>& nums) {
int n = nums.size();
int l = 0, r = n - 1;
while(l < r) {
while(l < r && nums[l] % 2 == 0) ++l;
while(l < r && nums[r] % 2 == 1) --r;
if (l < r) {
swap(nums[l], nums[r]);
}
}
return nums;
}
};
AC Code (Java)
class Solution {
public int[] sortArrayByParity(int[] nums) {
int n = nums.length;
int l = 0, r = n - 1;
while(l < r) {
while(l < r && nums[l] % 2 == 0) ++l;
while(l < r && nums[r] % 2 == 1) --r;
if (l < r) {
nums[l] = nums[l] ^ nums[r];
nums[r] = nums[l] ^ nums[r];
nums[l] = nums[l] ^ nums[r];
}
}
return nums;
}
}
AC Code (Python)
class Solution:
def sortArrayByParity(self, nums: List[int]) -> List[int]:
n = len(nums)
l, r = 0, n - 1
while l < r:
while l < r and nums[l] % 2 == 0:
l+=1
while l < r and nums[r] % 2 == 1:
r-=1
if l < r:
nums[l], nums[r] = nums[r], nums[l]
return nums
边栏推荐
- Chrome developer tool shortcut key reference to improve development efficiency
- vulnhub 靶機 Jangow: 1.0.1
- CA运行fft版本
- Detailed explanation of multiresolution decomposition and reconstruction of wavelet analysis
- leetcode-序列和为K的数量-对前缀和和哈希代码的分析
- TCP three handshakes and four swings
- The Debian system is ported with USBWiFi rtl8192eu driver and set to start automatically
- 我的2020年线上的夏令营总结
- Intelligent gateway based on Ruixin micro 3568 core board
- Servlet概述
猜你喜欢
代码审计之企业级进销存管理系统
Reasons and solutions for the whitening of computer shortcuts -- the lesson of blood woo woo
CA gem5 stats.txt 统计信息
2022-7-8 第八小组 顾宇佳 拓展训练
经纬度及其与坐标系的转换
代码审计之oasys系统
TCP three handshakes and four swings
7-1 懂的都懂
Evaluate reading and writing speed and network of Reza rz/g2l storage
LeetCode:第302场周赛(总结)
随机推荐
CA gem5 stats.txt 统计信息
TCP three handshakes and four swings
[DOM] first knowledge of DOM
C语言数据类型及typedef下的uint8_t / uint32_t
ESM测向误差对定位误差的影响分析
雷达基础知识
Many new features of ktor2.0 were thought of a year ago and have been implemented in the project
经纬度及其与坐标系的转换
Leshan normal programming competition 2020-e: divide stones [01 backpacks]
2022-7-8 第八小组 顾宇佳 拓展训练
RISC_V交叉编译环境(国内镜像 避坑)
"Embedded intelligence" constantly empowers medical devices
Bing必应搜索用不了方法
Least square linear fitting and its code implementation (C language)
How to put a "platform" into a small box? (Part 2) technical realization
FreeRTOS thread safe and interrupt safe printf implementation
leetcode-序列和为K的数量-对前缀和和哈希代码的分析
How to set the oil on the through hole cover when exporting the Gerber file of PCB
內網滲透隧道技術的相關知識
【Markdown】关于Markdown我想说这些~