当前位置:网站首页>[leetcode refers to offer 21. Adjust the array order so that odd numbers precede even numbers (simple)]
[leetcode refers to offer 21. Adjust the array order so that odd numbers precede even numbers (simple)]
2022-04-23 21:21:00 【Minaldo7】
subject :
Enter an array of integers , Implement a function to adjust the order of the Numbers in the array , Make all odd numbers in the first half of the array , All even numbers are in the second half of the array .
Example :
Input :nums = [1,2,3,4]
Output :[1,3,2,4]
notes :[3,1,2,4] And one of the right answers .
Tips :
0 <= nums.length <= 50000
0 <= nums[i] <= 10000
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The problem solving process :
Double pointer
class Solution {
public int[] exchange(int[] nums) {
int start = 0, end = nums.length-1, temp = 0;;
while(start < end){
while(start < end && nums[start] % 2 == 1){
start++;
}
while(start < end && nums[end] % 2 == 0){
end--;
}
if(start < end){
temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
}
}
return nums;
}
}
Execution results :
版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479098.html
边栏推荐
- 3-5通过XSS获取cookie以及XSS后台管理系统的使用
- Is qiniu school useful and is the recommended securities account safe
- ubutnu20安裝CenterNet
- MySQL advanced common functions
- Addition, deletion, modification and query of advanced MySQL data (DML)
- Selenium 显示等待WebDriverWait
- 小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌
- Common commands of MySQL in Linux
- Normalized transforms in pytorch The real calculation process of normalize
- The computer is out of power. How did I pass the terrible interview of Tencent cloud?
猜你喜欢
ROS学习笔记-----ROS的使用教程
How to make Jenkins job run automatically after startup
Gsi-ecm digital platform for engineering construction management
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
[leetcode refers to offer 27. Image of binary tree (simple)]
Zhongchuang storage | how to choose a useful distributed storage cloud disk
1.整理华子面经--1
Arm architecture assembly instructions, registers and some problems
Amazon and epic will be settled, and the Microsoft application mall will be opened to third parties
thinkphp5+数据大屏展示效果
随机推荐
South Korea may ban apple and Google from offering commission to developers, the first in the world
软件测试要怎么学?自学还是培训看完这篇文章你就懂了
Sklearn function: Train_ Test_ Split (split training set and test set)
Centralized record of experimental problems
Opencv reports an error. Expected PTR < CV:: UMAT > for argument '% s'‘
GSI-ECM工程建设管理数字化平台
Tensorflow1. X and 2 How does x read those parameters saved in CKPT
Assertionerror: invalid device ID and runtimeerror: CUDA error: invalid device ordinal
MySQL进阶之表的增删改查
laravel 发送邮件
危机即机遇,远程办公效率为何会提升?
Express③(使用Express编写接口、跨域有关问题)
Pipes and xargs
Amazon and epic will be settled, and the Microsoft application mall will be opened to third parties
Mysql database common sense storage engine
学会打字后的思考
unity 功能扩展
ros功能包内自定义消息引用失败
Reference of custom message in ROS function pack failed
thinkphp5+数据大屏展示效果