当前位置:网站首页>力扣解法汇总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;
}
}边栏推荐
- BaseDexClassLoader那些事
- LeetCode Algorithm 1791. Find the central node of the star chart
- ACL 2022 | 预训练语言模型和图文模型的强强联合
- What insurance products can you buy at the age of 60?
- [untitled]
- Glfwpollevents() program crash
- Ozzanation - système d'action basé sur sse
- [adjustment] in 2022, the Key Laboratory of laser life sciences of the Ministry of education of South China Normal University enrolled adjustment students in optics, electronic information, biomedicin
- The establishment and introduction of the announcement module of PHP development blog system
- Database
猜你喜欢

MySQL table common operation mind map

Graphic data analysis | business cognition and data exploration

Does the virtual host have independent IP

Smartbi helps you solve the problem of losing high-value customers

Graphic data analysis | data cleaning and pretreatment

Fatal error in launcher: unable to create process using

Graphical data analysis | business analysis and data mining

Knowledge points of mall development

Redis cluster + sentinel mode + replicas

程序员应该如何解决买菜难问题?手把手带你利用无障碍辅助功能快速下单抢菜
随机推荐
Fatal error in launcher: unable to create process using
Master of a famous school has been working hard for 5 years. AI has no paper. How can the tutor free range?
Subject knowledge and educational ability of information technology in the first half of 2019 – subjective questions
Modification of system module information of PHP security development 12 blog system
Bracket generation (backtracking)
力扣解法汇总713- 乘积小于 K 的子数组
竞价广告每次点击出价多少钱是固定的吗?
如何提高广告的广告评级,也就是质量得分?
Explore performance optimization! Performance improvement from 2 months to 4 hours!
如何定位关键词使得广告精准投放。
微信公众号开发地理位置坐标的转换
php安全开放10文章列表模块的分页编写
MySQL高级部分知识点
力扣解法汇总944-删列造序
混泥土(地面+墙面)+ 山体裂缝数据集汇总(分类及目标检测)
力扣解法汇总面试题 01.05. 一次编辑
ACL 2022 | 预训练语言模型和图文模型的强强联合
Does the virtual host have independent IP
力扣解法汇总699-掉落的方块
力扣解法汇总449-序列化和反序列化二叉搜索树