【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 ...
随机推荐
- anki的使用以及anki server的配置
本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/anki_and_anki_server 首先吐槽,anki作为老 ...
- delphi开第二个进程报错cannot create file editorlineends.ttr
网上说问题是windows系统补丁造成的,解决办法有卸补丁.装插件,还有自己搞个bat启动. 在网上看到最好的一个办法是: 把这个文件EditorLineEnds.ttr的后缀改为ttf,然后安装这个 ...
- 数据库sql优化总结之3--SQL优化总结
SQL是每个Java程序员必回的一项技能, 对于项目中的各种复杂业务, 你是否能写出高效率, 简洁的SQL对于项目的运行效率和稳定性是有非常大的作用的. 通过个人的理解和网上的资料总结了一下常见的S ...
- 007 webpack基本的用法
1.安装node.js 因为webpack是基于node.js的 2.新建目录 3.需求 列表的隔行变色 4.初始化 在终端中使用npm init命令可以自动创建这个package.json文件 n ...
- CEF CefSettings 结构体 详解
1. single_process: 设置为ture时,browser和render使用同一个进程.Chromium 不正是支持此运行模式,并且不如默认的多进程稳定. 2. no_sandbox: 沙 ...
- Navicat 破解版链接
本文为转载内容 百度网盘地址: https://pan.baidu.com/s/1nvIIOad 压缩包中有注册码和使用方法
- laravel composer 使用阿里云镜像
使用composer安装错误提示: 即: [Composer\Downloader\TransportException] The "https://packagist.laravel-ch ...
- springboot 整合Elasticsearch
Elasticsearch Elasticsearch 是一个分布式.可扩展.实时的搜索与数据分析引擎. 它能从项目一开始就赋予你的数据以搜索.分析和探索的能力,可用于实现全文搜索和实时数据统计. 在 ...
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode] 399. Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...