首页 > 其他分享 >LeetCode 905. Sort Array By Parity

LeetCode 905. Sort Array By Parity

时间:2024-04-01 11:00:53浏览次数:18  
标签:Sort Parity return temp nums int length swap Array

原题链接在这里:https://leetcode.com/problems/sort-array-by-parity/description/

题目:

Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers.

Return any array that satisfies this condition.

Example 1:

Input: nums = [3,1,2,4]
Output: [2,4,3,1]
Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.

Example 2:

Input: nums = [0]
Output: [0]

Constraints:

  • 1 <= nums.length <= 5000
  • 0 <= nums[i] <= 5000

题解:

Could use two pointers left from the begining and right from the end, both move to the middle and do swap when l points to odd number and right points to even number.

Time Complexity: O(n). n = nums.length.

Space: O(1).

AC Java:

 1 class Solution {
 2     public int[] sortArrayByParity(int[] nums) {
 3         if(nums == null || nums.length == 0){
 4             return nums;
 5         }
 6 
 7         int l = 0;
 8         int r = nums.length - 1;
 9         while(l < r){
10             while(l < r && nums[l] % 2 == 0){
11                 l++;
12             }
13 
14             while(l < r && nums[r] % 2 == 1){
15                 r--;
16             }
17 
18             if(l < r){
19                 swap(nums, l++, r--);
20             }
21         }
22 
23         return nums;
24     }
25 
26     private void swap(int[] nums, int i, int j){
27         int temp = nums[i];
28         nums[i] = nums[j];
29         nums[j] = temp;
30     }
31 }

If we want to keep the original order of even numbers, we could have both pointers moving to the same direction.

Time Complexity: O(n). n = nums.length.

Space: O(1).

AC Java:

 1 class Solution {
 2     public int[] sortArrayByParity(int[] nums) {
 3         if(nums == null || nums.length == 0){
 4             return nums;
 5         }
 6 
 7         int ind = 0;
 8         for(int i = 0; i < nums.length; i++){
 9             if(nums[i] % 2 == 0){
10                 swap(nums, ind++, i);
11             }
12         }
13 
14         return nums;
15     }
16 
17     private void swap(int[] nums, int i, int j){
18         int temp = nums[i];
19         nums[i] = nums[j];
20         nums[j] = temp;
21     }
22 }

 

标签:Sort,Parity,return,temp,nums,int,length,swap,Array
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18107979

相关文章

  • NumPy库中的ndarray对象——ndarray的切片和索引
    一、普通切片与索引ndarray对象的内容可以通过索引或切片来访问和修改,与Python中list的切片操作一样。ndarray数组可以基于0-n的下标进行索引,切片对象可以通过内置的slice函数,并设置start,stop及step参数进行,从原数组中切割出一个新数组。我们来介绍一下sta......
  • Python大数据为啥一定要用Numpy Array,靠着这份900多页的PDF面试整理
    1.内存占用更小适当地使用Numpy数组替代List,你能让你的内存占用降低20倍。对于Python原生的List列表,由于每次新增对象,都需要8个字节来引用新对象,新的对象本身占28个字节(以整数为例)。所以列表list的大小可以用以下公式计算:64+8*len(lst)+len(lst)*28字节而使......
  • 多目标应用:基于非支配排序的蜣螂优化算法(Non-Dominated Sorting Dung beetle optimize
    一、柔性作业车间调度问题柔性作业车间调度问题(FlexibleJobSchedulingProblem,FJSP)的描述如下:n个工件{J,J......
  • darknet | darknet之nms do_nms_sort详解
    在yolo模型inference执行完成后,会产生很多的冗余结果,此时就需要调用nms对冗余结果进行去重nms函数在darknet框架中是do_nms_sort函数,位于box.c文件中,源码如下:voiddo_nms_sort(detection*dets,inttotal,intclasses,floatthresh){inti,j,k;......
  • Yii2-助手类(ArrayHelper)
    Yii2-助手类(ArrayHelper)数组助手类ArrayHelperYii数组助手类提供了额外的静态方法,让你更高效的处理数组。模型转数组$model=Country::findOne(['code'=>'BR']);VarDumper::dump(ArrayHelper::toArray($model));//['code'=>'BR''name'=&g......
  • Array方法总结(三)
    Array.prototype.slice()返回一个新的数组对象。从原数组的start和end(不包括end)索引范围内浅拷贝。slice(start,end)constfruits=["Banana","Orange","Lemon","Apple","Mango"];constcitrus=fruits.slice(1,3);//fruits包含['......
  • Array方法总结(一)
    Array.prototype.at()at(index) 传入一个整数值参数,返回该索引对应的元素。传入负整数从数组最后一个元素开始倒数。constarray=[10,8,2];console.log(array.at(-2));//8Array.prototype.concat()合并两个或多个数组。返回一个新数组。constarray1=[7,[1,......
  • sort函数对vector一维或者二维数组排序
    目录sort对一维数组排序1、sort对一位数组升序排序2、sort对一维数组降序排序sort对二维数组排序1、sort默认对横坐标进行升序排序,如下:2、使用自定义排序对纵坐标进行升序排序:额外知识:对横坐标进行降序排列,当横坐标相同时,对纵坐标进行升序排序sort对一维数组排序......
  • lodash已死?radash最全使用介绍(附源码详细说明)—— Array方法篇(1)
    相信很多前端同学甚至非前端都或多或少使用过lodash库,我们都知道lodash是一个非常丰富的前端工具库,比如最常用的防抖和节流,使用lodash都能很快实现,在github上更是有着58.7k的star数。但最近出现的Radash库,号称lodashplus版本,比之更新、更小、更全面、源码更易于理解。阅读本文......
  • 双端队列Deque——ArrayDeque的实现
    Deque接口表示一个双端队列(DoubleEndedQueue),允许在队列的首尾两端操作,所以既能实现队列行为,也能实现栈行为。Deque常用的两种实现ArrayDeque和LinkedList,这篇主要介绍下Deque的常用操作,并重点看下ArrayDeque的实现逻辑。1、接口API1.1、Queue接口Queue的API......