当前位置:网站首页>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
边栏推荐
- The collapse of margin
- Use of jumpserver
- Section 7 Data Dictionary: hash followed by Daewoo redis ------- directory post
- 运维小白成长记——架构第8周
- 字符串常用方法(2)
- 【“码”力全开,“章”显实力】2022年第1季Task挑战赛贡献者榜单
- Template inheritance and import
- About the writing method of interface 1 chain interpretation 2. Method execution (finally) must be executed
- Alibaba /166 obtains the API instructions for all products in the store
- Simulation implementation vector
猜你喜欢

The 5th Digital China Construction summit opened in Fuzhou, Fujian
![[opencv] - thresholding](/img/4e/88c8c8063de7cb10e44e76e77dbb8e.png)
[opencv] - thresholding
![[OBS] dependency Library: x264 vs Build](/img/24/4caea5dc6ff092a3161f43b6026d25.png)
[OBS] dependency Library: x264 vs Build

Install jumpserver

【刷题记录】20. 有效的括号

6126. Design food scoring system

Common methods of number and math classes

手写博客平台~第二天

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

web渗透经验汇总ing
随机推荐
How to read "STL source code analysis"?
sklearn 的模型保存与加载使用
Alibaba 1688 keyword search product API usage display
Typora 它依然是我心中的YYDS 最优美也是颜值最高的文档编辑神器 相信你永远不会抛弃它
Bib | mol2context vec: context aware deep network model learning molecular representation for drug discovery
Pytorch的旅程一:线性模型
05mysql lock analysis
jmeter --静默运行
0701~放假总结
下拉列表组件使用 iScroll.js 实现滚动效果遇到的坑
Simulation implementation vector
Cookies and session "suggestions collection"
数组扁平化.flat(Infinity)
Flink operation Hudi data table
Use of jumpserver
Go to bed capacity exchange
JumpServer的使用
Go language file operation
Four ways of simple interest mode
排序的几种方式for while 还有sort