码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • leetcode每日一题——Split With Minimum Sum


    文章目录

      • 一、题目
      • 二、题解

    一、题目

    2578. Split With Minimum Sum

    Given a positive integer num, split it into two non-negative integers num1 and num2 such that:

    The concatenation of num1 and num2 is a permutation of num.
    In other words, the sum of the number of occurrences of each digit in num1 and num2 is equal to the number of occurrences of that digit in num.
    num1 and num2 can contain leading zeros.
    Return the minimum possible sum of num1 and num2.

    Notes:

    It is guaranteed that num does not contain any leading zeros.
    The order of occurrence of the digits in num1 and num2 may differ from the order of occurrence of num.

    Example 1:

    Input: num = 4325
    Output: 59
    Explanation: We can split 4325 so that num1 is 24 and num2 is 35, giving a sum of 59. We can prove that 59 is indeed the minimal possible sum.
    Example 2:

    Input: num = 687
    Output: 75
    Explanation: We can split 687 so that num1 is 68 and num2 is 7, which would give an optimal sum of 75.

    Constraints:

    10 <= num <= 109

    二、题解

    我的麻烦解法

    class Solution {
    public:
        int splitNum(int num) {
            vector<int> vals;
            while(num){
                vals.push_back(num % 10);
                num /= 10;
            }
            sort(vals.begin(),vals.end(),greater<int>());
            int a = 0,b = 0;
            int oddCount = 0,evenCount = 0;
            for(int i = 0;i < vals.size();i++){
                if(i % 2 == 0){
                    if(oddCount == 0) a += vals[i];
                    else a += pow(10,oddCount) * vals[i];
                    oddCount++;
                }
                else{
                    if(evenCount == 0) b += vals[i];
                    else b += pow(10,evenCount) * vals[i];
                    evenCount++;
                }
            }
            return a + b;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    更好的解法

    得到num1和num2时从最高位开始乘更好,不要像我上面那样从个位开始乘

    class Solution {
    public:
        int splitNum(int num) {
            string stnum = to_string(num);
            sort(stnum.begin(), stnum.end());
            int num1 = 0, num2 = 0;
            for (int i = 0; i < stnum.size(); ++i) {
                if (i % 2 == 0) {
                    num1 = num1 * 10 + (stnum[i] - '0');
                }
                else {
                    num2 = num2 * 10 + (stnum[i] - '0');
                }
            }
            return num1 + num2;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    【进阶】Spring中的注解与反射
    【深入浅出Java并发编程指南】「源码分析篇」透析ThreadLocal线程私有区域的运作机制和源码体系
    常见排序算法之选择排序
    Unity3D赛车游戏+脚本基础
    【追光者】大学即(已)将(经)毕业,四年,我的所感所想(部分)。
    python笔记--函数、异常
    Python基础:【习题系列】列表、元组、字典和集合
    基于java+ssm+shiro的出租房管理平台
    新型超导Fluxonium量子比特正加速量子计算机的创建
    FPGA时序分析
  • 原文地址:https://blog.csdn.net/weixin_46841376/article/details/133712912
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号