LeetCode 905. Sort Array By Parity
905. Sort Array By Parity
Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.
You may return any answer array that satisfies this condition.
Example 1:
Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Note:
1 <= A.length <= 50000 <= A[i] <= 5000
题目描述:题意是要求给数组排序,排序的原则是偶数在前,奇数在后。
题目分析:很简单,我们直接给数组分分类就好了,然后用一个新的数组去存取值就行了。
python 代码:
class Solution(object):
def sortArrayByParity(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
odd_list = []
even_list = []
final_list = []
A_length = len(A)
for i in range(A_length):
if A[i] % 2 == 0:
even_list.append(A[i])
else:
odd_list.append(A[i])
final_list = even_list + odd_list
return final_list
C++ 代码:
class Solution {
public:
vector<int> sortArrayByParity(vector<int>& A) {
vector<int> final_list(A.size());
int count_odd = 0;
int count_even = 0;
for(int i = 0; i < A.size(); i++){
if(A[i] % 2 == 0){
count_even++;
}
else{
count_odd++;
}
}
vector<int> odd_list(count_odd);
vector<int> even_list(count_even);
int odd = 0;
int even = 0;
for(int i = 0; i < A.size(); i++){
if(A[i] % 2 == 0){
even_list[even++] = A[i];
}
else{
odd_list[odd++] = A[i];
}
}
final_list = even_list;
final_list.insert(final_list.end(),odd_list.begin(),odd_list.end());
return final_list;
}
};
LeetCode 905. Sort Array By Parity的更多相关文章
- [LeetCode] 905. Sort Array By Parity 按奇偶排序数组
Given an array A of non-negative integers, return an array consisting of all the even elements of A, ...
- LeetCode 905 Sort Array By Parity 解题报告
题目要求 Given an array A of non-negative integers, return an array consisting of all the even elements ...
- LeetCode 905. Sort Array By Parity 按奇偶校验排列数组
题目 Given an array A of non-negative integers, return an array consisting of all the even elements of ...
- [leetcode] 905. Sort Array By Parity [easy]
原题链接 很水的一道题,就是数组内部交换. 水题就想着减少复杂度嘛,于是学到一种交换写法. class Solution { public: vector<int> sortArrayBy ...
- 【LEETCODE】41、905. Sort Array By Parity
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 905. Sort Array By Parity - LeetCode
Question 905. Sort Array By Parity Solution 题目大意:数组排序,偶数放前,奇数在后,偶数的数之间不用管顺序,奇数的数之间也不用管顺序 思路:建两个list, ...
- 【Leetcode_easy】905. Sort Array By Parity
problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...
- [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 C++ 解题报告
922. Sort Array By Parity II 题目描述 Given an array A of non-negative integers, half of the integers in ...
随机推荐
- JHipster生成单体架构的应用示例
本文演示如何用JHipster生成一个单体架构风格的应用. 环境需求:安装好JHipster开发环境的CentOS 7.4(参考这里) 应用名:app1 实体名:role 主机IP:192.168.2 ...
- 编写脚本实现DHCP服务与DHCP中继自动化执行
编写脚本实现DHCP服务与DHCP中继自动化执行 本脚本是在liunx搭建DHCP服务器以及DHCP中继服务器实验环境下实现的https://www.cnblogs.com/yuzly/p/10539 ...
- vbs脚本,电脑说出指定内容
新建文件, 输入一下脚本, 然后保存文件为 .vbs 文件, 双击脚本, 电脑就能听到电脑发出 你好 的声音 CreateObject("SAPI.SpVoice").Speak& ...
- C#中Request.ServerVariables详细说明及代理
Request.ServerVariables("Url") 返回服务器地址 Request.ServerVariables("Path_Info") 客户端提 ...
- 设计模式のProxyPattern(代理模式)----结构模式
一.产生背景 在直接访问对象时带来的问题,比如说:要访问的对象在远程的机器上.在面向对象系统中,有些对象由于某些原因(比如对象创建开销很大,或者某些操作需要安全控制,或者需要进程外的访问),直接访问会 ...
- zuul重试配置
#retry#该参数用来开启重试机制spring.cloud.loadbalancer.retry.enabled=true#断路器的超时时间,断路器的超时时间需要大于ribbon的超时时间,不然不会 ...
- springcloud(十五):Spring Cloud 终于按捺不住推出了自己的服务网关 Gateway
Spring 官方最终还是按捺不住推出了自己的网关组件:Spring Cloud Gateway ,相比之前我们使用的 Zuul(1.x) 它有哪些优势呢?Zuul(1.x) 基于 Servlet,使 ...
- centos7下kubernetes(7.kubernetesScale Up/Down)
伸缩(Scale up/down)是指在线增加或减少pod副本数量 通过yml文件创建两个nginx的pod 先查看一下nginx的yml文件: 通过kubectl apply -f创建 通过kube ...
- servlet是线程安全的么
servlet生命周期 三个重要方法 1 init() 进行资源的加载 2 service() 处理请求,根据请求方式,调用doGet或者doPost 3 destroy() 进行资源的释放 ser ...
- Python中的单例模式——装饰器实现剖析
Python中单例模式的实现方法有多种,但在这些方法中属装饰器版本用的广,因为装饰器是基于面向切面编程思想来实现的,具有很高的解耦性和灵活性. 单例模式定义:具有该模式的类只能生成一个实例对象. 先将 ...