#Leetcode# 922. Sort Array By Parity II
https://leetcode.com/problems/sort-array-by-parity-ii/
Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.
Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i]is even, i is even.
You may return any answer array that satisfies this condition.
Example 1:
Input: [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
Note:
2 <= A.length <= 20000A.length % 2 == 00 <= A[i] <= 1000
代码:
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A) {
int n = A.size();
vector<int> ans;
vector<int> isodd; vector<int> nodd;
for(int i = 0; i < n; i ++) {
if(A[i] % 2) isodd.push_back(A[i]);
else nodd.push_back(A[i]);
}
int a = isodd.size() - 1, b = nodd.size() - 1;
for(int i = 0; i < n; i ++) {
if(i % 2) {ans.push_back(isodd[a]); a --;}
else {ans.push_back(nodd[b]); b --;}
}
return ans;
}
};
这个题好无聊哦。。。
#Leetcode# 922. Sort Array By Parity II的更多相关文章
- LeetCode 922. Sort Array By Parity II C++ 解题报告
922. Sort Array By Parity II 题目描述 Given an array A of non-negative integers, half of the integers in ...
- [LeetCode] 922. Sort Array By Parity II 按奇偶排序数组之二
Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...
- LeetCode 922 Sort Array By Parity II 解题报告
题目要求 Given an array A of non-negative integers, half of the integers in A are odd, and half of the i ...
- 【LEETCODE】42、922. Sort Array By Parity II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】922. Sort Array By Parity II
problem 922. Sort Array By Parity II solution1: class Solution { public: vector<int> sortArray ...
- 【LeetCode】922. Sort Array By Parity II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...
- 【leetcode】922. Sort Array By Parity II
题目如下: 解题思路:非常简单的题目,引入两个变量oddInx = 1和evenInx = 0,和与A等长的结果数组res.然后遍历A,如果A[i]为偶数,则令res[evenInx] = A[i], ...
- 【leetocde】922. Sort Array By Parity II
Given an array of integers nums, half of the integers in nums are odd, and the other half are even. ...
- 992. Sort Array By Parity II - LeetCode
Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一半是偶数,分别对偶数数和奇数数排序并要求这个数本身是偶数要放在偶数 ...
随机推荐
- C语言基础篇(零)gcc编译和预处理
导航: 1. gcc编译 2. 预处理 ----->x<------------->x<--------------->x<--------------->x ...
- ASP.NET 并发控制
当多个用户试图同时修改数据时,需要建立控制机制来防止一个用户的修改对同时操作的其他用户所作的修改产生不利的影响.处理这种情况的系统叫做“并发控制”. 并发控制的类型 通常,管理数据库中的并发有三种常见 ...
- 20155329胡佩伦《Java程序设计》第2周学习总结
学号 20155329 <Java程序设计>第2周学习总结 教材学习内容总结 基本类型 整数(short.int.long) 字节(byte) 浮点数(float/double) 字符(c ...
- js日期控件遇到的问题
一.问题: 在web项目里有很多时候需要使用日期控件来完成相关的功能,但是日期控件的日期格式又和我们的需求不符 那么,就需要我们来自定义日期的格式完成需求 二.解决: 1.取月末: (1)强制取值: ...
- day9 匿名函数 lambda
1. list列表排序 #### sort排序 nums = [,,,,,] nums.sort() print(nums) ### 结果 [, , , , , ] ######## 逆序 In [] ...
- codevs 5429 多重背包
5429 多重背包 http://codevs.cn/problem/5429 分析: f[i]=g[j-k*siz[i]]+k*val[i]; 发现一个状态d只会更新,d+siz[i],d+2*si ...
- Drupal8 使用模块的配置文件
D8中移除了variable表及相关方法 (variable_get(),variable_set()等) .用config表取代了. 新的方法该如何使用? 以D8的Youtube模块为例 配置文件要 ...
- cogs87 乘积最大
cogs87 乘积最大 原题链接 题解 竟然不用高精... f[i][j]表示前i位数j个乘号的最大数f[i][j]=max{f[i-l][j-1]*num[i-l+1][i]} num[a][b]表 ...
- 【MySQL数据库权限】RDS for MySQL创建高权限账号
原文转自:https://help.aliyun.com/document_detail/26130.html?spm=5176.2020520104.201.1.580be8abjlGorJ 为满足 ...
- javaweb(十五)——JSP基础语法
任何语言都有自己的语法,JAVA中有,JSP虽然是在JAVA上的一种应用,但是依然有其自己扩充的语法,而且在JSP中,所有的JAVA语句都可以使用. 一.JSP模版元素 JSP页面中的HTML内容称之 ...