【LEETCODE】41、905. Sort Array By Parity
package y2019.Algorithm.array; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: SortArrayByParity
* @Author: xiaof
* @Description: 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.
*
* 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.
*
* @Date: 2019/7/3 8:48
* @Version: 1.0
*/
public class SortArrayByParity { public int[] solution(int[] A) {
//先获取所有偶元素,然后获取所有奇元元素,还有一个方法就是交换前后位置
//吧所有偶数放到前面,奇数放后面,类似快排
int index1 = 0, index2 = A.length - 1;
while(index1 < index2) {
//遍历起始第一个不是偶数的位置
while(index1 < A.length && (A[index1] & 1) == 0) {
index1++;
}
//后面往前,找到第一个不是奇数
while(index2 > 0 && (A[index2] & 1) == 1) {
index2--;
}
//交换位置
if(index1 < index2 && index1 < A.length && index2 > 0) {
//交换位置
int temp = A[index1];
A[index1] = A[index2];
A[index2] = temp;
}
} return A;
} public static void main(String args[]) {
int A[] = {3,1,2,4};
SortArrayByParity fuc = new SortArrayByParity();
System.out.println(fuc.solution(A));
}
}
【LEETCODE】41、905. Sort Array By Parity的更多相关文章
- 【LEETCODE】42、922. Sort Array By Parity II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】905. Sort Array By Parity
problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】714、买卖股票的最佳时机含手续费
Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- LeetCode 905. Sort Array By Parity
905. Sort Array By Parity Given an array A of non-negative integers, return an array consisting of a ...
随机推荐
- Pollard-Rho算法求大数质因子
/* * 大整数分解到现在都是世界级的难题,但却是一个重要的研究方向,大整数在公共密钥的研究上有着重要的作用 * Pollard Rho算法的原理就是通过某种方法得到两个整数a和b.而待分解的大整数为 ...
- linux 关闭主板上的蜂鸣器声音
本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/mainboard_speaker_close 在从deepin的 ...
- Java运算符和类型转换
以下代码输出结果是: public class Test { public static void main(String[] args) { int a = 5; System.out.printl ...
- Java实现RS485串口通信,发送和接收数据进行解析
最近项目有一个空气检测仪,需要得到空气检测仪的实时数据,保存到数据库当中.根据了解得到,硬件是通过rs485进行串口通讯的,需要发送16进制命令给仪器,然后通过轮询来得到数据. 需要先要下载RXTX的 ...
- 2D转换模块
2D转换模块(transform) 1.取值:rotate(45deg)--- 旋转,默认以自己为中心作为参考点来旋转 .translate(100px,0px)--- 平移,第一个参数代表水平方向, ...
- log4net 控制台和文件和数据库输出三种方式
1.新建console应用项目SendEvaluateDataToProvinceConsole 2.选择SendEvaluateDataToProvinceConsole项目右键 选择 管理NuGe ...
- 压测引起的 nginx报错 502 no live upstreams while connecting to upstream解决
对系统的某个接口进行极限压测,随着并发量上升,nginx开始出现502 no live upstreams while connecting to upstream的报错,维持最大并发量一段时间,发现 ...
- 针对nginx,来具体聊聊正向代理与反向代理 (转载)
https://www.sohu.com/a/235704408_468627 先来说说什么是代理服务器? 所谓代理服务器就是位于发起请求的客户端与原始服务器端之间的一台跳板服务器,正向代理可以隐藏客 ...
- 008 webpack的其他使用方式
一:配置 1.配置文件 每次修改main文件,重新打包都要指定入口与出口,比较费事,可以使用配置文件的方式 在根目录下新建webpack.config.js: const path = require ...
- JMeter压测“java.net.SocketException: Socket closed”解决方法 - Andrea-Pirlo
报错详情: 引起 java.net.SocketException: Socket closed 错误的原因通常是 未设置连接的超时时间. 解决方法: 该问题可以尝试通过以下方法解决. 如果在 HTT ...