2578. 最小和分割 01.18(三)

给你一个正整数 num ,请你将它分割成两个非负整数 num1 和 num2 ,满足:

  • num1 和 num2 直接连起来,得到 num 各数位的一个排列。
    • 换句话说,num1 和 num2 中所有数字出现的次数之和等于 num 中所有数字出现的次数。
  • num1 和 num2 可以包含前导 0 。

请你返回 num1 和 num2 可以得到的和的 最小 值。

注意:

  • num 保证没有前导 0 。
  • num1 和 num2 中数位顺序可以与 num 中数位顺序不同。

示例 1:

输入:num = 4325
输出:59
解释:我们可以将 4325 分割成 num1 = 24 和 num2 = 35 ,和为 59 ,59 是最小和。

示例 2:

输入:num = 687
输出:75
解释:我们可以将 687 分割成 num1 = 68 和 num2 = 7 ,和为最优值 75 。

提示:

  • 10 <= num <= 109
  • public class Solution {
        public int SplitNum(int num) {
            List<int> iList = new List<int>();
            while(num!=0){
                iList.Add(num%10);
                num /= 10;
            }
            iList.Sort();
            int len = iList.Count;
            int c = len/2;
            int res1 = 0, res2 =0;
            for(int i=0;i<len/2;i++){
                res1 *= 10;
                res1 += iList[2*i];
                res2 *= 10;
                res2 += iList[2*i+1];
            }
            if(len%2 == 1){
                res1 *=10;
                res1 += iList[iList.Count-1];
            }
            return res1 + res2;
        }
    }
    public class Solution {
        public int SplitNum(int num) {
            List<int> iList = new List<int>();
            while(num!=0){
                iList.Add(num%10);
                num /= 10;
            }
            iList.Sort();
            int len = iList.Count;
            int res1 = 0, res2 =0;
    
            for(int i =0;i < len; i++)
            {
                if(i % 2 ==0) res1 = res1*10 + iList[i];
                else res2 = res2*10 + iList[i];
            }
            return res1 + res2;
        }
    }

  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小路灯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值