当前位置:网站首页>力扣解法汇总905-按奇偶排序数组
力扣解法汇总905-按奇偶排序数组
2022-06-12 02:04:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
给你一个整数数组 nums,将 nums 中的的所有偶数元素移动到数组的前面,后跟所有奇数元素。
返回满足此条件的 任一数组 作为答案。
示例 1:
输入:nums = [3,1,2,4]
输出:[2,4,3,1]
解释:[4,2,3,1]、[2,4,1,3] 和 [4,2,1,3] 也会被视作正确答案。
示例 2:
输入:nums = [0]
输出:[0]
提示:
1 <= nums.length <= 5000
0 <= nums[i] <= 5000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sort-array-by-parity
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 遍历两遍,第一遍找出偶数,第二遍找出奇数
代码:
public class Solution905 {
public int[] sortArrayByParity(int[] nums) {
int[] result = new int[nums.length];
int index = 0;
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
if (num % 2 == 0) {
result[index++] = num;
}
}
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
if (num % 2 != 0) {
result[index++] = num;
}
}
return result;
}
}边栏推荐
- Wide match modifier symbol has been deprecated, do not use
- “中国东信杯”广西大学第四届程序设计竞赛(同步赛)
- matplotlib. pyplot. Bar chart (II)
- Linux (centos7) installer mysql - 5.7
- 力扣解法汇总1037-有效的回旋镖
- Wechat applet - a case of comparing the size of numbers
- LeetCode LCP 07. Deliver information
- CVPR2022 | iFS-RCNN:一种增量小样本实例分割器
- Database
- 力扣解法汇总450-删除二叉搜索树中的节点
猜你喜欢

阿里云oss文件上传系统

Don't miss it! Five large data visualization screens that HR must collect

leetcodeSQL:612. Nearest distance on plane

Graphical data analysis | data analysis tool map

Summary of concrete (ground + wall) + Mountain crack data set (classification and target detection)

Software engineering - system flow chart

How to maximize the use of various matching methods—— Google SEM

How to automatically color cells in Excel

MySQL表常用操作思维导图

kali安装empire过程中遇到的各种报错解决方案
随机推荐
Various error reporting solutions encountered by Kali during Empire installation
How WPS inserts a directory and the operating steps for quickly inserting a directory
初探性能优化!从2个月到4小时的性能提升!
Bracket generation (backtracking)
Introduction to SVM
C asynchronous programming from simple to deep (III) details awaiter
力扣解法汇总433-最小基因变化
MySQL advanced knowledge points
Ozzanation - système d'action basé sur sse
Don't miss it! Five large data visualization screens that HR must collect
A mystery of the end of vagrant up
Redis实现消息队列的4种方案
Database
Graphical data analysis | business analysis and data mining
Wide match modifier symbol has been deprecated, do not use
php安全开放10文章列表模块的分页编写
力扣解法汇总面试题 17.11-单词距离
BaseDexClassLoader那些事
ozzanimation-基於sse的動作系統
[learn FPGA programming from scratch -20]: quick start chapter - operation steps 4-2-quick use of Altera quartz II tool (Modelsim co simulation, program download to altera development board)