当前位置:网站首页>剑指 Offer 21. 调整数组顺序使奇数位于偶数前面(循环不变量)
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面(循环不变量)
2022-08-09 12:45:00 【养猪去】
class Solution {
public int[] exchange(int[] nums) {
int n = nums.length;
int i = -1, j = 0;
for(; j < n; j++) {
if(nums[j] % 2 == 1) {
int t = nums[++i];
nums[i] = nums[j];
nums[j] = t;
}
}
return nums;
}
}
边栏推荐
猜你喜欢
随机推荐
使用RecyclerView实现三级折叠列表
Rust从入门到精通04-数据类型
How to solve the 0x80070005 error when the computer is reinstalled and the system is restored
Flutter Getting Started and Advanced Tour (3) Text Widgets
WSA toolkit installed app store tip doesn't work how to solve?
read stream special attention
Do you know the difference between comments, keywords, and identifiers?
安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育
gin的中间件和路由分组
某高校的R语言数据分析期末作业
Flutter introduction advanced trip (5) Image Widget
使用注解将EventBus封装抽取到基类
JVM内存泄漏和内存溢出的原因
World's 4th mad scientist dies on his 103rd birthday
批量读取word docx文件指定表格内容,保存在excel文件中
阿里大淘系模型治理阶段性分享
5G China unicom 一般性异常处理
Flutter入门进阶之旅(三)Text Widgets
5G China unicom general exception handling
ftplib+ tqdm 上传下载进度条