首页 > 其他分享 >[LeetCode] 2149. Rearrange Array Elements by Sign

[LeetCode] 2149. Rearrange Array Elements by Sign

时间:2023-11-04 13:22:06浏览次数:27  
标签:integers Elements nums int positive Sign Rearrange ++ 数组

You are given a 0-indexed integer array nums of even length consisting of an equal number of positive and negative integers.

You should rearrange the elements of nums such that the modified array follows the given conditions:
Every consecutive pair of integers have opposite signs.
For all integers with the same sign, the order in which they were present in nums is preserved.
The rearranged array begins with a positive integer.
Return the modified array after rearranging the elements to satisfy the aforementioned conditions.

Example 1:
Input: nums = [3,1,-2,-5,2,-4]
Output: [3,-2,1,-5,2,-4]
Explanation:
The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4].
The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4].
Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions.

Example 2:
Input: nums = [-1,1]
Output: [1,-1]
Explanation:
1 is the only positive integer and -1 the only negative integer in nums.
So nums is rearranged to [1,-1].

Constraints:
2 <= nums.length <= 2 * 105
nums.length is even
1 <= |nums[i]| <= 105
nums consists of equal number of positive and negative integers.

按符号重排数组。

给你一个下标从 0 开始的整数数组 nums ,数组长度为 偶数 ,由数目相等的正整数和负整数组成。

你需要 重排 nums 中的元素,使修改后的数组满足下述条件:

  1. 任意 连续 的两个整数 符号相反
  2. 对于符号相同的所有整数,保留 它们在 nums 中的 顺序 。
  3. 重排后数组以正整数开头。
    重排元素满足上述条件后,返回修改后的数组。

思路是双指针。这里我们需要两个指针 a 和 b 分别去找 input 数组里出现的正数和负数,然后按照规则放入结果里。因为最后要求输出的数组要求正负数交替出现且先出现正数,所以代码里就可以直接先找正数,再找负数。

时间O(n)
空间O(n)
Java实现

class Solution {
    public int[] rearrangeArray(int[] nums) {
        int p = 0;
        int a = 0;
        int b = 0;
		int[] res = new int[nums.length];
        while (p < nums.length) {
            while (nums[a] < 0) {
                a++;
            }
            res[p++] = nums[a++];
            while (nums[b] > 0) {
                b++;
            }
            res[p++] = nums[b++];
        }
        return res;
    }
}

标签:integers,Elements,nums,int,positive,Sign,Rearrange,++,数组
From: https://www.cnblogs.com/cnoodle/p/17809223.html

相关文章

  • ASP.NET Core+Vue3 实现SignalR通讯
    从ASP.NETCore3.0版本开始,SignalR的Hub已经集成到了ASP.NETCore框架中。因此,在更高版本的ASP.NETCore中,不再需要单独引用Microsoft.AspNetCore.SignalR包来使用Hub。在项目创建一个类继承Hub,首先是写一个CreateConnection方法ConnectionId是SignalR中标识的客户端连接的唯......
  • Altium Designer批量修改原理图参数
    1、实现目标:原理图的元器件参数批量修改批量修改元器件value值批量隐藏(例如元器件具体名称)批量修改管脚名称2、参考原理图:3、操作:批量修改元器件value值(注:这样的操作可以避免一个一个的点击元器件进行修改,可以在一个页面进行全部修改)此方法非批量修改成同一个参......
  • powerdesigner 数据库建模
    作用,用于数据库建模,形成pdm图,生成数据表或者逆向工程到设计图上1、cdm数据库建模文件表格代表实体,线条代表关系,建模不是建数据库,数据库的一种抽象当前只是建模阶段,不清楚具有以后要向哪种数据库中去生成表,所以这里的数据类型都是一种具体数据库类型的代替名称。name属性的中文名c......
  • uni.uploadFile和this.$refs.signatureRef.canvasToTempFilePath
    canvasToTempFilePath生成的图片是临时h5路径可用于临时回显,如果图片的路径要上传接口,需要使用uni.uploadFile来将图片上传到服务器//我用uniapp做app签名时写的代码片段,上传完服务器之后的路径就可以传到后端给的接口啦,然后在查询的时候就可以通过订单返回的图片路径进行回显t......
  • postman Pre-request Script(预处理)post请求获取sign(接口鉴权)
    背景请求业务接口时需要先调用auth应用的鉴权接口获取sign(类似其他系统登录接口返回的token),否则会提示:鉴权失败,从而导致业务接口无法使用。获取sign接口请求参数为业务接口的请求参数,所以Pre-requestScript(预处理)post请求内的body为变量。一、Pre-requestScript(预处理)......
  • 【iOS逆向与安全】某车之家sign签名分析
    1.目标分析某车之家sign签名算法的实现2.操作环境fridamac系统Charles抓包越狱iPhone3.流程寻找切入点通过Charles抓包获取到关键词为_sign,这也就是我们的切入点:静态分析在静态分析前,我们先观察sign值的特征,比如32位就有可能是md5,数字加字母加+/然后以=号结......
  • 阿里低代码引擎 | LowCodeEngine - 如何将新组件库接入物料,以TDesign为例
    阿里低代码引擎默认提供了以antd为例的物料,并搭建了以antd为物料的示例demo,但实际中我们使用的不一定就是antd,有可能是别的组件库,也有可能是自己公司的组件库,这里就以自己为例告诉大家如何将新组件库接入阿里低代码引擎的物料中,以腾讯的组件库tdesign为例。先看阿里低代码引擎的d......
  • mysqld got signal 11
    【1】mysql实例启动故障5.7.21-》5.7.42数据库升级后,启动发现错误日志如下2023-08-10T23:05:53.463377+08:000[Warning]TIMESTAMPwithimplicitDEFAULTvalueisdeprecated.Pleaseuse--explicit_defaults_for_timestampserveroption(seedocumentationformore......
  • 安装 Altium Designer(AD20)
     ComponentFromSupplierSearch   在电路设计中,AltiumDesigner是较为常见的设计软件。但是在安装AD时,可能是会出现如下报错:AD报错内容:Unabletodownloadextension.“ComponentFromSuplierSearch”;Errordetails:Cannotfindextension“ComponentFromSuplierSearch”.解......
  • Exception in thread "main" java.net.BindException: Cannot assign requested addre
    两种情况1.端口号被占用,导致地址无法绑定#windows查看端口pidnetstat-aon|findstr8080(端口号)#linux查看端口占用netstat-anp|grep80802.ip地址与本机地址不匹配,导致地址无法绑定#windows查看ipipconfig#linux查看ipifconfig......