当前位置:网站首页>[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
边栏推荐
- 深入探究ASP.NET Core读取Request.Body的正确方式
- Two Stage Detection
- ubutnu20安裝CenterNet
- Pipes and xargs
- [leetcode refers to offer 32 - III. print binary tree III from top to bottom (medium)]
- Question brushing plan -- backtracking method (I)
- 41. 缺失的第一个正数
- Deep analysis of C language function
- [※ leetcode refers to offer 32 - II. Print binary tree II from top to bottom (simple)]
- Express ③ (use express to write interface and cross domain related issues)
猜你喜欢
Reentrant function
Prim、Kruskal
1.整理华子面经--1
41. The first missing positive number
thinkphp5+数据大屏展示效果
MySQL basic collection
Deep analysis of C language pointer (Part I)
Is rust more suitable for less experienced programmers?
Google tries to use rust in Chrome
CUDA, NVIDIA driver, cudnn download address and version correspondence
随机推荐
Normalized transforms in pytorch The real calculation process of normalize
How to make Jenkins job run automatically after startup
Thinkphp5 + data large screen display effect
Problem brushing plan -- dynamic programming (IV)
Getting started with detectron2
Sklearn function: Train_ Test_ Split (split training set and test set)
What about laptop Caton? Teach you to reinstall the system with one click to "revive" the computer
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
unity 功能扩展
Flomo software recommendation
IOT design and development
Opencv reports an error. Expected PTR < CV:: UMAT > for argument '% s'‘
Minecraft 1.12.2 module development (43) custom shield
Selenium 显示等待WebDriverWait
Alibaba cloud responded to the disclosure of user registration information
wait、waitpid
亚马逊和Epic将入驻,微软应用商城向第三方开放
3-5通过XSS获取cookie以及XSS后台管理系统的使用
flomo软件推荐
Solve importerror: cannot import name 'imread' from 'SciPy misc‘