当前位置:网站首页>[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 16:10:00 【LuZhouShiLi】
Interview questions 21- Adjust the array order so that the odd Numbers precede the even Numbers
One 、 subject

Two 、 Ideas
initialization :I,J Double pointer , Point to the array nums Left and right ends
Circular exchange : When i >= j Out of the loop
- The pointer i When an odd number is encountered, execute i = i + 1 skip , Until you find an even number
- The pointer J If an even number is encountered, execute j = j - 1 skip , Until you find an odd number
- In exchange for Nums[i] and nums[j] value
Return value : Return modified nums Array
3、 ... and 、 Code
class Solution {
public:
vector<int> exchange(vector<int>& nums) {
int i = 0,j = nums.size() - 1;
while(i < j)
{
while(i < j && (nums[i] & 1) == 1) i++;// Description encountered an odd number direct ++ And higher computational efficiency
while(i < j && (nums[j] & 1) == 0) j--; // Description encountered an even number direct --
swap(nums[i],nums[j]);
}
return nums;
}
};
边栏推荐
- Talk about data center network again
- Overview and example analysis of opengauss database performance tuning
- 时间复杂度与空间复杂度解析
- 面试高频算法题---最长回文子串
- Toolbar details of user interface - autorunner automated test tool
- Project workspace creation steps - Zezhong ar automated test tool
- How the autorunner automated test tool creates a project -alltesting | Zezhong cloud test
- Database design recommendations
- Heartless sword English Chinese bilingual poem 001 Spring outing
- It's really not human to let the express delivery arrive before the refund
猜你喜欢

AutoRunner自动化测试工具如何创建项目-Alltesting|泽众云测试

干掉 Swagger UI,这款神器更好用、更高效!

面试经典题目:怎么做的性能测试?【杭州多测师】【杭州多测师_王sir】
![Interview classic question: how to do the performance test? [Hangzhou multi surveyors] [Hangzhou multi surveyors \wang Sir]](/img/ea/2c5b48b08a9654b61694b93a2e7d0a.png)
Interview classic question: how to do the performance test? [Hangzhou multi surveyors] [Hangzhou multi surveyors \wang Sir]

Opengauss database JDBC environment connection configuration (eclipse)

Nat commun | language model can learn complex molecular distribution

Using cloud DB to build apps quick start - quick games

如何优化 Compose 的性能?通过「底层原理」寻找答案 | 开发者说·DTalk

Princeton Dengjia student's personal account: must I have a doctorate? No, I can also be an applied scientist in a large factory as an undergraduate

Factory calibrated gravity: working principle and installation position of carbon monoxide sensor, calibration standard description
随机推荐
[Yugong series] June 2022 Net architecture class 076- execution principle of distributed middleware schedulemaster
Database design recommendations
laravel 2020-01-01T00:00:00.000000Z 日期转化
What if the win10 security center cannot be closed
Code farming essential SQL tuning (Part 2)
Opengauss database flashback function verification
CLP information - financial standardization "14th five year plan" highlights data security
Overview and operation of database dense equivalent query
再聊数据中心网络
Laravel 8 uses passport for auth authentication and token issuance
Kill the swagger UI. This artifact is better and more efficient!
Database resource load management (Part 1)
项目经理如何击退被工作汇报支配的恐惧感?
GO语言-值类型和引用类型
Nat commun | language model can learn complex molecular distribution
Collection | can explain the development and common methods of machine learning!
Opengauss version 3.0.0 was officially released, and immediately experience the first lightweight version in the community
Memory optimization table mot management
CLP information - No. 1 central document on Strengthening Rural Revitalization financial services
Time processing logic for the last 7 days, the last 10 days, and the last 90 days