当前位置:网站首页>LeetCode(剑指 Offer)- 21. 调整数组顺序使奇数位于偶数前面
LeetCode(剑指 Offer)- 21. 调整数组顺序使奇数位于偶数前面
2022-08-08 17:24:00 【放羊的牧码】
题目链接:点击打开链接
题目大意:略
解题思路:略
相关企业
- 字节跳动
AC 代码
- Java
// 解决方案(1)
class Solution {
public int[] exchange(int[] nums) {
int len = nums.length;
List<Integer> l1 = new ArrayList<>();
List<Integer> l2 = new ArrayList<>();
for (int i = 0; i < len; i++) {
if (nums[i] % 2 != 0) {
l1.add(nums[i]);
} else {
l2.add(nums[i]);
}
}
l1.addAll(l2);
int[] rs = new int[len];
for (int i = 0; i < l1.size(); i++) {
rs[i] = l1.get(i);
}
return rs;
}
}
// 解决方案(2)
class Solution {
public int[] exchange(int[] nums) {
int i = 0, j = nums.length - 1, tmp;
while(i < j) {
while(i < j && (nums[i] & 1) == 1) i++;
while(i < j && (nums[j] & 1) == 0) j--;
tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
return nums;
}
}
- C++
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++;
while(i < j && (nums[j] & 1) == 0) j--;
swap(nums[i], nums[j]);
}
return nums;
}
};
边栏推荐
- 史上最强IDEA工具使用教程,你想要的全都有!
- 字节一面:TCP 和 UDP 可以使用同一个端口吗?
- 10 种最佳 IDE 软件 ,你更忠爱哪一个?
- 4. S32K14X study notes: S32 Design Studio new and imported projects
- Tensorflow教程(五)——MNIST项目提高
- arm交叉编译
- XDOJ - count the number of positive integers
- 【数学模型】灰色关联分析
- Tensorflow教程(四)——MNIST项目入门
- VISTA无人驾驶模拟器;FinRL量化金融深度强化学习库;『深度神经网络应用』电子书;CUDA/TensorRT案例集锦;前沿论文 | ShowMeAI资讯日报
猜你喜欢
DSPE-PEG-Biotin,385437-57-0,磷脂-聚乙二醇-生物素用于生物分子的检测和纯化
4. S32K14X study notes: S32 Design Studio new and imported projects
【数学模型】灰色关联分析
R file not found problem
Cholesterol-PEG-DBCO,CLS-PEG-DBCO,胆固醇-聚乙二醇-二苯基环辛炔一种环炔烃
MySQL 表的增删查改
【20210923】Choose the research direction you are interested in?
转载,fragment讲的很好的大佬
通俗易懂的epoll
[Paper Reading] RAL 2022: Receding Moving Object Segmentation in 3D LiDAR Data Using Sparse 4D Convolutions
随机推荐
C人脸识别
D2. Sage‘s Birthday (hard version)
多线程-并发编程
Chapter 20 Source Code File REST API Reference (2)
mysql进阶(二十九)常用函数汇总
1.初识MySQL数据库
六、Jmeter定时器
文件操作和IO
Detailed explanation of JVM memory model and structure (five model diagrams)
The difference between B+ tree and B- tree
L2-008 最长对称子串 (25 分)
4. S32K14X study notes: S32 Design Studio new and imported projects
敏捷开发项目管理的一些心得
1dp到底多大!
【数学模型】TOPSIS
VISTA无人驾驶模拟器;FinRL量化金融深度强化学习库;『深度神经网络应用』电子书;CUDA/TensorRT案例集锦;前沿论文 | ShowMeAI资讯日报
L2-013 红色警报 (25 分)(并查集)
KITTI数据集简介(一)—— 传感器介绍
Appium 自动化测试环境搭建
socket concept