码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode每日一题(2149. Rearrange Array Elements by Sign)


    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.

    题比较简单, 把 nums 中的正数和负数分开来, 然后再 merge 起来就可以了。


    
    impl Solution {
        pub fn rearrange_array(nums: Vec<i32>) -> Vec<i32> {
            let mut positives = Vec::new();
            let mut negatives = Vec::new();
            nums.into_iter().for_each(|v| {
                if v > 0 {
                    positives.push(v);
                    return;
                }
                negatives.push(v);
            });
            positives
                .into_iter()
                .zip(negatives)
                .fold(Vec::new(), |mut l, (p, n)| {
                    l.push(p);
                    l.push(n);
                    l
                })
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    SpringBoot整合SQLite
    GBase 8c V3.0.0数据类型——系统表信息函数
    Flutter应用发布流程详解:从开发到上架一站式指南
    AWS 中文入门开发教学 39- AWS CLI - AWS认证 必须会的命令行工具
    vue模板语法: 插值语法和指令语法以及v-bind指令使用
    10月19日,每日信息差
    MySQL日志(undo log 和 redo log 实现事务的原子性/持久性/一致性)
    【集群】LVS+Keepalived群集
    【C语言】文件的操作与文件函数的使用(详细讲解)
    SAP小技巧 S4系统的互信访问
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/127682966
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号