当前位置:网站首页>Sword finger offer 21. adjust the array order so that odd numbers precede even numbers
Sword finger offer 21. adjust the array order so that odd numbers precede even numbers
2022-07-24 18:19:00 【[email protected]】

https://leetcode.cn/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/
Their thinking : Double finger needling
Left pointer left Traverse from left to right , Stop when an even number is encountered ; Right pointer left Traverse from right to left , Stop when an odd number is encountered ; Exchange the odd and even numbers encountered at the same time
class Solution {
public int[] exchange(int[] nums) {
int left=0,right=nums.length-1;
while(left<right){
while(left<right&&nums[left]%2==1){
left++;
}
while(left<right&&nums[right]%2==0){
right--;
}
swap(nums,left,right);
}
return nums;
}
public void swap(int[] nums,int i,int j){
int tmp=nums[i];
nums[i]=nums[j];
nums[j]=tmp;
}
}
//O(n)
//O(1)
class Solution {
public:
vector<int> exchange(vector<int>& nums) {
int l=0,r=nums.size()-1;
while(l<r){
while(l<r&&nums[l]%2==1){
l++;
}
while(l<r&&nums[r]%2==0){
r--;
}
swap(nums[l],nums[r]);
}
return nums;
}
};
class Solution:
def exchange(self, nums: List[int]) -> List[int]:
left,right=0,len(nums)-1
while left < right:
while left<right and nums[left]%2==1:
left+=1
while left<right and nums[right]%2==0:
right-=1
nums[left],nums[right]=nums[right],nums[left]
return nums
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/205/202207241815443051.html
边栏推荐
- 去不图床容量兑换
- 0615 ~ realize RBAC permission management with user-defined annotations
- Framework introduction
- Inherit, override, overload
- Common methods of number and math classes
- Read zepto source code touch module
- 球面上绘制圆matlab仿真
- How to read "STL source code analysis"?
- 2022 the latest short video de watermarking analysis API interface sharing
- 13 essential methods of color!
猜你喜欢

初识Pytorch和Pytorch环境配置

Bib | mol2context vec: context aware deep network model learning molecular representation for drug discovery

6126. Design food scoring system

Mozilla foundation released 2022 Internet health report: AI will contribute 15.7 trillion yuan to the global economy in 2030, and the investment in AI in the United States last year was nearly three t

Common methods of number and math classes

redis集群的三种方式

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

Install jumpserver

Three ways of redis cluster

The 5th Digital China Construction summit opened in Fuzhou, Fujian
随机推荐
middleware
[OBS] cooperation between video and audio coding and RTMP transmission
Example of single table query in ORM student management system
The drop-down list component uses iscrol JS to achieve the rolling effect of the pit encountered
移动端实现0.5px的实用方案
[OBS] dependency Library: x264 vs Build
0611~自习课
【OpenCV】—阈值化
web渗透经验汇总ing
[verification] only numbers (positive and negative numbers) can be entered
0630~ professional quality course
Int8 & int8, have you ever stumbled like this?
New can also create objects. Why do you need factory mode?
Laravel notes - RSA encryption of user login password (improve system security)
Icml2022 Best Paper Award: learning protein reverse folding from millions of predicted structures
Go language file operation
手写博客平台~第二天
redis集群的三种方式
数组扁平化.flat(Infinity)
About the writing method of interface 1 chain interpretation 2. Method execution (finally) must be executed